嵌套位置、别名和 php-fpm 上游的意外行为

嵌套位置、别名和 php-fpm 上游的意外行为

晚上好,

~ 我想做什么 ~

我需要一个匹配任何内容的默认位置块,location / {}我想,这个块有自己的根目录。它必须提供任何静态文件并解释其文档根目录或子文件夹中的任何 php 文件。(我设置了它,它似乎工作正常)

这就是我需要帮助的地方:(……

在同一个服务器块内,我想托管一个在访问mydomain.foo/panel/区域时提供服务的 php symphony 应用程序。因此我添加了一个^~ /panel/位置(uri 以 /panel/ 开头)

我的问题是 try_files 的行为不符合我的预期(如果 uri 与嵌套位置不匹配,则内部重定向到最后一个嵌套位置)。请参阅下面的代码注释。

~ 我的配置如下... ~

upstream phpfcgi {
    server unix:/var/run/php5-fpm.sock;
}

server {
    listen 80;
    server_name domain.tld;

    ## Default locations
    location / {
        root /home/me/sites/default/www;
        index index.php;
        location ~ \.php$ {
            include fastcgi.conf;
            fastcgi_pass phpfcgi;
        }
        location ~ /(.+) {
            try_files $uri $1 =404;
        }
    }

    ## panel area
    location ^~ /panel/ {
        # Public files are located in sf/web sub-directory
        alias /home/me/sites/sf/web/;

        # Since uri starts with '/panel/', I expect NGINX to execute try_files as a last resort.
        # if nested location do not match, and uri is not an existing file,
        # shouldn't nginx do an internal redirect with the 2nd try_files argument
        # and finally hit the nested location (triggering fastcgi_pass) ?
        # But try_files is ignored and nginx falls back to "/" location above instead.
        try_files $uri /panel/app_dev.php$is_args$args;

        # FastCGIpass to upstream
        location ~ (app_dev|config)\.php(/|$) {
            try_files $fastcgi_script_name =404;
            set $path_info $fastcgi_path_info;
            fastcgi_param PATH_INFO $path_info;
            include fastcgi.conf;
            fastcgi_pass phpfcgi;
        }
    }

    # Custom errors
    error_page 403 404 =404 @null;
    error_page 324 500 502 503 504 =500 @null;
    location @null {
        root /home/me/sites/_;
        rewrite ^.+$ /$status.html break;
        try_files $uri 444;
    }
}

如果我的配置太笨拙,那么我该如何满足我的要求?(默认位置是文档根目录,而同一台服务器上有一个为我的 php 应用程序提供服务的特殊区域)?

NGINX 文档越来越完善,但在请求处理流程中仍然存在我不明白的地方。

有什么解决方案/建议/改进吗?

谢谢:)

答案1

您使用的是嵌套位置块。我没有使用它们,因为我不太了解它们,但它们似乎增加了复杂性。尝试使用未嵌套的位置块。您的配置看起来与我见过的任何其他配置都大不相同,但我的 nginx 经验有限。

我在这个问题中有一个我的配置示例。

Nginx HHVM Wordpress 在一个中间子目录中执行 PHP 的问题

有一些最佳实践这里

这个问题还包括一个如何调试这些内容的示例,通过添加标题并查看它们。使事情更容易理解和调试。如果你从我的回答中得到的全部信息,我认为这将是有价值的。

相关内容