
This assumes you are already familiar with the basic setup of HAProxy with Cookies and Sticky Sessions. If not, check out my article FreeKB - HAProxy (Load Balance) - Load Balance with Cookies and Sticky Sessions.
Let's say you are load balancing multiple subdomains that are part of the same domain, such as foo.example.com and bar.example.com.
frontend main
bind *:80
balance roundrobin
option prefer-last-server
acl foo hdr(host) -i foo.example.com
use_backend foo if foo
acl bar hdr(host) -i bar.example.com
use_backend bar if bar
default_backend foo
backend foo
cookie sticky insert indirect nocache
server server1 10.0.0.1:11111 check cookie server1
server server2 10.0.0.2:11111 check cookie server2
backend bar
cookie sticky insert indirect nocache
server server1 10.0.0.1:22222 check cookie server1
server server2 10.0.0.2:22222 check cookie server2
And when foo.example.com is requested perhaps they hit server1.

And when bar.example.com is requested perhaps they hit server2.

And you want to instead have it so that when any subdomain is request, subsequent requests are sticky to the same backend server. One option would be to use the domain option in the cookie directive. Notice in this example that the cookie directive contains "domain .example.com".
- cookie sticky means the name of the cookie will be "sticky"
- insert is used to create the cookie
- indirect removes the cookie on each incoming request before forwarding the message to the server
- nocache sets the Cache-Control: private HTTP header so that cache servers between HAProxy and the user won’t cache the response
- domain .example.com means a single cookie will be used for all subdomains in the .example.com domain
In this example, the following log-format options were used
- %ci logs the client IP address
- %cp logs the client port
- %b is the name of the backend that was requested
- %CC cookie request (e.g. my_cookie=foo)
- %s is the server name in the backend
- %ST is the HTTP response code (e.g. 200 OK or 404 Not Found or 500 Internal Server Error)
- %r logs HTTP requests (e.g. GET /index.html HTTP/1.1)
frontend main
bind *:80
balance roundrobin
option prefer-last-server
capture cookie sticky len 32
log-format %[date]\ %ci:%cp\ %b\ %CC\ %s\ %ST\ %r
acl foo hdr(host) -i foo.example.com
use_backend foo if foo
acl bar hdr(host) -i bar.example.com
use_backend bar if bar
default_backend foo
backend foo
cookie sticky insert indirect nocache domain .example.com
server server1 10.0.0.1:11111 check cookie server1
server server2 10.0.0.2:11111 check cookie server2
backend bar
cookie sticky insert indirect nocache domain .example.com
server server1 10.0.0.1:22222 check cookie server1
server server2 10.0.0.2:22222 check cookie server2
And when going to any domain in the example.com domain there will be a single cookie. For example, when requesting foo.example.com if you hit server1 and you then went to bar.example.com, you'll remain on server1.

And the haproxy.log should show that both foo.example.com and bar.example.com were sticky to the same server.
~]$ sudo tail /var/log/haproxy.log -f
Jun 3 05:24:17 localhost haproxy[1517]: 135.111.212.50:57242 foo sticky=server1 server1 GET / HTTP/1.1
Jun 3 05:24:20 localhost haproxy[1517]: 135.111.212.50:57263 bar sticky=server1 server1 GET / HTTP/1.1
Did you find this article helpful?
If so, consider buying me a coffee over at