首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >nginx重定向循环,index.html

nginx重定向循环,index.html
EN

Stack Overflow用户
提问于 2014-02-11 22:53:27
回答 2查看 4.3K关注 0票数 2

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

代码语言:javascript
运行
复制
"GET /screen/ HTTP/1.1" 301
"GET /screen/index.html/ HTTP/1.1" 301 
"GET /screen/index.html/index.html/ HTTP/1.1" 301 
... 

nginx版本: 1.5.6 nginx.conf

代码语言:javascript
运行
复制
    server {
        listen  80;
        server_name oa.wechat.com;

        location ~ ^/screen/ {
            alias /data/screen/static/;
            index index.html;
        }  
    } 

有人能告诉我原因吗?非常感谢。

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

代码语言:javascript
运行
复制
    # use normal match like this
    location  /i/ {
      alias  /spool/w3/images/;
    }

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

“alias”的错误用法是:

代码语言:javascript
运行
复制
    location ~ ^/screen/ {
        alias /data/screen/static/;
        index index.html;
    }  

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

无论如何,非常感谢肉体!

EN

回答 2

Stack Overflow用户

发布于 2014-02-11 23:01:56

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

除非您知道自己在做什么,否则请在server block中设置index指令。我们以下面的server块结束(请务必阅读注释)。

代码语言:javascript
运行
复制
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/;
    }

}

location示例

代码语言:javascript
运行
复制
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/;
    }

}

网络链接

票数 5
EN

Stack Overflow用户

发布于 2016-09-19 02:00:48

您应该将^位置修饰符从^/screen/移动,然后在~之前添加^,如下所示:

代码语言:javascript
运行
复制
 `location ^~ /screen/ {
     alias /data/screen/static/;
     index index.html;
  }`
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21705198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档