【问题标题】:HAProxy Sticky Sessions Node.js iOS Socket.ioHAProxy 粘性会话 Node.js iOS Socket.io
【发布时间】:2014-07-16 04:28:10
【问题描述】:

我正在尝试使用 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 是无效的,而实际上来自客户端的所有请求都应该发送到同一台服务器。

【问题讨论】:

    标签: ios node.js socket.io haproxy


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-07
      • 2012-08-16
      • 1970-01-01
      • 2016-12-07
      • 2011-12-30
      • 2014-06-21
      • 2011-09-23
      • 2012-02-11
      相关资源
      最近更新 更多