1 用戶IP 識別 nginx
haroxy 將用戶IP通過hash計算後 指定到固定的真實服務器上(相似於nginx 的IP hash 指令)redis
配置指令 balance source後端
2 cookie 識別 服務器
haproxy 將WEB服務端發送給客戶端的cookie中插入(或添加加前綴)haproxy定義的後端的服務器COOKIE ID。cookie
配置指令例舉 cookie SESSION_COOKIE insert indirect nocachesession
用firebug能夠觀察到用戶的請求頭的cookie裏 有相似" Cookie jsessionid=0bc588656ca05ecf7588c65f9be214f5; SESSION_COOKIE=app1" SESSION_COOKIE=app1就是haproxy添加的內容app
3 session 識別 frontend
haproxy 將後端服務器產生的session和後端服務器標識存在haproxy中的一張表裏。客戶端請求時先查詢這張表。jsp
配置指令例舉 appsession JSESSIONID len 64 timeout 5h request-learnide
#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