Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using HAproxy to load balance between more web server. Those webservers are using PHP and sessions to keep one's session open.

Now, I think that when a user refreshes the page, it is sent to one of those servers but, if he's sent to a different server, he will of course loose his session, no?

The question is mainly: how can make it so that a client connects to the same server?

Here is my configuration for the backend.

backend social_backend
mode http
option httplog
option http-server-close
option forceclose
no option httpclose
balance roundrobin
option forwardfor
timeout queue 5000
timeout server 86400000
timeout connect 86400000
timeout check 1s

server socket1 weight 1 maxconn 1024 check
server socket2 weight 1 maxconn 1024 check
server socket3 weight 1 maxconn 1024 check
share|improve this question
up vote 2 down vote accepted

What you're looking for is "sticky sessions", and you can use appsession parameter in HAProxy to enable that:

appsession <cookie> len <length> timeout <holdtime> 
           [request-learn] [prefix] [mode <path-parameters|query-string>]

When an application cookie is defined in a backend, HAProxy will check when the server sets such a cookie, and will store its value in a table, and associate it with the server's identifier. Up to characters from the value will be retained. On each connection, haproxy will look for this cookie both in the "Cookie:" headers, and as a URL parameter (depending on the mode used). If a known value is found, the client will be directed to the server associated with this value. Otherwise, the load balancing algorithm is applied. Cookies are automatically removed from memory when they have been unused for a duration longer than .

The definition of an application cookie is limited to one per backend.

Example : appsession JSESSIONID len 52 timeout 3h

Refer to HAProxy documentation for more details.

share|improve this answer
thank you. do i need to put that inside the backend? – Enrico Tuttobene Apr 21 '12 at 18:12
Yes, you can just add it to the end of your backend social_backend configuration file along with "cookie MySessionId prefix" line. – BluesRockAddict Apr 21 '12 at 18:15
great. thank you! – Enrico Tuttobene Apr 21 '12 at 18:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.