我有以下配置:
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
(带有和不带有斜杠)一起使用。
答案 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;
}