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规则优先匹配最长字符串的
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!