haproxy负载均衡保持客户端和服务器Session亲缘性的三种方式:
1 用户IP 识别
haroxy 将用户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
#haproxy -f haproxy.cfg -V
#haproxy.cfg
global
log 127.0.0.1 local0 info
maxconn 4096
user haproxy
group haproxy
daemon
nbproc 1
pidfile /var/run/haproxy.pid
defaults
mode http
maxconn 2000
contimeout 5000
clitimeout 30000
srvtimeout 30000
option httplog
option redispatch
option abortonclose
retries 3
listen admin_stats
bind 113.106.185.245:443
mode http
log 127.0.0.1 local0 err
stats uri /qhappy_stats
stats realm itindex.net\ Qhappy
stats auth qhappy:qhappy
stats refresh 5s
listen site_status
bind 113.106.185.245:445
mode http
log 127.0.0.1 local0 err
monitor-uri /site_status
frontend WEB_SITE
bind 0.0.0.0:8080
mode http
log global
option httplog
option httpclose
option forwardfor
acl COOKIE hdr_reg(host) -i ^(cookie.itindex.net)
acl SOURCE hdr_reg(host) -i ^(sourceip. itindex .net)
acl APPSESSION hdr_reg(host) -i ^(appsession. itindex .net)
acl NOSESSION hdr_reg(host) -i ^(nosession. itindex .net)
use_backend COOKIE_srv if COOKIE
use_backend SOURCE_srv if SOURCE
use_backend APPSESSION_srv if APPSESSION
use_backend NOSESSION_srv if NOSESSION
# default_backend ai_server
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
backend NOSESSION_srv
mode http
balance roundrobin
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 ai_server
mode http
balance roundrobin
cookie SERVERID
server REALsrv_70 184.82.239.70:80 cookie 2 check inter 1500 rise 3 fall 3 weight 1
server REALsrv_120 220.162.237.120:80 cookie 1 check inter 1500 rise 3 fall 3 weight 1
#Cookie
listen appli1-rewrite 0.0.0.0:80
mode http
option httplog
option dontlognull
option httpclose
option forwardfor
cookie SESSION_COOKIE insert indirect nocache
#balance roundrobin
server app1_1 172.20.37.249:8080 cookie app1inst1 check inter 2000 rise 2 fall 5
server app1_2 172.20.12.145:80 cookie app1inst2 check inter 2000 rise 2 fall 5
#IP
listen appli1 0.0.0.0:90
mode http
option httplog
option dontlognull
log 127.0.0.1 local3
cookie JSESSIONID rewrite
balance source
option httpchk GET /payCardSys/login.jsp
stats uri /stats
stats auth admin:admin
server app1_1 10.112.56.66:6601 cookie app1inst1 check inter 2000 rise 2 fall 5
server app1_2 10.112.56.67:6702 cookie app1inst2 check inter 2000 rise 2 fall 5