location 最后不带“/” 最后一段内容的模糊匹配,对后是开口的
location /abc/def 可以匹配 /abc/defghi 请求,也可以匹配 /abc/def/ghi 等
location 最后带“/” 完全匹配路径段
location /abc/def/ 不能匹配 /abc/defghi 请求,只能匹配 /abc/def/anything 这样的请求
proxy_pass 最后不带“/” 。相当于吧proxy_pass的路径替换了location匹配的路径前面的所有内容,其他不变
Nginx 则会把匹配的路径部分加入代理 uri。
请求:http://1.1.1:8001/mnt/public/img/202306/a.html
location /mnt/public/
{
proxy_pass http://abc.local;
}
真实的nginx代理之后的知识访问路径是:
http://abc.local/mnt/public/img/202306/a.html
=proxy_pass的路径+location匹配路径及后面的所有
proxy_pass 最后带“/” ,最后反向代理的路径,会删除location中的路径,直接在最前端写上proxy_pass代理的路径
相当于是绝对路径,则 Nginx 不会把 location 中匹配的路径部分加入代理 uri
请求:http://1.1.1:8001/mnt/public/img/202306/a.html
location /mnt/public/
{
proxy_pass http://abc.local/;
}
真实的nginx代理之后的知识访问路径是:
http://abc.local/img/202306/a.html
=proxy_pass的路径替换location自己以及之前的路径
注意更特殊的情况:
proxy_pass 代理的地址可能只是IP+端口,也可能会是IP+端口+部分路径
如果IP+端口的话,则代理规则与上面提到的一样。
如果IP+端口+部分路径的话,则代理时,不管proxy_pass最后有没有"/",一律会吧location中的路径删除掉,替换成proxy_pass中所有的。情况一下情况3与情况4
下面四种情况分别用http://192.168.1.9/proxy/test.html 进行访问:
情况1:
location /proxy/ {
proxy_pass http://127.0.0.1:81/;
}
会被代理到http://127.0.0.1:81/test.html 这个url。
location /proxy {
proxy_pass http://127.0.0.1:81/;
}
会被代理到http://127.0.0.1:81//test.html 这个url。 请注意这里的双斜线
情况2:
location /proxy/ {
proxy_pass http://127.0.0.1:81;
}
会被代理到http://127.0.0.1:81/proxy/test.html 这个url。
location /proxy {
proxy_pass http://127.0.0.1:81;
}
会被代理到http://127.0.0.1:81/proxy/test.html 这个url。
情况3:
location /proxy/ {
proxy_pass http://127.0.0.1:81/fly/;
}
会被代理到http://127.0.0.1:81/fly/test.html 这个url。
location /proxy {
proxy_pass http://127.0.0.1:81/fly/;
}
会被代理到http://127.0.0.1:81/fly//test.html 这个url。 请注意这里的双斜杠。
情况4:
location /proxy/ {
proxy_pass http://127.0.0.1:81/fly;
}
会被代理到http://127.0.0.1:81/flytest.html 这个url。 请注意这里的fly和test.html之间没有斜杠
location /proxy {
proxy_pass http://127.0.0.1:81/fly;
}
会被代理到http://127.0.0.1:81/fly/test.html 这个url。
在nginx中配置proxy_pass时,当在后面加上 / ,相当于绝对路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径代理走。
proxy_pass 主机+端口这种类型,后将访问地址直接加到后面即主机+端口+访问路径
proxy_pass 主机+端口+ 访问路径(包括根路径),需要去掉location后面的匹配路径,然后将剩余的添加到proxy_pass后面