对 nginx 中的尾部斜杠行为有点困惑 [英] A little confused about trailing slash behavior in nginx
问题描述
nginx 中的尾随斜线最近让我有些不眠之夜.请求一些帮助
Trailing Slash in nginx has been giving me some sleepless nights lately. Requesting some help with this
问题:proxy_pass
中奇怪的尾部斜杠行为.
Question : Strange trailing slash behavior in proxy_pass
.
那为什么会这样:
location /myapi/ {
proxy_pass http://node_server8/;
}
这不会
location /myapi/ {
proxy_pass http://node_server8;
}
注意第二个代码块中 http://node_server8
末尾缺少的尾部斜杠.这特别奇怪,因为我有一些其他配置,在后端没有斜杠,并且一切正常.
Notice the missing trailing slash at the end of http://node_server8
in second code block. This is specially strange as I have a few other configurations where I don't have a trailing slash on the backend and all works fine.
推荐答案
它们完全不同.
在第一个 proxy_pass
语句中,您包含了一个值为 /
的 URI 参数.在第二个你还没有.
In the first proxy_pass
statement you have included a URI parameter with a value of /
. In the second you haven't.
当您给 proxy_pass
一个 URI 参数(在前缀 location
内)时,它会像 alias
函数一样转换请求的 URI,从而location
指令的值替换为 URI 参数的值.例如 /myapi/foo
在被上游传递之前变成了 /foo
.
When you give proxy_pass
a URI parameter (within a prefix location
), it transforms the requested URI similarly to the alias
function, whereby the value of the location
directive is substituted for the value of the URI parameter. For example /myapi/foo
becomes /foo
before being passed upstream.
如果您没有为 proxy_pass
提供 URI 参数,则不会发生任何转换,并且请求 /myapi/foo
会原封不动地向上游传递.
If you do not provide proxy_pass
with a URI parameter, no transformation takes place, and the request /myapi/foo
is passed upstream unchanged.
有关详细信息,请参阅本文档.
See this document for details.
这篇关于对 nginx 中的尾部斜杠行为有点困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!