【问题标题】:nginx alias+location directivenginx别名+位置指令
【发布时间】:2012-04-22 11:18:27
【问题描述】:
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

感谢您的任何建议。

【问题讨论】:

    标签: nginx location alias


    【解决方案1】:

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

    【讨论】:

    • 我不完全理解为什么会这样,但它确实解决了问题。任何人都可以就中间位置块的情况添加更多解释吗?
    • 添加alias 将有效地将$document_root 覆盖为别名。请注意,它不会影响$fastcgi_script_name$request_filename。使用新的$document_root 和匹配文件名的正则表达式,解析为脚本文件。
    • 请注意,当请求在/testpath/ 下时,最后一个位置块没有做任何事情。
    • 我已经挣扎了 4 个小时了......直到我找到了这个答案......呸!
    【解决方案2】:

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

    您可以通过执行nginx -V 并找到--prefix= 后面的值来看到这一点。

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

    【讨论】:

    • 注意应该是-V 而不是-v(应该是大写,小写只给出版本号)
    猜你喜欢
    • 1970-01-01
    • 2012-11-09
    • 2013-12-23
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 2020-03-26
    相关资源
    最近更新 更多