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/;
    }
}
相关文章
相关标签/搜索