nginx $ document_root没有评估root指令正则表达式

时间:2015-11-29 18:32:08

标签: nginx

我的目标是拥有通配符子域,每个子域都从自己的目录中提供php。例如," www"子域名来自" public_html / www"和#34;博客"来自" public_html / blog。"这是我的nntx配置,它允许部分工作:

server {
    listen      80;
    server_name ~^(.*)\.example\.com$;

    root        /var/www/example.com/public_html/$1;
    index       index.php index.htm;

    location ~ \.php$ {
        fastcgi_split_path_info         ^(.+\.php)(/.+)$;
        fastcgi_pass    unix:/var/run/php5-fpm.sock;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }
}

为了测试,我在www目录中有一个index.php,在blog目录中有一个index.htm。博客子域工作正常,并显示博客目录中的index.htm。

然而,www子域不起作用,页面返回"没有指定输入文件。"如果我硬编码" www"进入根指令而不是" $ 1"它适用于" www"子域名,但当然博客也会显示该内容。

因此,似乎php特定位置中的$ document_root不会评估代表子域的根指令的$ 1。在使用$ document_root?

时,需要更改什么或如何确保评估根目录

1 个答案:

答案 0 :(得分:3)

尝试使用命名捕获而不是数字捕获。

server_name ~^(?<subsystem>.+)\.example\.com$;
root        /var/www/example.com/public_html/$subsystem;

可能会root评估迟到(我不知道),而$1会被location正则表达式覆盖。