nginx别名+位置指令

时间:2012-04-10 06:32:11

标签: nginx location alias

server {
    listen      80;
    server_name  pwta; 
    root html;

    location /test/{
        alias html/test/;
        autoindex on;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

此配置有效。但是,如果location /test/被替换,例如location /testpath/它不起作用(未指定输入文件)。我假设根据别名指令的解释,“位置”部分被删除,因此/testpath/info.php将导致html/test/info.php

感谢您的任何建议。

2 个答案:

答案 0 :(得分:11)

nginx alias

    server {
    listen      80;
    server_name  pwta;
    index index.html index.php;
    root html;

    location /testpath/ {
        alias html/test/;
    }
    location ~  ^/testpath/(.+\.php)$ { ### This location block was the solution
        alias html/test/;     
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$1;
        include fastcgi_params;
    }
     location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

答案 1 :(得分:8)

aliasroot指令最适用于绝对路径。您可以使用相对路径,但它们与用于编译nginx的prefix配置选项相关,通常不是您想要的。

您可以通过执行nginx -V并查找--prefix=之后的值来查看此内容。

通过查看日志证明这一点,你会发现“没有这样的文件”错误。

相关问题