Websocket握手与HAProxy挂起

Chr*_*isG 5 haproxy websocket sockjs

使用最新的Chrome浏览器,我正在尝试使用sockjs客户端与后端服务器进行通信haproxy.在我的本地主机(不在haproxy中间),这工作正常 - 客户端可以使用websocket协议连接,发送和接收消息.例如:

conn.onopen = function() {
    if (conn.readyState === SockJS.OPEN) {
        conn.send("hello server");
        console.log("msg sent");
    }
};
Run Code Online (Sandbox Code Playgroud)

一旦我将它部署在服务器上HAProxy,就会发生奇怪的事情,sockjs认为连接是打开的(如同在conn.readyState === SockJS.OPEN控制台日志中出现'msg sent'),但是,websocket握手只是挂起而服务器从未收到过msg.以下是我在haproxy日志中看到的内容:

Oct 23 09:08:25 localhost.localdomain haproxy[14121]: 129.xx.xxx.105:55000 [23/Oct/2012:09:08:24.459] public www/content 777/0/0/1/778 200 375 - - ---- 3/3/0/1/0 0/0 "GET /sockjs/info HTTP/1.1"
Oct 23 09:10:54 localhost.localdomain haproxy[14121]: 129.xx.xxx.105:55015 [23/Oct/2012:09:08:25.398] public www/content 0/0/0/1/149017 101 147 - - CD-- 4/4/0/0/0 0/0 "GET /sockjs/478/kyi342s8/websocket HTTP/1.1"
Run Code Online (Sandbox Code Playgroud)

请注意,第二个log msg 仅在我关闭后端服务器时出现haproxy.在关闭之前,日志中没有错误,但websocket握手没有完成,服务器没有收到任何消息.

使用Chrome的开发者工具,在"网络"标签中,我看到以下内容:

Request URL:ws://www.mysite.com/sockjs/478/kyi342s8/websocket
Request Method:GET
Status Code:101 Switching Protocols

Request Headers
Connection:Upgrade
Host:www.mysite.com
Origin:http://www.mysite.com
Sec-WebSocket-Extensions:x-webkit-deflate-frame
Sec-WebSocket-Key:TFEIKYhlqWWBZKlXzXAuWQ==
Sec-WebSocket-Version:13
Upgrade:websocket
(Key3):00:00:00:00:00:00:00:00

Response Headers
Connection:Upgrade
Sec-WebSocket-Accept:D+s3va02KH6QTso24ywcdxcfDgM=
Upgrade:websocket
(Challenge Response):00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
Run Code Online (Sandbox Code Playgroud)

websocket对象的"类型"和"时间延迟"在开发人员工具的网络选项卡下显示"待定".

最后,这是我的haproxy配置(版本1.4.22):

global
        log 127.0.0.1   local1 info
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        maxconn 4096
        chroot /usr/share/haproxy
        uid 99
        gid 99
        daemon
        #debug
        #quiet

defaults
        log             global
        mode            http
        option          httplog
        option          dontlognull
        retries         3
        option          redispatch
        maxconn         500
        timeout connect 6s

frontend public
        mode    http
        bind    *:80
        timeout client  300s
        option  http-server-close
        #option         http-pretend-keepalive
        # define ACLs
        acl host_static hdr_beg(host) -i static. data.
        acl host_www hdr_beg(host) -i www.
        acl url_static path_end .ico .txt .pdf .png .jpg .css .js .csv
        acl is_stats path_beg /haproxy/stats
        # define rules
        use_backend nginx if host_static or host_www url_static
        use_backend stats if is_stats
        default_backend www

backend nginx
        timeout server 20s
        server nginx 127.0.0.1:8484

backend stats
        stats enable
        stats uri /haproxy/stats

backend www
        timeout server 300s
        option forwardfor
        #no option httpclose
        option http-server-close
        server sockcontent 127.0.0.1:8080
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么会这样?是由于某些haproxy配置,甚至是服务器的一些常规网络设置(例如iptables)?

PS我已经尝试实现http-pretend-keepalivehaproxy,但它不工作.

Joe*_*oes 1

最有可能的是,您的网络中有透明防火墙,它会扰乱通往端口 80 的 Websocket 连接。

要验证情况是否如此:

  1. 让 haproxy 监听不同的端口并尝试它是否开始工作;
  2. 尝试在网络外部访问您的服务。

如果它开始工作,则表明您的网络出现问题。

不幸的是,在这种情况下,SockJS 不会使用后备传输,因为客户端认为它已连接,但连接并未真正建立。

可能的解决方案:让 haproxy 监听两个端口,在端口 80 上监听 Web 流量,在不同端口上监听 SockJS 流量。这保证了透明 HTTP 代理不会干扰您的 Websocket 连接。