這里我們分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