Nginx缺少尾部斜杠返回301

Nginx缺少尾部斜杠返回301,nginx,reverse-proxy,Nginx,Reverse Proxy,我有以下配置: 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; loc

我有以下配置:

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;
  }
}
当导航到http://localhost:8000/app/ 除删除尾部斜杠外,所有操作均按预期进行http://localhost:8000/app nginx返回301状态响应,我被重定向到http://localhost/app.

如何使nginx同时使用这两种工具http://localhost:8000/app/ 和http://localhost:8000/app 带或不带尾随斜杠。

try\u files语句中的$uri/term会导致nginx将尾随/附加到请求的uri,如果该uri解析为本地目录。更多信息,请参阅

尾随/通过发出3xx响应追加,当然nginx得到的端口是错误的,因为它对端口8000一无所知

如果不希望nginx发出任何3xx响应,只需从try_files语句中删除$uri/term即可

例如:

location  /app {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri /app/index.html?$args;
}
try_files语句中的$uri/term会导致nginx在请求的uri后面附加一个/如果该uri解析为本地目录。更多信息,请参阅

尾随/通过发出3xx响应追加,当然nginx得到的端口是错误的,因为它对端口8000一无所知

如果不希望nginx发出任何3xx响应,只需从try_files语句中删除$uri/term即可

例如:

location  /app {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri /app/index.html?$args;
}

当没有尾随/的URI指向目录时,您希望nginx做什么?此时,$uri/告诉它发出3xx响应,而listen指令告诉它在重定向中使用端口80。@RichardSmith我想要两者,并指向index.htmlThe重定向是由try_files语句中的$uri/引起的。您可以删除它。@RichardSmith,您是否建议只放try_文件$uri/app/index.html?$args;?你能把它作为一个答案发布吗?如果没有尾随/的URI指向一个目录,你希望nginx做什么?此时,$uri/告诉它发出3xx响应,而listen指令告诉它在重定向中使用端口80。@RichardSmith我想要两者,并指向index.htmlThe重定向是由try_files语句中的$uri/引起的。您可以删除它。@RichardSmith,您是否建议只放try_文件$uri/app/index.html?$args;?你能把它作为一个答案贴出来吗?作为例外,它没有帮助。我仍然被重定向到http://localhost/app/ 去http://localhost:8000/app -仍然http://localhost:8000/app/ 按预期工作ID重新启动nginx,是否清除浏览器缓存?这没有帮助。我仍然被重定向到http://localhost/app/ 去http://localhost:8000/app -仍然http://localhost:8000/app/ 按预期工作ID重新启动nginx,是否清除浏览器缓存?