Nginx 缺少尾部斜杠返回 301

vli*_*o20 1 reverse-proxy nginx

我有以下配置:

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

http://localhost:8000/app/按预期导航到所有工作但删除尾部斜杠 ( http://localhost:8000/app) 时,nginx 返回 301 状态响应,我被重定向到http://localhost/app.

我怎样才能让 nginx 同时使用http://localhost:8000/app/http://localhost:8000/app(带和不带斜杠)。

Ric*_*ith 6

如果该 URI 解析为本地目录,则语句中的$uri/术语try_files会导致在请求的 URInginx后附加一个尾随/。有关更多信息,请参阅此文档

/通过发出 3xx 响应附加尾随,当然nginx会导致端口错误,因为它对端口 8000 一无所知。

如果您不想nginx发布任何 3xx 回复,只需$uri/从您的try_files声明中删除该词即可。

例如:

location  /app {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri /app/index.html?$args;
}
Run Code Online (Sandbox Code Playgroud)