关于Nginx中的尾部斜杠行为有些困惑 [英] A little confused about trailing slash behavior in nginx
问题描述
跟踪Nginx中的Slash最近给了我一些不眠之夜.在此方面寻求帮助
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屋!