HAProxy provides a number of methods for maintaining a record of which backend server should handle a specific connection. This is known as creating a ‘sticky’ connection (other terms for this are ‘connection persistence’ and ‘connection affinity’). HAProxy can use the source ip address, url hash, cookies, sessions (checks cookies and url parameter), headers, and more, to determine which backend server to pass the connection to.
Add one of the following lines to the HAProxy config file, in the section containing your backend servers.
Load balancing PHP sessions:
appsession PHPSESSID len 64 timeout 3h request-learn prefix
Load balancing ASP.Net sessions:
appsession ASP.NET_SessionId len 64 timeout 3h request-learn prefix
Load balancing ASP sessions:
appsession ASPSESSIONID len 64 timeout 3h request-learn prefix
Load balancing Java Server Pages sessions:
appsession JSESSIONID len 52 timeout 3h request-learn prefix
Note: You may need to modify slightly to suit your environment. I haven’t tested all of these.
For more info on sticky sessions with HAProxy, check out the HAProxy help page for the ‘appsession’ keyword:
See http://en.wikipedia.org/wiki/Session_ID for more details on session ID’s