一、引言
在nginx中配置proxy_pass時,proxy_pass后面的路徑最后面加“/”和不加“/”會有所區別。加“/”時,nginx不會代理location部分,不加“/”時,nginx會同時代理location部分。下面通過實驗來證明。
二、實驗
實驗環境簡要說明:
node1為httpd服務器(172.16.47.101)
node2為客戶端(172.16.47.102)
node3為nginx代理服務器,自身不對外提供web服務(172.16.47.103)
以下四個例子都是通過http://172.16.47.103/admin/index.html這個地址來訪問。
node1網站目錄結構如下:每個目錄下面的index.html頁面的內容都是該頁面相對于/var/www/的路徑。
[root@node1 /var/www/html]# tree . ├── abc │ ├── admin │ │ └── index.html │ └── index.html ├── abcindex.html ├── admin │ └── index.html └── index.html 3 directories, 5 files [root@node1 /var/www/html]#
1.第一種情況:
location /admin/ { proxy_pass http://172.16.47.101/; }
會被代理到http://172.16.47.103/index.html,訪問結果如下
2.第二種情況:(注意,相對于第一種情況,路徑后面少了一個“/”)
location /admin/ { proxy_pass http://172.16.47.101; }
會被代理到http://172.16.47.103/admin/index.html,訪問結果如下
3.第三種情況:
location /admin/ { proxy_pass http://172.16.47.101/abc/; }
會被代理到http://172.16.47.103/abc/index.html,訪問結果如下
3.第四種情況:(注意,相對于第三種情況,路徑后面少了一個“/”)
location /admin/ { proxy_pass http://172.16.47.101/abc; }
會被代理到http://172.16.47.103/abcindex.html,訪問結果如下
以上,proxy_pass后面的路徑最后面加“/”和不加“/”在寫法上,差別很小,很多人沒寫時會沒留意到,但帶來的結果卻大不一樣,在使用中千萬要留心。
原創文章,作者:Lurker,如若轉載,請注明出處:http://www.www58058.com/66487