Nginx location proxy_pass 后面的url 加與不加/的區(qū)別
這里我們分4種情況討論
這里我們請求的網(wǎng)站為:192.168.1.123:80/static/a.html
整個(gè)配置文件是
server {
port 80,
server name 192.168.1.123
location /static {
proxy_pass 192.168.2.321:81
}
location /static {
proxy_pass 192.168.2.321:81/
}
location /static/ {
proxy_pass 192.168.2.321:81
}
location /static/ {
proxy_pass 192.168.2.321:81/
}
}
我們分開來講:
第一種:
location后沒有/ 轉(zhuǎn)發(fā)網(wǎng)站沒有/
#192.168.1.123->server name
# :80 ---------> port
#/statc ------->location
#/a.html ------>proxy_pass
location /static {
proxy_pass 192.168.2.321:81
}
最后網(wǎng)址經(jīng)過nginx轉(zhuǎn)向到的網(wǎng)址是 192.168.2.321:81/static/a.html
第二種:
location后沒有/ 轉(zhuǎn)發(fā)網(wǎng)站有/
#192.168.1.123---->server name
# :80 ------------> port
#/statc ---------->location
#/a.html --------->proxy_pass
location /static {
proxy_pass 192.168.2.321:81/
}
最后網(wǎng)址經(jīng)過nginx轉(zhuǎn)向到的網(wǎng)址是 192.168.2.321:81/a.html
第三種:
location后有/ 轉(zhuǎn)發(fā)網(wǎng)站沒有/
#192.168.1.123-->server name
# :80 ------------> port
#/statc/ ---------->location
#a.html --------->proxy_pass
location /static/ {
proxy_pass 192.168.2.321:81
}
最后網(wǎng)址經(jīng)過nginx轉(zhuǎn)向到的網(wǎng)址是 192.168.2.321:81/static/a.html
第四種:
location后有/ 轉(zhuǎn)發(fā)網(wǎng)站有/
#192.168.1.123-->server name
# :80 ------------> port
#/statc/ ---------->location(path1)
#a.html --------->proxy_pass (path2)
location /static/ {
proxy_pass 192.168.2.321:81/
}
最后網(wǎng)址經(jīng)過nginx轉(zhuǎn)向到的網(wǎng)址是 192.168.2.321:81/a.html
總結(jié):
從這四種我們可以的看出,當(dāng)nginx里面匹配時(shí)可以把端口后的參數(shù)分為path1+path2(其中我在上方標(biāo)注的location屬于path1,proxy_pass屬于path2)
當(dāng)proxy_pass
里面是ip:port+/時(shí)nginx最后匹配的網(wǎng)址是 proxy_pass的內(nèi)容加上path2
里面是ip:port時(shí)nginx最后匹配的網(wǎng)址是 proxy_pass的內(nèi)容加上path1+path2
轉(zhuǎn)載:https://blog.csdn.net/s_156/article/details/124059367

浙公網(wǎng)安備 33010602011771號