2019-05-20 nginx的负载均衡策略Round-Robin和ip_hash和一致性哈希

upstream rrups {
      server 127.0.0.1:9527 weight=2 max_conns=2 max_fails=2 fail_timeout=5;  #设定与上游服务器的权重,最大并发连接,在5s的时间内,失败2次连接就认为服务不可用
      server 127.0.0.1:8080;
      keepalive 32;  #与上游服务器保持连接的时长
}
server {
     server_name rrups.zhangdazhi.com;
     location / {
         proxy_pass http://rrups;
         proxy_http_version 1.1;   #对上游连接的http头部设定,因为1.0协议不支持keepalive
        proxy_set_header Connection "";
}
}
upstream iphashups {
        ip_hash;
         server 127.0.0.1:9527;
         server 127.0.0.1:8080;

}
server {
      set_real_ip_from 150.109.98.115;  #定义一个可信的ip地址
      real_ip_recursive on;
      real_ip_header X-Forwarded-For;
      server_name iphash.zhangdazhi.com;
      location /{
            proxy_pass http://iphashups;
             proxy_http_version 1.1;
            proxy_set_header Connetion "";
      }
 }
[root@hk conf.d]#curl -H 'X-Forwarded-For:192.168.1.1' iphash.zhangdazhi.com
server1
[root@hk conf.d]#curl -H 'X-Forwarded-For:192.168.1.1' iphash.zhangdazhi.com
server1
[root@hk conf.d]#curl -H 'X-Forwarded-For:192.168.1.1' iphash.zhangdazhi.com
server1
[root@hk conf.d]#curl -H 'X-Forwarded-For:192.168.1.1' iphash.zhangdazhi.com
server1
[root@hk conf.d]#curl -H 'X-Forwarded-For:192.168.2.1' iphash.zhangdazhi.com
server2
[root@hk conf.d]#curl -H 'X-Forwarded-For:192.168.2.1' iphash.zhangdazhi.com
server2
[root@hk conf.d]#curl -H 'X-Forwarded-For:192.168.2.1' iphash.zhangdazhi.com
server2
upstream iphashups {
       # ip_hash;
         hash user_$arg_username;  #基于任意关键字实现hash
         server 127.0.0.1:9527;
         server 127.0.0.1:8080;

}
server {
      set_real_ip_from 150.109.98.115;
      real_ip_recursive on;
      real_ip_header X-Forwarded-For;
      server_name iphash.zhangdazhi.com;
      location /{
            proxy_pass http://iphashups;
             proxy_http_version 1.1;
            proxy_set_header Connetion "";
      }
 }
[root@hk conf.d]#curl -H 'X-Forwarded-For:192.168.2.1' iphash.zhangdazhi.com?username=bbb
server2
[root@hk conf.d]#curl -H 'X-Forwarded-For:192.168.2.1' iphash.zhangdazhi.com?username=aaaaaaa
server1
upstream iphashups {
       # ip_hash;
         hash user_$arg_username consistent;  #一直性哈希加一个consistent参数即可
         server 127.0.0.1:9527;
         server 127.0.0.1:8080;
 
}
server {
      set_real_ip_from 150.109.98.115;
      real_ip_recursive on;
      real_ip_header X-Forwarded-For;
      server_name iphash.zhangdazhi.com;
      location /{
            proxy_pass http://iphashups;
             proxy_http_version 1.1;
            proxy_set_header Connetion "";
      }
 }
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容