分4种情况讨论:

请求的网站为:192.168.1.1:80/static/a.html

整体配置文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server {
port 80,
server name 192.168.1.1

location /static {
proxy_pass 192.168.2.1:81
}

location /static {
proxy_pass 192.168.2.1:81/
}

location /static/ {
proxy_pass 192.168.2.1:81
}

location /static/ {
proxy_pass 192.168.2.1:81/
}
}

第一种:location后无/,转发网站无/

1
2
3
4
5
6
7
8
# 192.168.1.1 -> server name
# :80 ---------> port
# /statc -------> location
# /a.html ------> proxy_pass

location /static {
proxy_pass 192.168.2.1:81
}

最后网址经过nginx转向到的网址是 192.168.2.1:81/static/a.html

第二种:location后无/,转发网站有/

1
2
3
4
5
6
7
8
# 192.168.1.1 -> server name
# :80 ---------> port
# /statc -------> location
# /a.html ------> proxy_pass

location /static {
proxy_pass 192.168.2.1:81/
}

最后网址经过nginx转向到的网址是 192.168.2.1:81/a.html

第三种:location后有/,转发网站无/

1
2
3
4
5
6
7
8
# 192.168.1.1 -> server name
# :80 ---------> port
# /statc -------> location
# /a.html ------> proxy_pass

location /static/ {
proxy_pass 192.168.2.1:81
}

最后网址经过nginx转向到的网址是 192.168.2.1:81/static/a.html

第四种:location后有/,转发网站有/

1
2
3
4
5
6
7
8
# 192.168.1.1 -> server name
# :80 ---------> port
# /statc -------> location
# /a.html ------> proxy_pass

location /static/ {
proxy_pass 192.168.2.1:81/
}

最后网址经过nginx转向到的网址是 192.168.2.1:81/a.html

总结

从这四种可以看出,当nginx里面匹配时可以把端口后的参数分为path1+path2
(其中上方标注的location属于path1,proxy_pass属于path2)

proxy_pass

里面是ip:port+/时nginx最后匹配的网址是 proxy_pass的内容加上path2

里面是ip:port时nginx最后匹配的网址是 proxy_pass的内容加上path1+path2