一、如果proxy_pass的目标地址后缀有/,表示把path中location匹配成功的部分剪切掉之后再拼接到proxy_pass目标地址。

示例:

uri/url:​​http://10.10.3.6/map/api/route/set​​
http:协议
10.10.3.6:IP和端口
/map/api/route/set:path
?query:访问参数,此处没有列出,实际接口请求是有
upstream nginx {
server 10.10.8.7:8080 max_fails=3 fail_timeout=9s weight=90;
server 10.10.8.8:8080 max_fails=3 fail_timeout=9s weight=90;
check_http_send "HEAD /status HTTP/1.0\r\n\r\n";
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_expect_alive http_2xx http_3xx http_4xx;
}
location /map/api/ {
proxy_pass http://nginx/service/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
}

匹配过程:

(1)location /map/api/匹配了path(/map/api/route/set)的/map/api/部分;

(2)然后path中location匹配的部分会去除,也就是/map/api/route/set会变成route/set

(3)然后去除后的path(route/set),拼接到porxy_pass目标地址后面(http://10.10.8.7:8080/service/),就形成了http://10.10.8.7:8080/service/route/set


二、如果proxy_pass的目标地址后缀没有/(默认没有),url和query部分不会变(把匹配后的path拼接到proxy_pass目标域名之后作为代理的URL)

示例:

uri/url:http://10.10.8.7:8080/service/route/set
http:协议
10.10.8.7:8080:IP和端口
/service/route/set:path
?query:访问参数,此处没有列出,实际接口请求是有

情况1:如proxy_pass目标地址中不包含path,则直接将原uri的path拼接在proxy_pass中目标地址之后;

upstream mapapp {
server 10.10.8.10:8816 max_fails=2 fail_timeout=2s weight=10;
#server 10.10.8.11:8816 max_fails=2 fail_timeout=2s weight=10;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD /status HTTP/1.0\r\n\r\n";
}
location /service/route/ {
proxy_pass http://mapapp;
proxy_redirect off;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

匹配过程:

(1)将请求的path(/service/route/set)拼接到目标域名之后作为最终代理的URL,即​http://10.10.8.10:8816/service/route/set)​


情况2:如proxy_pass目标地址中包含path,则将原uri去除location匹配表达式后的内容拼接在proxy_pass中的目标地址之后。

upstream mapapp {
server 10.10.8.10:8816 max_fails=2 fail_timeout=2s weight=10;
#server 10.10.8.11:8816 max_fails=2 fail_timeout=2s weight=10;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD /status HTTP/1.0\r\n\r\n";
}
location /service/route {
proxy_pass http://mapapp/map;
proxy_redirect off;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}


匹配过程:

(1)proxy_pass目标地址有path

(2)将path中location匹配的部分去除,也就是/service/route/set会变成/set

(3)然后去除后的path(/set),拼接到porxy_pass目标地址后面(http://10.10.8.10:8816/map),就形成了http://10.10.8.10:8816/map/set