我有一个在 Nginx 上运行的应用程序,它的工作服务器 block 如下所示:
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 文件并且它可以工作。
问题:
我使用部署到 foo 目录的 capistrano 设置部署脚本:
/home/deployer/apps/modules/foo/
Capistrano 在“foo”目录中创建一个“当前”目录以包含从 Github 提取的应用程序文件,因此我需要将根路径更改为:
/home/deployer/apps/modules/foo/current/
但 Nginx 将位置指令附加到根指令的末尾....因此,当您访问/foo 时,Nginx 会尝试查找:
/home/deployer/apps/modules/foo/current/foo/
使用别名应该忽略位置指令中的/foo 设置并从确切的别名路径提供文件(日志确认正在发生),但是当我使用别名指令时,php 配置没有被正确应用我得到了 404 返回。
如果我回到根指令并完全删除“当前”目录,它就可以正常工作。我需要从“当前”目录提供文件以便与 Capistrano 部署一起顺利工作,但无法弄清楚如何使别名指令与 php 一起工作。
任何人有任何想法或建议,我是否遗漏了什么?
最佳答案
感谢@xavier-lucas 关于不能使用带别名的 try_files 的建议。
要对 php 使用别名,我必须从原始问题中显示的 php 位置 block 中删除 try_files 指令:
try_files $uri =404;
我实际上不得不在/foo 位置重申 php location block 并删除上面的行。它最终看起来像这样:
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 文件。
关于Nginx 别名指令不适用于 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27066463/