简体   繁体   English

HAProxy + WebSocket断开连接

[英]HAProxy + WebSocket Disconnection

I am using HAProxy to send requests, on a subdomain, to a node.js app. 我正在使用HAProxy将子域上的请求发送到node.js应用程序。

I am unable to get WebSockets to work. 我无法让WebSockets工作。 So far I have only been able to get the client to establish a WebSocket connection but then there is a disconnection which follows very soon after. 到目前为止,我只能让客户端建立WebSocket连接,但之后很快就会断开连接。

I am on ubuntu. 我在ubuntu上。 I have been using various versions of socket.io and node-websocket-server . 我一直在使用各种版本的socket.ionode-websocket-server The client is either the latest versions of Safari or Chrome. 客户端是Safari或Chrome的最新版本。 HAProxy version is 1.4.8 HAProxy版本是1.4.8

Here is my HAProxy.cfg 这是我的HAProxy.cfg

global 
    maxconn 4096 
    pidfile /var/run/haproxy.pid 
    daemon 

defaults 
    mode http 

    maxconn 2000 

    option http-server-close
    option http-pretend-keepalive

    contimeout      5000
    clitimeout      50000
    srvtimeout      50000

frontend HTTP_PROXY
    bind *:80 

    timeout client  86400000

    #default server
    default_backend NGINX_SERVERS

    #node server
    acl host_node_sockettest hdr_beg(host) -i mysubdomain.mydomain

use_backend NODE_SOCKETTEST_SERVERS if host_node_sockettest


backend NGINX_SERVERS 
server THIS_NGINX_SERVER 127.0.0.1:8081

backend NODE_SOCKETTEST_SERVERS
timeout queue   5000
timeout server  86400000

server THIS_NODE_SERVER localhost:8180 maxconn 200 check

I've trawled the web and mailing list but can not get any of the suggested solutions to work. 我已经浏览了网络和邮件列表但无法获得任何建议的解决方案。

(ps this could be for serverfault, but there are other HAProxy question on SO, so I have chosen to post here) (ps这可能是针对serverfault,但是在SO上还有其他HAProxy问题,所以我选择在这里发帖)

Upgrade to latest version of socket.io (0.6.8 -> npm install socket.io@0.6.8 , which is patched to work with HAProxy) and download the latest version of HAProxy. 升级到socket.io的最新版本(0.6.8 - > npm install socket.io@0.6.8 ,已修补以与HAProxy配合使用)并下载最新版本的HAProxy。

Here is an example config file: 这是一个示例配置文件:

global
    maxconn     4096 # Total Max Connections. This is dependent on ulimit
    nbproc      2

defaults
    mode        http

frontend all 0.0.0.0:80
    timeout client 5000
    default_backend www_backend
    acl is_websocket hdr(Upgrade) -i WebSocket
    acl is_websocket hdr_beg(Host) -i ws

    use_backend socket_backend if is_websocket

backend www_backend
    balance roundrobin
    option forwardfor # This sets X-Forwarded-For
    timeout server 5000
    timeout connect 4000
    server server1 localhost:8081 weight 1 maxconn 1024 check
    server server2 localhost:8082 weight 1 maxconn 1024 check
    server server3 localhost:8083 weight 1 maxconn 1024 check

backend socket_backend
    balance roundrobin
    option forwardfor # This sets X-Forwarded-For
    timeout queue 5000
    timeout server 5000
    timeout connect 5000
    server server1 localhost:8081 weight 1 maxconn 1024 check
    server server2 localhost:8082 weight 1 maxconn 1024 check
    server server3 localhost:8083 weight 1 maxconn 1024 check

It's likely that your client is using WebSockets version 76. In which case you can't use "mode http" because the WebSockets handshake violates HTTP. 您的客户端可能正在使用WebSockets版本76.在这种情况下,您无法使用“mode http”,因为WebSockets握手违反了HTTP。 There seems to be ambivalence in the committee about whether the WebSockets handshake should be compatible with HTTP or not. 委员会似乎对于WebSockets握手是否应与HTTP兼容存在矛盾心理。 Anyways, the problem with the v76 handshake is that raw data is sent with the handshake (the checksum chunk). 无论如何,v76握手的问题是原始数据是通过握手(校验和块)发送的。

The relevant HAProxy discussion: http://www.mail-archive.com/haproxy@formilux.org/msg03046.html 相关的HAProxy讨论: http//www.mail-archive.com/haproxy@formilux.org/msg03046.html

From the discussion it sounds like there might be a way to default to TCP mode and fall back to HTTP for non-WebSockets connections. 从讨论中可以看出,可能存在一种默认为TCP模式的方法,并且对于非WebSockets连接可以回退到HTTP。

We are using a Netty implementation https://github.com/ibdknox/socket.io-netty and here is the HAProxy file that worked for us. 我们使用的是Netty实现https://github.com/ibdknox/socket.io-netty ,这里是适用于我们的HAProxy文件。 The trick to get it not to fall back to XHR-Polling but use Websockets is putting HAProxy into TCP mode. 让它不回退到XHR-Polling但使用Websockets的诀窍是将HAProxy置于TCP模式。 HAProxy config: HAProxy配置:

global
    daemon
    maxconn 32000

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

listen http-in
    bind *:80
    server server1 1.1.1.1:8000 check
    server server2 1.1.1.1:8000 check

listen socketio-in
    mode tcp
    bind *:8080
    balance source
    timeout queue 5000
    timeout server 86400000
    timeout connect 86400000
    server server1 1.1.1.1:8080 check
    server server2  1.1.1.1:8080 check

Where 1.1.1.1 is your IPs 1.1.1.1是你的IP

Try using Socket.io instead of node-websockets-server, it's an abstraction layer with fallbacks to many different methods of instant communication between browser and server. 尝试使用Socket.io而不是node-websockets-server,它是一个抽象层,可以回退到浏览器和服务器之间的许多不同的即时通信方法。

Whilst it's true WebSockets violate HTTP 1.0, they do not violate HTTP 1.1, so you should be able to proxy them with any server capable of proxying HTTP 1.1 虽然它是真正的WebSockets违反HTTP 1.0,但它们不违反HTTP 1.1,因此您应该能够使用任何能够代理HTTP 1.1的服务器代理它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM