HAProxy支持修改或插入Cookie,以提供会话持久性,并使用cookie
参数。通过将服务器添加到一个名为COOKIENAME
cookie的名称
cookie COOKIENAME prefix
这个例子将修改现有的cookie:
在任一后端或听的部分,添加以下。您的客户将看到类似server1~someotherdata
的东西,但您的应用只会看到someotherdata
部分。所以你可以在现有的cookies上使用它。此外,此方法允许您只在该cookie存在时执行会话persitence,这意味着您仍然可以在您的站点的静态部分周围平衡人员,并且只在需要时强制实施粘性,但会将该cookie名称添加到会话中。
而且命名的服务器,让你的服务器产品线如下所示:
server server1 1.2.3.4 cookie server1
更多细节是在HAProxy config guide,它也像您可以使用appsession
配置参数为好。
完成此操作后,您可以从列表中选择自己的平衡方法,但我倾向于使用roundrobin
,但在考虑粘性会话后,leastconn
可能会为您提供更好的平衡。
从文档更使它更容易找到参考部分:
cookie <name> [ rewrite | insert | prefix ] [ indirect ] [ nocache ]
[ postonly ] [ preserve ] [ domain <domain> ]*
[ maxidle <idle> ] [ maxlife <life> ]
Enable cookie-based persistence in a backend.
May be used in sections : defaults | frontend | listen | backend
yes | no | yes | yes