Nginx -- proxy_pass配置

一、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/;
    }
}

  

 

posted @ 2017-10-11 16:17  Bigberg  阅读(25333)  评论(0编辑  收藏  举报