在進行nginx反向代理配置的時候,location和proxy_pass中的斜線會形成各類困擾,有時候多一個或少一個斜線,就會形成徹底不一樣的結果,因此特意將location和proxy_pass後有無斜線的狀況進行了排列組合,進行了一次完整的測試,找出原理,以提升姿式水平~nginx
兩臺nginx服務器api
nginx A: 192.168.1.48服務器
nginx B: 192.168.1.56測試
nginx A配置:url
location /foo/ { proxy_pass http://192.168.1.56/; } nginx B收到的請求:/api
nginx A配置:代理
location /foo/ { proxy_pass http://192.168.1.56/; } nginx B收到的請求://api
nginx A配置:日誌
location /foo/ { proxy_pass http://192.168.1.56/; } nginx B收到的請求:/foo/api
nginx A配置:code
location /foo/ { proxy_pass http://192.168.1.56/; } nginx B收到的請求:/foo/api
nginx A配置:ip
location /foo/ { proxy_pass http://192.168.1.56/bar/; } nginx B收到的請求:/bar/api
nginx A配置:字符串
location /foo { proxy_pass http://192.168.1.56/bar/; } nginx B收到的請求:/bar//api
nginx A配置:
location /foo/ { proxy_pass http://192.168.1.56/bar; } nginx B收到的請求:/barapi
nginx A配置:
location /foo { proxy_pass http://192.168.1.56/bar; } nginx B收到的請求:/bar/api
看到這裏是否是都暈了呢,實際上是有規律的
如今把這些案例按表格排列起來,結果表示nginx B收到的請求
表一
案例 | location | proxy_pass | 結果 |
---|---|---|---|
1 | /foo/ | http://192.168.1.48/ | /api |
2 | /foo | http://192.168.1.48/ | //api |
3 | /foo/ | http://192.168.1.48 | /foo/api |
4 | /foo | http://192.168.1.48 | /foo/api |
表二
案例 | location | proxy_pass | 結果 |
---|---|---|---|
5 | /foo/ | http://192.168.1.48/bar/ | /bar/api |
6 | /foo | http://192.168.1.48/bar/ | /bar//api |
7 | /foo/ | http://192.168.1.48/bar | /barapi |
8 | /foo | http://192.168.1.48/bar | /bar/api |
原請求路徑:本文中統一爲 "/foo/api"
location: 上面表格中的location列
proxy_pass:上面表格中的proxy_pass列
新請求路徑:nginx將原請求路徑處理事後的字符串
重點對proxy_pass
進行分析,能夠分爲3種形式
而後按照ip:port後是否接了字符串歸爲2類,"/"也是字符串,所以1歸爲一類,二、3歸爲一類,下面對這兩類狀況進行說明
當 proxy_pass 的 ip:port 後未接字符串的時候,nginx 會將原請求路徑原封不動地轉交給下一站 nginx,如案例3和4
當 proxy_pass 的 ip:port 後接了字符串的時候,nginx 會將 location 從 原請求路徑 中剔除,再將剩餘的字符串拼接到 proxy_pass 後生成 新請求路徑,而後將 新請求路徑 轉交給下一站nginx(上面一種狀況實際上和這個是同樣的,只不過剔除的字符串是空串~~)
舉個最讓人疑惑的例子:案例7。proxy_pass 的 ip:port 後接了字符串 "/bar",所以將 location:"/foo/" 從 原請求路徑:"/foo/api" 中剔除,變爲"api",再將"api"拼接到proxy_pass:http://192.168.1.48/bar 後生成了新請求url:"http://192.168.1.48/barapi",所以下一站的nginx收到的請求就是 "/barapi"。
案例6:proxy_pass 的 ip:port 後接了字符串 "/bar/",所以將 location:"/foo" 從 原請求路徑 "/foo/api" 中剔除,變爲 "/api",再將 "/api" 拼接到proxy_pass:http://192.168.1.48/bar/ 後生成了 新請求路徑:"http://192.168.1.48/bar//api",所以下一站的nginx收到的請求就是 /bar//api。
其它的案例均可以以此類推,如今終於搞明白了,不再用一頭霧水。