HAProxy Sticky Sessions Node.js iOS Socket.io

时间:2014-05-27 09:56:10

标签: ios node.js socket.io haproxy

我正在尝试使用HAProxy实现粘性会话。

我有一个HAProxy实例,它路由到两个不同的Node.js服务器,每个服务器都运行socket.io。我使用iOS应用程序(https://github.com/pkyeck/socket.IO-objc)连接到这些套接字服务器(通过HAProxy服务器)。

与使用Web浏览器不同,粘性会话不起作用,就像客户端没有正确处理cookie一样,因此HAProxy服务器只是将请求路由到任何地方。下面你可以看到我的HAProxy配置(我已经删除了IP地址):

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

这导致socket.io握手出现问题,因为初始握手路由到server1然后来自客户端的后续心跳转到server2。这导致server2拒绝客户端,因为就服务器2而言,套接字会话ID是无效的,而实际上来自客户端的所有请求都应该转到同一服务器。

1 个答案:

答案 0 :(得分:1)

通过以下方式更新haproxy配置文件/etc/haproxy/haproxy.cfg:

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
相关问题