在 nginx 中从不同位置提供 php 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2237189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Serving php files from different locations in nginx
提问by ErJab
I have my main website and wordpress in different directories on my server on which I use nginx as the web server. The main website is in /home/me/www and Wordpress is in /home/me/wordpress. I need to have them in separate directories this way for a particular reason. How do I specify this in the nginx configuration file? I currently have the following and it does not work:
我的主网站和 wordpress 位于我使用 nginx 作为 Web 服务器的服务器上的不同目录中。主网站在 /home/me/www 中,Wordpress 在 /home/me/wordpress 中。由于特定原因,我需要以这种方式将它们放在单独的目录中。如何在 nginx 配置文件中指定它?我目前有以下内容,但它不起作用:
location / {
root /home/me/www;
index index.php index.html index.htm;
}
location /blog {
root /home/me/wordpress;
index index.php index.html index.htm;
}
location ~ \.php$ {
set $php_root /home/me/www;
if ($request_uri ~ /blog) {
set $php_root /home/me/wordpress;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
It currently returns HTTP 404 when I try to access http://mydomain/blog
当我尝试访问http://mydomain/blog时,它当前返回 HTTP 404
采纳答案by Nick Presta
Check out this questionand the Nginx Manual.
Try changing your blogline to:
尝试将您的blog线路更改为:
location ^~ /blog/ {
root /home/me/wordpress;
index index.php index.html index.htm;
}
回答by minniux
I struggled with this for hours now and finally reached the working configurations as the following:
我为此苦苦挣扎了几个小时,终于达到了如下工作配置:
location /php-app {
passenger_enabled off;
alias /path/to/php-app/;
index index.php index.html;
try_files $uri $uri/ @rewrite;
}
location @rewrite {
rewrite ^/php-app(.*)$ /index.php?q= last;
}
location ~ \.php$ {
alias /path/to/php-app/;
rewrite ^/php-app(.*)$ last;
passenger_enabled off;
fastcgi_pass unix:/tmp/php-fpm.socket;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /path/to/php-app$fastcgi_script_name;
fastcgi_intercept_errors on;
}
回答by Ammarooo
Just adding more detail in @Nick Presta answer.
只需在@Nick Presta 答案中添加更多细节。
^~: If a carat and tilde modifier is present, and if this block is selected as the best non-regular expression match, regular expression matching will not take place.
^~:如果存在 carat 和 tilde 修饰符,并且该块被选为最佳非正则表达式匹配,则不会进行正则表达式匹配。
Checkout These differences enter link description here
结帐这些差异在此处输入链接描述

![php SQLSTATE[08004] [1040] 连接太多](/res/img/loading.gif)