我有一个在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
}
}
Run Code Online (Sandbox Code Playgroud)
当我访问/ foo时,Nginx在/ home / deployer / apps / modules / foo /路径中查找index.php文件,它可以工作。
问题:
我使用capistrano设置了一个部署脚本,该脚本部署到foo目录中:
/home/deployer/apps/modules/foo/
Run Code Online (Sandbox Code Playgroud)
Capistrano在'foo'目录中创建一个'current'目录,以包含从Github提取的应用程序文件,因此我需要将根路径更改为:
/home/deployer/apps/modules/foo/current/
Run Code Online (Sandbox Code Playgroud)
但是Nginx将location指令附加到root指令的末尾....因此,当您访问/ foo时,Nginx尝试查找:
/home/deployer/apps/modules/foo/current/foo/
Run Code Online (Sandbox Code Playgroud)
使用别名应该无视location指令中的/ foo设置,并从确切的别名路径(日志确认正在发生)提供文件,但是当我使用alias指令时,php配置未正确应用,得到一个404返回。
如果我回到root指令并完全删除“当前”目录,它可以正常工作。我需要从“当前”目录提供文件以与Capistrano部署一起正常工作,但无法弄清楚如何使别名指令与php一起使用。
任何人有任何想法或建议,我是否缺少任何东西?
感谢@ xavier-lucas关于无法使用带有别名的try_files的建议。
要在php中使用别名,我必须从原始问题所示的php位置块中删除try_files指令:
try_files $uri =404;
Run Code Online (Sandbox Code Playgroud)
我实际上不得不在/ 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;
}
}
Run Code Online (Sandbox Code Playgroud)
这允许直接从别名指令中列出的目录中处理php文件。