轉發:nginx的proxy_pass路徑轉發規則淺析(末尾/問題)

一 location匹配路徑末尾沒有 /

此時proxy_pass後面的路徑必須拼接location的路徑:

1

2

3

4

5

6

7

8

location /sta

{

   proxy_redirect off;

   proxy_set_header        Host $host;

   proxy_set_header        X-Real-IP $remote_addr;

   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_pass http://192.168.1.31/sta;

}

  • 外面訪問:http://192.168.1.30/sta/sta1.html
  • 相當於訪問:http://192.168.1.31/sta/sta1.html

注:這裏也可以寫成:“proxy_pass http://192.168.1.31/sta/;”。當然,不推薦使用上面這種寫法

二 location匹配路徑末尾有 /

此時proxy_pass後面的路徑需要分爲以下四種情況討論:

(1)proxy_pass後面的路徑只有域名且最後沒有 /:

1

2

3

4

5

6

7

8

location /sta/

{

   proxy_redirect off;

   proxy_set_header        Host $host;

   proxy_set_header        X-Real-IP $remote_addr;

   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_pass http://192.168.1.31;

}

  • 外面訪問:http://192.168.1.30/sta/sta1.html
  • 相當於訪問:http://192.168.1.31/sta/sta1.html

(2)proxy_pass後面的路徑只有域名同時最後有 /:

1

2

3

4

5

6

7

8

location /sta/

{

   proxy_redirect off;

   proxy_set_header        Host $host;

   proxy_set_header        X-Real-IP $remote_addr;

   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_pass http://192.168.1.31/;

}

  • 外面訪問:http://192.168.1.30/sta/sta1.html
  • 相當於訪問:http://192.168.1.31/sta1.html

(3)proxy_pass後面的路徑還有其他路徑但是最後沒有 /:

1

2

3

4

5

6

7

8

location /sta/

{

   proxy_redirect off;

   proxy_set_header        Host $host;

   proxy_set_header        X-Real-IP $remote_addr;

   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_pass http://192.168.1.31/abc;

}

  • 外面訪問:http://192.168.1.30/sta/sta1.html
  • 相當於訪問:http://192.168.1.31/abcsta1.html

(4)proxy_pass後面的路徑還有其他路徑同時最後有 /:

1

2

3

4

5

6

7

8

location /sta/

{

   proxy_redirect off;

   proxy_set_header        Host $host;

   proxy_set_header        X-Real-IP $remote_addr;

   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_pass http://192.168.1.31/abc/;

}

  • 外面訪問:http://192.168.1.30/sta/sta1.html
  • 相當於訪問:http://192.168.1.31/abc/sta1.html

附:在nginx上面配置APK文件下載路徑:

1

2

3

4

5

6

7

8

9

location ^~ /h5/appdownload/

{

      proxy_redirect off;

      proxy_set_header        Host $host;

      proxy_set_header        X-Real-IP $remote_addr;

      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_pass http://192.168.1.31/;

      proxy_set_header   Cookie $http_cookie;

}

  • 外面訪問:http://test.com/h5/appdownload/Demo_1.0.0.apk
  • 相當於訪問:http://192.168.1.31/Demo_1.0.0.apk
發佈了101 篇原創文章 · 獲贊 16 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章