0
votes

I am trying to implement sticky sessions with HAProxy.

I have a HAProxy instance that routes to two different Node.js servers, each running socket.io. I am connecting to these socket servers (via the HAProxy server) using an iOS app (https://github.com/pkyeck/socket.IO-objc).

Unlike when using a web browser, the sticky sessions do not work, it is like the client is not handling the cookie properly and so the HAProxy server just routes the request to wherever it likes. Below you can see my HAProxy config (I have removed the IP addresses):

listen webfarm xxx.xxx.xxx.xxx:80
   mode http
   stats enable
   stats uri /haproxy?stats
   stats realm Haproxy\ Statistics
   stats auth haproxy:stats
   balance roundrobin
   #replace XXXX with customer site name
   cookie SERVERID insert indirect nocache
   option httpclose
   option forwardfor
   #replace with web node private ip
   server web01 yyy.yyy.yyy.yyy:8000 cookie server1 weight 1 maxconn 1024 check
   #replace with web node private ip
   server web02 zzz.zzz.zzz.zzz:8000 cookie server2 weight 1 maxconn 1024 check

This is causing a problem with the socket.io handshake, because the initial handshake routes to server1 then subsequent heartbeats from the client go to server2. This causes server2 to reject the client because the socket session ID is invalid as far as server 2 is concerned, when really all requests from the client should go to the same server.

1

1 Answers

1
votes

Update the haproxy config file /etc/haproxy/haproxy.cfg by the following:

global
        daemon
        maxconn 256

defaults
        mode http
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms

frontend http-in
        bind *:80
        default_backend servers
        option forwardfor

backend servers
        cookie SRVNAME insert
        balance leastconn
        option forwardfor
        server node1 127.0.0.1:3001 cookie node1 check
        server node2 127.0.0.1:3002 cookie node2 check
        server node3 127.0.0.1:3003 cookie node3 check
        server node4 127.0.0.1:3004 cookie node4 check
        server node5 127.0.0.1:3005 cookie node5 check