haproxy 解决集群session共享问题
Haproxy二种方法保持客户端session一致
1、用户IP 识别
haroxy 将用户IP经过hash计算后 指定到固定的真实服务器上(类似于nginx 的IP hash 指令)
配置指令:balance source
配置文件中backend的相关配置
backend test-proxy-srv
mode http
option redispatch
option abortonclose
#把负载均衡算法设置成按源地址请求的算法,同一个ip只会访问同一个后端真实服务器
balance source
cookie SERVERID
option httpchk HEAD /index.html
server web1 10.0.0.6:80 cookie server1 weight 4 check inter 2000 rise 2 fall 3
server web2 10.0.0.7:8000 cookie server2 weight 6 check inter 2000 rise 2 fall 3
2、cookie 识别
haproxy 将WEB服务端发送给客户端的cookie中插入(或添加加前缀)haproxy定义的后端的服务器COOKIE ID。
配置指令例举: cookie SESSION_COOKIE insert indirect nocache
用firebug可以观察到用户的请求头的cookie里 有类似
" Cookie 9ai9=0bc588656ca05ecf7588c65f9be214f5; SESSION_COOKIE=server1" SESSION_COOKIE=server1就是haproxy添加的内容.
配置文件中backend的相关配置、
backend test-proxy-srv
mode http
option redispatch
option abortonclose
#负载均衡算法可以是任何算法
balance roundrobin
#cookie这里需要改成插入到字段到请求头中
cookie SESSION_COOKIE insert indirect nocache
option httpchk HEAD /index.html
server web1 10.0.0.6:80 cookie server1 weight 4 check inter 2000 rise 2 fall 3
server web2 10.0.0.7:8000 cookie server2 weight 6 check inter 2000 rise 2 fall 3
客户端浏览器访问,在响应头中就会携带这个cookie
本文来自博客园,作者:厚礼蝎,转载请注明原文链接:https://www.cnblogs.com/guangdelw/p/17143445.html