这看起来很可笑,但我在一个多小时的搜索中还没有找到一个有效的答案。当我访问"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
...
nginx版本: 1.5.6 nginx.conf
server {
listen 80;
server_name oa.wechat.com;
location ~ ^/screen/ {
alias /data/screen/static/;
index index.html;
}
}
有人能告诉我原因吗?非常感谢。
我已经检查了nginx文档。“alias”的正确用法:
# use normal match like this
location /i/ {
alias /spool/w3/images/;
}
# use regex match like this
location ~ ^/download/(.*)$ {
alias /home/website/files/$1;
}
“alias”的错误用法是:
location ~ ^/screen/ {
alias /data/screen/static/;
index index.html;
}
在这种情况下,请求将被视为目录请求,而不是文件请求,这将导致重定向循环。
无论如何,非常感谢肉体!
发布于 2014-02-11 23:01:56
它已经在尝试访问该目录中的index.html
,因为它是nginx的index
directive的默认值。问题是您在location
block中使用index
指令,它有特殊的含义并执行内部重定向(如文档所述)。
除非您知道自己在做什么,否则请在server
block中设置index
指令。我们以下面的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/;
}
}
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/;
}
}
网络链接
发布于 2016-09-19 02:00:48
您应该将^
位置修饰符从^/screen/
移动,然后在~
之前添加^
,如下所示:
`location ^~ /screen/ {
alias /data/screen/static/;
index index.html;
}`
https://stackoverflow.com/questions/21705198
复制相似问题