Nginx location 配置 后路径映射问题 (路径替换),反向代理路径加不加“/“

刺骨的言语ヽ痛彻心扉 2021-06-22 15:38 758阅读 0赞

nginx 配置 proxy_pass时可以实现URL路径的部分替换。

1.proxy_pass的目标地址,默认不带/,表示只代理域名,url和querystring部分不会变(把请求的path拼接到proxy_pass目标域名之后作为代理的URL)

2.如果在目标地址后增加/,则表示把path中location匹配成功的部分剪切掉之后再拼接到proxy_pass目标地址

例子:

server {
access_log /home/access.log;
error_log /home/error.log;
server_name h5.xxx.com;
location /abc {
proxy_pass http://server\_url;
}

  1. location /abc \{
  2. proxy\_pass http://server\_url/;
  3. \}

}

比如请求 /abc/b.html

如上两个匹配成功后,实际代理的目标url分别是

http://server\_url/abc/b.html (把/abc/b.html拼接到http://server\_url之后)

http://server\_url/b.html (把/abc/b.html的/abc去掉之后,拼接到http://server\_url/之后)

发表评论

表情:
评论列表 (有 0 条评论,758人围观)

还没有评论,来说两句吧...

相关阅读