haproxy-基于cookie的会话保持
cookie value:为当前server指定cookie值,实现基于cookie的会话黏性,相对于基于 source 地址 hash 调度算法对客户端的粒度更精准,但同时也加大了haproxy负载,目前此模式使用较少, 已经被session共享服务器代替
注意:不支持 tcp mode,使用 http mode
cookie name [ rewrite | insert | prefix ][ indirect ] [ nocache ][ postonly ] [ preserve ][ httponly ] [ secure ][ domain ]* [ maxidle <idle> ][ maxlife ] name: #cookie 的key名称,用于实现持久连接 insert: #插入新的cookie,默认不插入cookie indirect: #如果客户端已经有cookie,则不会再发送cookie信息 nocache: #当client和hapoxy之间有缓存服务器(如:CDN)时,不允许中间缓存器缓存cookie,因为这会导致很多经过同一个CDN的请求都发送到同一台后端服务器
listen zzhz bind 192.168.80.110:81 #IP地址 mode http log global
cookie ZZHZ insert nocache indirect
server web1 192.168.80.110:80 weight 1 check inter 3000 fall 2 rise 5 cookie AAA #名称最好与server的一样。 server web2 192.168.80.120:80 weight 3 check inter 3000 fall 3 rise 5 cookie BBB
测试结果:
[root@localhost7e ~]# curl -i 192.168.80.110:81
set-cookie: ZZHZ=AAA; path=/
192.168.80.110
[root@localhost7e ~]# curl -i 192.168.80.110:81 HTTP/1.1 200 OK server: nginx/1.20.1 date: Sat, 23 Jul 2022 09:11:03 GMT content-type: text/html content-length: 15 last-modified: Fri, 22 Jul 2022 08:50:22 GMT etag: "62da64ce-f" accept-ranges: bytes set-cookie: ZZHZ=BBB; path=/ cache-control: private 192.168.80.120
[root@localhost7e ~]# curl -vb ZZHZ=AAA 192.168.80.110:81
[root@localhost7e ~]# curl -vb ZZHZ=AAA 192.168.80.110:81 * About to connect() to 192.168.80.110 port 81 (#0) * Trying 192.168.80.110... * Connected to 192.168.80.110 (192.168.80.110) port 81 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.29.0 > Host: 192.168.80.110:81 > Accept: */* > Cookie: ZZHZ=AAA > < HTTP/1.1 200 OK < server: nginx/1.20.1 < date: Sat, 23 Jul 2022 09:15:36 GMT < content-type: text/html < content-length: 15 < last-modified: Fri, 22 Jul 2022 08:50:17 GMT < etag: "62da64c9-f" < accept-ranges: bytes < 192.168.80.110 * Connection #0 to host 192.168.80.110 left intact