Nginx 别名指令不适用于 php

标签 nginx php server

我有一个在 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/

相关文章:

php - date strtotime() 计算返回 1969 年的日期

php - 如何让 DOCUMENT_ROOT 在本地主机和网络主机上工作?

php - Laravel 5.2 获取Linux系统环境变量

ssl - 如何在nginx反向代理配置中的proxy_ssl_name中指定多个服务器名称

Nginx - 如果 IP 未列入白名单,则显示特定页面

iis - Web 服务器是否可以向自己发出 HTTPS 请求?

python - 在 python 套接字中为 socket.connect() 指定源 IP 地址

html - 什么是文件扩展名 .bhtml 以及为什么使用它?

ruby-on-rails - rbenv : bundle: command not found on production server

ruby-on-rails - Rails 3.2.2 在使用 capistrano 部署后在样式表和 js Assets 上获得 404