网关代理

image.png

应用场景

image.png
image.png
image.png
qps=300

反向代理proxy_pass

  1. proxy_pass http://xxx.com
  2. # 比如:
  3. location / {
  4. proxy_pass http://www.xiumubai.com; #注意:没有配置证书的话是不能配置https的地址
  5. # root /www/ng-test/foo;
  6. # index index.html index.htm;
  7. }

负载均衡

代理到多台服务器

  1. upstream httpd {
  2. server http://www.xiumubai.com weight=8;
  3. server http://interview.xiumubai.com weight=2 down bakcup;
  4. }
  5. location / {
  6. proxy_pass httpd;
  7. # root /www/ng-test/foo;
  8. # index index.html index.htm;
  9. }

weight-权重

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

  1. upstream httpd {
  2. server 127.0.0.1:8050 weight=10 down;
  3. server 127.0.0.1:8060 weight=1;
  4. server 127.0.0.1:8060 weight=1 backup;
  5. }
  • down:表示当前的server暂时不参与负载
  • weight:默认为1.weight越大,负载的权重就越大。
  • backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。

其他参数:

  • ip_hash 根据客户端的ip地址转发同一台服务器,可以保持回话。
  • least_conn 最少连接访问 url_hash 根据用户访问的url定向转发请求
  • fair 根据后端服务器响应时间转发请求