2012-04-10 144 views
7
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.phpnginx别名+位置指令

感谢您的任何建议。

回答

10
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; 
    } 
+0

我不完全明白为什么这样做,但确实解决了问题。任何人都可以添加更多关于中间位置块的更多解释吗? – Brad 2013-12-04 20:33:57

+0

添加'alias'将有效地覆盖'$ document_root'到任何别名。请注意,它不会影响'$ fastcgi_script_name'或'$ request_filename'。使用新的'$ document_root'和正则表达式匹配文件名,解析为脚本文件。 – Gajus 2015-03-27 16:55:50

+0

注意,当请求位于'/ testpath /'下时,最后一个位置块没有做任何事情。 – Gajus 2015-03-27 17:02:20

8

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

您可以通过执行nginx -V并找到--prefix=后面的值来查看此信息。

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

+1

请注意,应该是'-V'而不是'-v'(应该是大写,小写只给出版本号) – Basic 2015-01-05 12:18:40