Nginx的别名指令不使用PHP

我有一个在Nginx上运行的应用程序,其工作服务器块如下所示:

server { listen 80; server_name example.com; root /home/deployer/apps/my_app/current/; index index.php; location / { index index.php; try_files $uri $uri/; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/home/deployer/apps/shared/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } location /foo { root /home/deployer/apps/modules/; # tried this: # alias /home/deployer/apps/modules/foo/; # but php is not working with alias, only with root } } 

当我访问/ foo时,Nginx会在/ home / deployer / apps / modules / foo /中查找一个index.php文件,它可以工作。

问题:

我使用capistrano部署到foo目录中设置了一个部署脚本:

 /home/deployer/apps/modules/foo/ 

Capistrano在'foo'目录下创build一个'current'目录来包含从Github中获取的应用程序文件,所以我需要将根path改为:

 /home/deployer/apps/modules/foo/current/ 

但是Nginx将位置指令追加到根指令的末尾….所以,当你访问/ foo时,Nginx会尝试查找:

 /home/deployer/apps/modules/foo/current/foo/ 

使用别名应该忽略位置指令中设置的/ foo,并从确切的别名path(日志确认正在发生)提供文件,但是当我使用别名指令时,phpconfiguration没有被正确应用,而我得到一个404返回。

如果我回到根指令并删除“当前”目录,它工作正常。 我需要从“当前”目录提供的文件与Capistrano部署工作顺利,但不能弄清楚如何使别名指令工作与PHP。

任何人有任何想法或build议,我错过了什么?

感谢@ xavier-lucas提供的关于不能使用带有别名的try_files的建议。

要使用PHP的别名,我不得不从原始问题中显示的PHP位置块中删除try_files指令:

 try_files $uri =404; 

我实际上已经重申/ foo位置内的php位置块,并删除上面的行。 它最终看起来像这样:

 location /foo { alias /home/deployer/apps/modules/foo/; location ~ \.php$ { # try_files $uri =404; -- removed this line fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/home/deployer/apps/shared/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } } 

这允许php文件直接从alias指令中列出的目录进行处理。

使用

 location /foo/ { alias /home/deployer/apps/modules/foo/current/; } 

代替

 location /foo { alias /home/deployer/apps/modules/foo/; }