【问题标题】:NGINX try_files + alias directivesNGINX try_files + 别名指令
【发布时间】:2013-12-23 23:25:49
【问题描述】:

我正在尝试使用 php 代码向站点的 /blog 子目录提供请求,该代码位于文档根目录之外的文件夹中。这是我的主机配置:

server {
    server_name  local.test.ru;
    root   /home/alex/www/test2;

    location /blog {
        alias   /home/alex/www/test1;
        try_files $uri $uri/ /index.php$is_args$args;

        location ~ \.php$ {
            fastcgi_split_path_info ^(/blog)(/.*)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            include fastcgi_params;
        }
    }
}

我收到类似的请求

wget -O - http://local.test.ru/blog/nonExisting

只是 /home/alex/www/test2/ 文件夹中 index.php 文件的代码。

但是,这个配置:

server {
    server_name  local.test.ru;
    root   /home/alex/www/test2;

    location /blog {
        alias   /home/alex/www/test1;
        try_files $uri $uri/ /blog$is_args$args;
        index index.php;

        location ~ \.php$ {
            fastcgi_split_path_info ^(/blog)(/.*)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            include fastcgi_params;
        }
    }
}

从 /home/alex/www/test2/ 给我 index.html 文件。 请给我一个线索 - 为什么?我怎样才能强制 NGINX 处理 /home/alex/www/test1/index.php 呢?

【问题讨论】:

    标签: nginx


    【解决方案1】:

    这是a long standing bug in nginx。但是您可以再次使用root 指令来解决问题。有点像 hack,但至少它有效。

    server {
        index       index.php;
        root        /home/alex/www/test2;
        server_name local.test.ru;
    
        location /blog {
            root      /home/alex/www/test1;
            try_files $uri $uri/ /blog$is_args$args;
        }
    }
    

    【讨论】:

    • 简单地将alias 替换为root 是行不通的。在 OP 的情况下,http://localhost/blog 将在 /home/alex/www/test1 中搜索,而在此处将在 /home/alex/www/test1/blog 中搜索。尽管/blog/nonExisting 在这两种情况下都可能失败,但/blog 不会像以前那样工作。
    • 也不敢相信这个bug在3年后仍然打开!
    • 也不敢相信这个bug在4年后仍然打开!
    • 也不敢相信这个bug在5年后仍然打开!
    • 也不敢相信这个bug在6年后仍然打开!
    【解决方案2】:

    我们无法通过在 location 块中指定 root 来使其工作。我们的解决方案是改用别名。请注意,有必要在 try_files 指令中重复该位置的路径两次,然后在 .php 配置块中:

    server {
        server_name localhost;
        root /app/frontend/www;
    
        location /backend/ {
    
            alias /app/backend/www/;
    
            # serve static files direct + allow friendly urls
            # Note: The seemingly weird syntax is due to a long-standing bug in nginx: https://trac.nginx.org/nginx/ticket/97
            try_files $uri $uri/ /backend//backend/index.php?$args;
    
            location ~ /backend/.+\.php$ {
                include fastcgi_params;
                fastcgi_buffers 256 4k;
                fastcgi_param SCRIPT_FILENAME $request_filename;
                fastcgi_param HTTPS $proxied_https;
                fastcgi_pass phpfiles;
            }
    
        } # / location
    
    }
    

    来源:nginx/conf.d/app.conf 来自debian-php-nginx stack in the docker-stack project

    【讨论】:

    • 这对我在 /api 位置安装 laravel 很有用
    • "请注意,有必要在 try_files 指令中重复该位置的路径两次" .....?!它看起来很错误,但实际上有效!谢谢,永远不会想到这一点!
    • 我现在可以亲你了
    • 谢谢老兄,在我看到你的帖子之前,我要杀了人。这个错误几乎花了我整个下午才找到解决方案。
    • @DylanHunt nginx 中有一个错误,需要在这里重复两次 location 指令。为什么?不知道
    【解决方案3】:

    还有另一种解决方法可以提供更大的灵活性。它由一个指向 127.0.0.1 的 proxy_pass 指令和另一个 server 块组成。

    在您的情况下,它应该如下所示:

    upstream blog.fake {
        server 127.0.0.1;
    }
    server {
        server_name local.test.ru;
        root /home/alex/www/test2;
        index index.html;
    
        location /blog {
            proxy_pass http://blog.fake/;
        }
    }
    server {
        server_name blog.fake;
        root /home/alex/www/test1;
    
        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }
    
        location ~ \.php(/|$) {
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param HTTPS off;
        }
    }
    

    【讨论】:

    • 优雅的解决方案
    • 这是迄今为止最好的选择!
    • 我真的很喜欢这个解决方案,它可以很容易地理解正在发生的事情并且不需要复杂的嵌套。
    • 这值得更多的认可
    • 这个解决方案很棒,但它还需要将/blog 部分重新添加到REQUEST_URIfastcgi_param REQUEST_URI /blog$request_uri; 以对PHP 完全透明。
    猜你喜欢
    • 2012-04-22
    • 2014-10-08
    • 2021-03-13
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多