posts - 33,comments - 2,views - 11241

1、location后面带 / ,proxy_pass 后面不带 /

location /aaa/{
    proxy_pass http://127.0.0.1:9099/test4/hello/getProxyUrl;
}

 

测试地址:http://localhost:10000/aaa/123

 测试结果:请求失败!

实际地址 http://127.0.0.1:9099/test4/hello/getProxyUrl123,参数123直接拼接放在了最后,且没有 / ,导致请求失败

 

 2、location后面带 / ,proxy_pass 后面带 /

 

 测试结果:成功!实际地址为:http://127.0.0.1:9099/test4/hello/getProxyUrl/123,参数123拼接在了最后

 

3、location后面不带 / ,proxy_pass 后面带 /

location /aaa{
    proxy_pass http://127.0.0.1:9099/test4/hello/getProxyUrl/;
}

 

 

 

测试结果:成功!实际地址为:http://127.0.0.1:9099/test4/hello/getProxyUrl//123,参数123拼接在了最后,且多了个 /

 

4、location后面不带 / ,proxy_pass 后面不带 /

location /aaa{
    proxy_pass http://127.0.0.1:9099/test4/hello/getProxyUrl;
}

 

 测试结果:成功!实际地址为:http://127.0.0.1:9099/test4/hello/getProxyUrl/123,参数123拼接在了最后

 

5、location匹配中如何把参数拼接到proxy_pass后面的呢?以location不带 / 为例,因为结果看的更清楚

location /aaa{
    proxy_pass http://127.0.0.1:9099/test4/hello/getProxyUrl/;
}

测试地址为localhost:10000/aaaBBBB ,可以看到地址中并没有传递参数,但看下面的运行结果,请求成功了,且参数为BBBB

 

 nginx从/aaa处截断,一分为二,/aaa后面的字符串会原封不动的拼接到proxy_pass后面。location后面的 / 只会影响url匹配,proxy_pass后面的 / 只会影响实际url的拼接。

 

总结:location、proxy_pass最好都带 / ,或者都不带 /;url匹配时,location后面的 / 就是一个普通字符来处理,没有特殊的含义。而proxy_pass中的 / 是资源分割符,是个特殊字符。

  区别:location带 / 时,像 /aaaBBB 这种是不会匹配的;location不带 / ,像 /aaaBBB 这种是会匹配的。location带与不带 / 看实际需求。

 Tip:  多个location规则优先匹配最长字符串的

posted on   李白菜  阅读(1020)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示