NGINX try_files +别名指令 [英] NGINX try_files + alias directives

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

问题描述

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

I'm trying to serve request to /blog subdirectory of a site with the php code, located in a folder outside document root directory. Here's my host config:

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文件的代码.

just a code of index.php file from /home/alex/www/test2/ folder.

但是,此配置:

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?

gives me index.html file from /home/alex/www/test2/. Please give me a clue - why? And how can I force NGINX to process /home/alex/www/test1/index.php instead?

推荐答案

这是一个长期存在的错误在nginx中.但是您可以再次使用root指令来解决.有点骇人听闻,但至少可以奏效.

This is a long standing bug in nginx. But you can work around by using the root directive again. Kind of a hack, but at least it works.

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;
    }
}

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

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