HAProxy keeping implement cookie-based client session
When using ip_hash, if there are many users use the same public IP address to access the same services, because the public IP used by these users are the same, HAproxy will dispatch them to the same back-end server, which may acquired pressure caused by a single server is too large, so that other methods need to be scheduled.
HAProxy insert a layer of cookie can be achieved when the user first visit will be to see if there cookie, if not in the response message. The drive to cookie back to the client, when a user requests access again will be scheduled according to the cookie. lvs and nginx can not be achieved
cookie <value>
The current value of the specified cookie server, sticky cookie-based session to achieve
cookie NAME [rewrite|insert|prefix] [indirect] [nocache] [postonly] [preserve] [httponly] [secure] [domain <domain>] [maxidle<idle>] [maxlife<life>]
NAME: cookie名称,用于持久连接
rewrite: 重写。如果之前有cookie就会覆盖了重写,或插入到前缀上
insert: 插入
prefix: 前缀
nocache: 当client和haproxy之间有缓存存在时,不缓存cookie
Cookie-based implementation will remain
Modify the configuration file, add the option cooke
listen web
bind 172.20.27.20:80
mode http
cookie SERVER-COOKIE insert indirect nocache #插入cookie选项
option forwardfor
server web1 192.168.27.21:80 cookie web1 check inter 3000 fall 3 rise 5 #在后端服务器中拆入cookie的值
server web2 192.168.27.22:80 cookie web2 check inter 3000 fall 3 rise 5
test
Use curl to view the packet header
[root@localhost ~]# curl -I www.mylinuxops.com
HTTP/1.1 200 OK
Server: nginx/1.17.0
Date: Tue, 04 Jun 2019 11:41:23 GMT
Content-Type: text/html
Content-Length: 8
Last-Modified: Tue, 04 Jun 2019 11:29:30 GMT
ETag: "5cf6561a-8"
Accept-Ranges: bytes
Set-Cookie: SERVER-COOKIE=web2; path=/ #此为插入的cookie信息
Cache-control: private
Since the curl command can not record the cookie, use a browser to access the next
Reproduced in: https: //blog.51cto.com/11886307/2406614