nginx 重定向循环,index.html

use*_*667 2 nginx

这看起来很荒谬,但我在一个多小时的搜索中没有找到有效的答案。当我访问“ http://oa.wechat.com/screen/index.html ”时,它会导致一个301重定向循环,如下所示:

"GET /screen/ HTTP/1.1" 301
"GET /screen/index.html/ HTTP/1.1" 301 
"GET /screen/index.html/index.html/ HTTP/1.1" 301 
... 
Run Code Online (Sandbox Code Playgroud)

nginx 版本:1.5.6 nginx.conf

    server {
        listen  80;
        server_name oa.wechat.com;

        location ~ ^/screen/ {
            alias /data/screen/static/;
            index index.html;
        }  
    } 
Run Code Online (Sandbox Code Playgroud)

谁能告诉我原因?非常感谢。

我已经检查了 nginx 文档。“别名”的正确用法:

    # use normal match like this
    location  /i/ {
      alias  /spool/w3/images/;
    }

    # use regex match like this
    location ~ ^/download/(.*)$ {
      alias /home/website/files/$1;
    }
Run Code Online (Sandbox Code Playgroud)

使用“别名”的错误方法是:

    location ~ ^/screen/ {
        alias /data/screen/static/;
        index index.html;
    }  
Run Code Online (Sandbox Code Playgroud)

在这种情况下,请求将被视为目录请求,而不是文件请求,这将导致重定向循环。

无论如何,非常感谢肉!

Fle*_*der 5

它已经在尝试访问index.html该目录,因为它是 nginxindex指令的默认值。问题是您index在一个location中使用该指令,该指令具有特殊含义并执行内部重定向(如文档所述)。

除非您知道自己在做什么,否则请indexserverblock 中设置指令。我们最终得到以下server块(请务必阅读评论)。

server {
    # Both default values and not needed at all!
    #index      index.html;
    #listen     80;
    server_name oa.wechat.com;

    # Do not use regular expressions to match the beginning of a
    # requested URI without protecting it by a regular location!
    location ^~ /screen/ {
        alias /data/screen/static/;
    }

}
Run Code Online (Sandbox Code Playgroud)

location 例子

server {

    # Won't work because the /data is considered the new document root and
    # the new location matches the regular expression again.
    location ~ ^/screen/ {
        alias /data/screen/static/;
    }

    # Should work because the outer location limits the inner location
    # to start with the real document root (untested)
    location / {
        location ~ ^/screen/ {
            alias /data/screen/static/;
        }
    }

    # Should work as well above reason (untested)
    location / {
        location ~ ^(/screen/) {
            alias /data$1static/;
        }
    }

    # Might work as well because we are using the matching group
    # VERY BAD because we have a regular expression outside any regular location!
    location ~ ^(/screen/) {
        alias /data$1static/;
    }

    # Always works and allows nesting of more directives and is totally save
    location ^~ /screen/ {
        alias /data/screen/static/;
    }

}
Run Code Online (Sandbox Code Playgroud)

网页链接