首页 > 解决方案 > 如何使用忽略客户端 IP 的 Haproxy 粘性会话?

问题描述

我使用 haproxy 作为 3 个应用程序服务器的负载均衡器,在客户端流量到达此负载均衡器之前,它将首先命中一个 WAF,该 WAF 具有多个随机分布式 IP。因此,当连接更新时,由于会话 cookie 的更改,原始会话可能会跳转到另一台服务器,我不希望这种情况发生。

WAF 供应商建议我在制作会话 cookie 时将负载均衡器的会话标识符设置为使用 X-Real-IP。或者从标识中删除 IP 组合以制作我不知道该怎么做的会话 cookie。可以给我建议吗?

下面是我的 haproxy 设置。

frontend http_frontend
bind *:80
acl url_static       path_beg       -i /static /images /javascript /stylesheets
acl url_static       path_end       -i .jpg .gif .png .css .js

use_backend static          if url_static
default_backend bk_http

frontend https_frontend
bind *:443
mode tcp
default_backend bk_https

backend static
balance     roundrobin
server      static 127.0.0.1:4331 check

backend bk_http
mode http
balance     roundrobin
stick on src table bk_https
cookie SRVNAME insert
server  web1 ip1:80 check cookie SA check
server  web2 ip2:80 check cookie SB check

backend bk_https
mode tcp
balance leastconn
stick-table type ip size 2000k expire 30m
stick on src
default-server inter 1s
cookie SRVNAME insert
server  web1 ip1:443 check cookie web1
server  web2 ip2:443 check cookie web2

标签: load-balancinghaproxysticky-session

解决方案


我认为cookie仅在http模式下可用,在TCP模式下你需要做ssl affinity

尝试这个 :

#maximum SSL session ID length is 32 bytes.
stick-table type binary len 32 size 20M expire 30m
acl clienthello req_ssl_hello_type 1
acl serverhello rep_ssl_hello_type 2

# use tcp content accepts to detects ssl client and server hello.
tcp-request inspect-delay 5s
tcp-request content accept if clienthello

# no timeout on response inspect delay by default.
tcp-response content accept if serverhello

# SSL session ID (SSLID) may be present on a client or server hello.
# Its length is coded on 1 byte at offset 43 and its value starts
# at offset 44.
# Match and learn on request if client hello.
stick on payload_lv(43,1) if clienthello

# Learn on response if server hello.
stick store-response payload_lv(43,1) if serverhello
server XXX:443 check
server YYY:443 check
option ssl-hello-chk

为了检查这一点,请通过套接字启用统计信息并通过 haproxy 在现场连接 2 个浏览器:

stats socket /var/lib/haproxy/stats


bash#> echo "show table" | socat unix-connect:/var/lib/haproxy/stats stdio
# table: BACKEND_NAME, type: binary, size:20971520, used:2

bash#>echo "show table BACKEND_NAME" | socat unix-connect:/var/lib/haproxy/stats stdio
# table: BACKEND_NAME, type: binary, size:20971520, used:2
0x7f9ca0f24314:
key=8A3DF855010388A4DD94F71E0FEAF7A54A7032EA56D477D20F59B4F28CEF183B use=0
exp=1264499 server_id=1
0x7f9ca0f245c4:
key=C7EA05BA85730EAF725035EFB3C4F7537FCCCFD0469FB45A4A2DE85308ECF1C7 use=0
exp=1696667 server_id=2

警告,如果您启用了 nbproc,则进程的关联性受到限制。


推荐阅读