Nginx -- proxy_pass配置

1、proxy_pass

  做用域: locationhtml

  • 不影響瀏覽器地址欄的url
  • 設置被代理server的協議和地址
  • 協議能夠爲http或https
  • 地址能夠爲域名或IP

2、配置規則

  2.1 測試環境

  測試機: 172.16.200.160  my.yemao.comapi

        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.htmlurl

  實際上是訪問到了 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/;
    }
}
相關文章
相關標籤/搜索