在第二个相同的情况下下载nginx php文件

server { listen 80; server_name example.com; root /srv/www/example.com; access_log /srv/www/example.com/logs/access.log; error_log /srv/www/example.com/logs/error.log; location / { index index.html index.php; } location /site1 { try_files $uri $uri/ /index.php; index index.php index.html; root /srv/www/example.com; } #Pass the PHP scripts to FastCGI server location ~ /site1/.+\.php$ { set $php_root /srv/www/example.com; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name; include fastcgi_params; } location /site2 { try_files $uri $uri/ /index.php; index index.php index.html; root /srv/www/example.com; } #Pass the PHP scripts to FastCGI server location ~ /site2/.+\.php$ { set $php_root /srv/www/example.com; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name; include fastcgi_params; } 

}

当用户访问域http://example.com时,文件http://example.com/index.html在浏览器中呈现(文件path:/srv/www/example.com/index.html)。

当用户访问域名http://example.com/site1时,文件http://example.com/site1/index.php在浏览器中呈现(文件path:/srv/www/example.com/site1/的index.php)。

但是当用户访问域名http://example.com/site2时,文件http://example.com/site2/index.php是DOWNLOADED(文件path:/srv/www/example.com/site2/index)。 PHP)。

站点1和站点2的configuration是相同的。 为什么一个人工作,一个人不工作? 我究竟做错了什么?

其实你做的很多事情都错了:)

第一个问题是你在location块内使用重复的root指令。 然后你使用$php_rootvariables,当没有必要的时候。

一个小问题是,你正在把你的日志写入一个可公开访问的目录(在根目录下)。

这是一个简化的configuration,应该能够完成您正在尝试的操作:

 server { listen 80; server_name example.com; root /srv/www/example.com; access_log /srv/www/example.com/logs/access.log; error_log /srv/www/example.com/logs/error.log; index index.html index.php; location /site1/ { alias /srv/www/example.com/site1/; try_files $uri $uri/ /index.php; } location /site2/ { alias /srv/www/example.com/site2/; try_files $uri $uri/ /index.php; } # Alternative solution to the above two location blocks # location ~ /site(?<sitenr>[1-2])/ { # alias /srv/www/example.com/site$sitenr/; # } location ~ \.php$ { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } } 

在这里,我们只需用alias指令声明/site1 / /site2位置,然后try_files将尝试提供这些文件。 如果找不到该文件,则将请求传递给PHP处理块。

在PHP处理块中,我们使用$document_root来传递位于块中的alias目录规范。

所以我们只需要一个PHP块,应该避免这个问题。

在另一个单一的location块示例中,我使用正则expression式将引用的站点编号捕获到variables中,然后将该variables用于alias指令。 这进一步消除了不必要的重复。