Nginx别名指令不适用于php [英] Nginx alias directive not working with php

查看:71
本文介绍了Nginx别名指令不适用于php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an app that is running on Nginx with a working server block like so:

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文件,并且可以正常工作.

When I visit /foo, Nginx looks in the path /home/deployer/apps/modules/foo/ for an index.php file and it works.

问题:

我使用capistrano设置了一个部署脚本,该脚本可以部署到foo目录:

I set up a deploy script using capistrano that deploys to the foo directory:

/home/deployer/apps/modules/foo/

Capistrano在'foo'目录中创建一个'当前'目录,以包含从Github提取的应用程序文件,因此我需要将根路径更改为:

Capistrano creates a 'current' directory within the 'foo' directory to contain the application files pulled in from Github, so I needed to change the root path to be:

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

但是Nginx将location指令附加到root指令的末尾....因此,当您访问/foo时,Nginx会尝试查找:

But Nginx appends the location directive to the end of the root directive.... so, when you visit /foo, Nginx tries to look in:

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

使用别名应该无视location指令中设置的/foo并从确切的别名路径(日志确认正在发生)中提供文件,但是当我使用alias指令时,php配置未正确应用而我得到的是404.

Using alias is supposed to disregard the /foo set in the location directive and serve files from the exact alias path (which the logs confirm is happening), but when I use the alias directive, the php configuration is not being applied correctly and I am getting a 404 returned.

如果我回到root指令并完全删除当前"目录,则可以正常工作.我需要从当前"目录提供文件,以使Capistrano部署顺利进行,但无法弄清楚如何使alias指令与php一起使用.

If I go back to the root directive and remove the 'current' directory altogether, it works fine. I need the files to be served from the 'current' directory to work smoothly with the Capistrano deploy, but cannot figure out how to make the alias directive work with php.

任何人有任何想法或建议,我想念什么吗?

Anyone have any ideas or advice, am I missing something?

推荐答案

感谢@ xavier-lucas关于无法使用带有别名的try_files的建议.

thanks to @xavier-lucas for the suggestion about not being able to use try_files with alias.

要在php中使用别名,我必须从原始问题所示的php位置块中删除try_files指令:

To use alias with php, I had to remove the try_files directive from the php location block shown in the original question:

try_files $uri =404;

我实际上不得不在/foo位置重新声明php位置块,并删除上面的行.最终看起来像这样:

I actually had to restate the php location block within the /foo location and remove the above line. It ended up looking like this:

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文件.

This allows php files to be processed directly from the directory listed in the alias directive.

这篇关于Nginx别名指令不适用于php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆