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

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

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

例子:

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


比如请求 /abc/b.html
nginx转发后请求的地址
http://server_url/abc/b.html (把/abc/b.html拼接到http://server_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/;
        }
 }


比如请求 /abc/b.html
nginx转发后请求的地址

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

 

 












 
posted @ 2023-12-06 14:16  沧海一滴  阅读(833)  评论(0编辑  收藏  举报