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

问题描述

我正在尝试使用 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 + alias 指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 06:08