一、proxy_pass
作用域: location
- 不影響瀏覽器地址欄的url
- 設置被代理server的協議和地址
- 協議可以為http或https
- 地址可以為域名或IP
二、配置規則
2.1 測試環境
測試機: 172.16.200.160 my.yemao.com
172.16.200.143 test.yemao.com
注:在proxy_pass中的代理url后加上/,代理轉發的url中就不會帶上location中匹配路徑;
如果后面沒有/,代理轉發的url中就會帶上location中的匹配路徑
2.2 url 后帶 / (則不會加上location中的匹配路徑)
我們訪問 http://my.yemao.com/proxy/index.html,其實是訪問到了 http://test.yemao.com/index.html
2.3 url中不帶 / (則會加上location中的匹配路徑)
我們訪問 http://my.yemao.com/proxy/index.html
其實是訪問到了 http://test.yemao.com/proxy/index.html
這里將 location 中匹配的 proxy 也自動加到了 代理轉發的地址后面
2.4 代理轉發的地址后面帶目錄和/
我們訪問 http://my.yemao.com/proxy/index.html
其實是訪問到了 http://test.yemao.com/yemao/index.html
2.5 代理轉發的地址后面帶目錄沒有 /
我們訪問 http://my.yemao.com/proxy/index.html
其實是訪問到了 http://test.yemao.com/testindex.html
2.6 轉發的后台有多個服務器
upstream backend { server backend1.example.com weight=5; server backend2.example.com:8080; server unix:/tmp/backend3; server backup1.example.com:8080 backup; server backup2.example.com:8080 backup; } server { location / { proxy_pass http://backend/user/pub/api/; } }