问题描述
我有以下配置:
服务器{听80;server_name 本地主机;位置/应用{根目录/usr/share/nginx/html;index index.html index.htm;try_files $uri $uri//app/index.html?$args;}error_page 500 502 503 504/50x.html;位置 =/50x.html {根目录/usr/share/nginx/html;}}
当导航到 http://localhost:8000/app/
时,一切正常,但删除尾部斜杠 (http://localhost:8000/app
) nginx 返回 301 状态响应,我被重定向到 http://localhost/app
.
如何让 nginx 与 http://localhost:8000/app/
和 http://localhost:8000/app
一起工作(有和没有尾随斜线).
try_files
语句中的 $uri/
术语导致 nginx
如果该 URI 解析为本地目录,则将尾随 /
附加到请求的 URI.有关更多信息,请参阅本文档.
结尾的 /
是通过发出 3xx 响应来附加的,当然 nginx
会得到端口错误,因为它对端口 8000 一无所知.
如果您不希望 nginx
发出任何 3xx 响应,只需从您的 try_files
语句中删除 $uri/
项即可.>
例如:
location/app {根目录/usr/share/nginx/html;index index.html index.htm;try_files $uri/app/index.html?$args;}
I have the following config:
server {
listen 80;
server_name localhost;
location /app {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /app/index.html?$args;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
When navigating to http://localhost:8000/app/
all works as expected but when removing the trailing slash (http://localhost:8000/app
) nginx returns 301 status response and I am being redirected to http://localhost/app
.
How can I make nginx work with both http://localhost:8000/app/
and http://localhost:8000/app
(with and without trailing slash).
The $uri/
term in the try_files
statement causes nginx
to append a trailing /
to the requested URI, if that URI resolves to a local directory. See this document for more.
The trailing /
is appended by issuing a 3xx response, and of course nginx
gets the port wrong as it knows nothing about port 8000.
If you do not want nginx
to issue any 3xx responses, simply remove the $uri/
term from your try_files
statement.
For example:
location /app {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri /app/index.html?$args;
}
这篇关于Nginx 缺少尾部斜杠返回 301的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!