1

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 10.10.10.1:81 weight 1 maxconn 1024 check
server socket2 10.10.10.2:81 weight 1 maxconn 1024 check
server socket3 10.10.10.3:81 weight 1 maxconn 1024 check
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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