源地址 :
https://www.zifangsky.cn/917.html
一 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
每次更新apk文件,只需要上傳新的apk文件到192.168.1.31服務器,然后再更新對外的下載地址為http://test.com/h5/appdownload/newName.apk即可,並不需要更改nginx的任何配置