Search code examples
rubywebsocketeventmachineem-websocket

Websockets, em-websocket & HAProxy 502 error


I'm trying to run a webserver and websocket server on port 80. To do this I'm using HAProxy to route the connections using this config:

global
    maxconn     4096 # Total Max Connections. This is dependent on ulimit
    nbproc      1
    ulimit-n        65536

defaults
    mode      http

frontend all 0.0.0.0:80
    timeout client 86400000
    acl is_websocket hdr_beg(host) -i live
    acl is_websocket hdr(Upgrade) -i WebSocket

    default_backend www_backend

    use_backend production_socket_backend if is_websocket

backend www_backend

    balance roundrobin
    option forwardfor # This sets X-Forwarded-For
    timeout server 30000
    timeout connect 4000
    server appserver 127.0.0.1:81 weight 1 maxconn 1024

backend production_socket_backend
    balance roundrobin
    option forwardfor # This sets X-Forwarded-For
    timeout queue 5000
    timeout server 86400000
    timeout connect 86400000
    server appserver 127.0.0.1:8083 weight 1 maxconn 1024

I'm testing using Google Chrome. On some machines I connect fine, on others it will give me a 502 error and em-websocket logs this error:

#<EventMachine::WebSocket::HandshakeError: Connection and Upgrade headers required>

If I stop running the proxy and run the web socket server on port 80 it works fine which to me indicates the problem is with the proxy. I've read somewhere that HAProxy shouldn't be run in http mode when dealing with websockets as the upgrade packet isn't valid HTTP, could this have something to do with the problems I'm seeing?


Solution

  • I'm using port 443 for now. In the future I'll have web sockets running on a separate server to get around this problem.