Nginx缺少尾部斜杠返回301

时间:2018-08-03 08:57:07

标签: 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;
  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(带有和不带有斜杠)一起使用。

1 个答案:

答案 0 :(得分:1)

$uri/语句中的try_files项会使nginx附加尾随/到请求的URI(如果该URI解析为本地目录)。有关更多信息,请参见this document

结尾的/通过发出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;
}
相关问题