HAProxy + WebSocket 断开连接

发布于 2024-10-05 18:55:52 字数 1127 浏览 1 评论 0原文

我正在使用 HAProxy 在子域上将请求发送到 node.js 应用程序。

我无法让 WebSockets 工作。到目前为止,我只能让客户端建立 WebSocket 连接,但很快就会断开连接。

我在 ubuntu 上。我一直在使用 socket.ionode-websocket-server 的各种版本。客户端是最新版本的 Safari 或 Chrome。 HAProxy 版本是 1.4.8

这是我的 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

我已经浏览了网络和邮件列表,但无法获得任何建议的解决方案。

(ps这可能是服务器故障,但是还有其他HAProxy问题,所以我选择在这里发布)

I am using HAProxy to send requests, on a subdomain, to a node.js app.

I am unable to get WebSockets to work. 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.

I am on ubuntu. I have been using various versions of socket.io and node-websocket-server. The client is either the latest versions of Safari or Chrome. HAProxy version is 1.4.8

Here is my 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.

(p.s. this could be for serverfault, but there are other HAProxy question on S.O, so I have chosen to post here)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

∞觅青森が 2024-10-12 18:55:53

我们正在使用 Netty 实现 https://github.com/ibdknox/socket.io-netty 这是为我们工作的 HAProxy 文件。让它不回退到 XHR-Polling 而是使用 Websockets 的技巧是将 HAProxy 置于 TCP 模式。 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

其中 1.1.1.1 是您的 IP

We are using a Netty implementation https://github.com/ibdknox/socket.io-netty and here is the HAProxy file that worked for us. The trick to get it not to fall back to XHR-Polling but use Websockets is putting HAProxy into TCP mode. HAProxy config:

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

陌若浮生 2024-10-12 18:55:53

尝试使用 Socket.io 而不是 node-websockets-server,它是一个抽象层,可以支持浏览器和服务器之间即时通信的许多不同方法。

虽然 WebSocket 确实违反了 HTTP 1.0,但它们并不违反 HTTP 1.1,因此您应该能够使用任何能够代理 HTTP 1.1 的服务器来代理它们

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.

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

小霸王臭丫头 2024-10-12 18:55:52

升级到最新版本的socket.io (0.6.8 -> npm install [ email protected],已修补以与 HAProxy 一起使用)
并下载最新版本的 HAProxy。

这是一个示例配置文件:

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

Upgrade to latest version of socket.io (0.6.8 -> npm install [email protected], which is patched to work with HAProxy)
and download the latest version of 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
悸初 2024-10-12 18:55:52

您的客户端可能正在使用 WebSockets 版本 76。在这种情况下,您不能使用“模式 http”,因为 WebSockets 握手违反了 HTTP。对于 WebSockets 握手是否应该与 HTTP 兼容,委员会似乎存在矛盾的态度。无论如何,v76 握手的问题在于原始数据是与握手(校验和块)一起发送的。

相关的 HAProxy 讨论:http://www.mail-archive.com/ [email protected]/msg03046.html

从讨论来看,听起来可能有一种方法可以默认为 TCP 模式并退出对于非 WebSockets 连接,返回到 HTTP。

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. There seems to be ambivalence in the committee about whether the WebSockets handshake should be compatible with HTTP or not. Anyways, the problem with the v76 handshake is that raw data is sent with the handshake (the checksum chunk).

The relevant HAProxy discussion: http://www.mail-archive.com/[email protected]/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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文