1. 程式人生 > >NGINX配置之二: nginx location proxy_pass 後面的url 加與不加/的區別.

NGINX配置之二: nginx location proxy_pass 後面的url 加與不加/的區別.

clas 網站 pat spa location htm .html 我們 轉發

這裏我們分4種情況討論

這裏我們請求的網站為:192.168.1.123:80/static/a.html

整個配置文件是

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後沒有/      轉發網站沒有/
#192.168.1.123->server name
# :80 ---------> port
#/statc ------->location
#/a.html ------>proxy_pass 

location /static{
proxy_pass  192.168.2.321:81
}
最後網址經過nginx轉向到的網址是 192.168.2.321:81/static/a.html

第二種:

location後沒有/      轉發網站有/
#192.168.1.123---->server name
# :80 ------------> port
#/statc ---------->location
#/a.html --------->proxy_pass 

location /static{
proxy_pass  192.168.2.321:81/
}
最後網址經過nginx轉向到的網址是 192.168.2.321:81/a.html

第三種:
location後有/      轉發網站沒有/
#192.168.1.123-->server name
# :80 ------------> port
#/statc/ ---------->location
#a.html --------->proxy_pass 

location /static/{
proxy_pass  192.168.2.321:81
}
最後網址經過nginx轉向到的網址是 192.168.2.321:81/static/a.html
第四種:
location後有/      轉發網站有/
#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/
}
最後網址經過nginx轉向到的網址是 192.168.2.321: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

NGINX配置之二: nginx location proxy_pass 後面的url 加與不加/的區別.