Home  >  Article  >  Backend Development  >  Use proxy to solve the problem that nginx alias cannot execute PHP and other fastcgi problems

Use proxy to solve the problem that nginx alias cannot execute PHP and other fastcgi problems

WBOY
WBOYOriginal
2016-08-08 09:22:171277browse
The alias under nginx seems to only serve static files. For FastCGI such as PHP, errors will always occur, typically 404.
There are already some solutions for this on the Internet, such as: http://u.odo.com.cn/space-2-do-blog-id-3087.html Such settings are relative to the application It is indeed usable in simple situations, such as when there is no program in the root directory or rewrite is not needed.
In actual applications, relatively more complex configurations are often required. For example, popular single-entry programs need to hand over non-existent file access to index.php, which will cause conflicts and lead to access errors. Moreover, such a configuration will also bring maintenance risks to future expansion.
I have also encountered this problem recently, and I couldn’t find a suitable answer after searching Google. For this reason, I thought why not do the opposite, split a vhost into multiple ones, and proxy the root directory to solve the problem. , the configuration is as follows:

Copy content to the clipboard

Code:
location ^~ /bbs {
alias /srv/www/bbs;
proxy_set_header Host bbs.xxx.com;
proxy_set_header X-Real-IP $ remote_addr;
proxy_pass http://127.0.0.1;
}location's ^~ means that after matching this path, other location configurations are ignored, including location /. This way rewrite or other configuration will not have any impact on this.
Some people may ask at this point, since it is so troublesome, why not just use the virtual host directly, but reverse proxy one more request?
There are many possible reasons for this, such as AJAX, javascript, cookies, etc. are not easy to handle in cross-domain situations, URLs are forward compatible, etc.

The above introduces the use of proxy to solve fastcgi problems such as nginx alias being unable to execute PHP, including aspects of the problem. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn