一、前言
在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加 /,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。
二、举例
- 假设地址:https://1103s.cn/wxapp/wx/custom/getTitleList
域名 | 匹配符 | 接口地址 |
---|---|---|
1103s.cn | /wxapp/ | /wx/custom/getTitleList |
- 例1:代理到URL:http://127.0.0.1/wx/custom/getTitleList
#小程序服务
location /wxapp/ {
proxy_pass http://127.0.0.1:8082/;
}
- 例2:代理到URL:http://127.0.0.1/wxapp/wx/custom/getTitleList
#小程序服务
location /wxapp/ {
proxy_pass http://127.0.0.1:8082;
}
从例1 例2中得出结论:如果你的proxy_pass地址,最后是以 / 结尾的,nginx转发时候回去掉/wxapp、反之带上/wxapp。
- 例3:代理到URL:http://127.0.0.1/sss/wx/custom/getTitleList
#小程序服务
location /wxapp/ {
proxy_pass http://127.0.0.1:8082/sss/;
}
- 例4:代理到URL:http://127.0.0.1/ssswx/custom/getTitleList
#小程序服务
location /wxapp/ {
proxy_pass http://127.0.0.1:8082/sss;
}
例3 例4来说,假设proxy_pass地址后还有地址,那么针对例1 例2 的结论完全不同,
无论您最后是不是 / 结尾,都不可能带/wxapp,而差一点是,你转发地址最后/sss的路径,会一直尾随,
- 有 / 地址路径就有(http://127.0.0.1/sss/wx/custom/getTitleList )
- 没有 / 地址路径就是直接拼接的(http://127.0.0.1/ssswx/custom/getTitleList )
😁 作者:Teddy (公众号:鸡仓故事汇)
ok!到这里就大功告成,小编(Teddy)在这里先感谢大家的到来。
虽然不是太详细,小编已经很努力,给小编来个一键三连(点赞,关注,收藏),小编会越来越努力。。。