配置HAProxy Session亲缘性的三种方式
haproxy负载均衡保持客户端和服务器Session亲缘性的三种方式:
1 用户IP 识别
haproxy 将用户IP通过hash计算后 指定到固定的真实服务器上(相似于nginx 的IP hash 指令)
配置指令 balance source
2 cookie 识别
haproxy 将WEB服务端发送给客户端的cookie中插入(或添加前缀)haproxy定义的后端的服务器COOKIE ID。
配置指令例举 cookie SESSION_COOKIE insert indirect nocache
用firebug能够观察到用户的请求头的cookie里 有相似" Cookie jsessionid=0bc588656ca05ecf7588c65f9be214f5; SESSION_COOKIE=app1" SESSION_COOKIE=app1就是haproxy添加的内容
3 session 识别
haproxy 将后端服务器产生的session和后端服务器标识存在haproxy中的一张表里。客户端请求时先查询这张表。
配置指令例举 appsession JSESSIONID len 64 timeout 5h request-learn
配置举例:
#vi /usr/local/haproxy/haproxy.cfg
backend COOKIE_srv
mode http
cookie SESSION_COOKIE insert indirect nocache
server REALsrv_70 184.82.239.70:80 cookie 11 check inter 1500 rise 3 fall 3 weight 1
server REALsrv_120 220.162.237.120:80 cookie 12 check inter 1500 rise 3 fall 3 weight 1
backend SOURCE_srv
mode http
balance source
server REALsrv_70 184.82.239.70:80 cookie 11 check inter 1500 rise 3 fall 3 weight 1
server REALsrv_120 220.162.237.120:80 cookie 12 check inter 1500 rise 3 fall 3 weight 1
backend APPSESSION_srv
mode http
appsession JSESSIONID len 64 timeout 5h request-learn
server REALsrv_70 184.82.239.70:80 cookie 11 check inter 1500 rise 3 fall 3 weight 1
server REALsrv_120 220.162.237.120:80 cookie 12 check inter 1500 rise 3 fall 3 weight 1
#启动haproxy
/usr/local/haproxy/haproxy -f
#查看是否启动
[zhangy@BlackGhost haproxy]$ ps -ef|grep haproxy
4859 ? 00:00:00 haproxy
4860 ? 00:00:00 haproxy
HAProxy监听的端口是1080,代理192.168.18.2:10000,
127.0.0.1:10000
统计监听的是8888端口 http://localhost:8888/haproxy-stats