Search code examples
javaspringtcpwebsockethaproxy

WebSocket not working with HAProxy in tcp mode


Consider this HAProxy configuration here:

global
    chroot /var/lib/haproxy
    user haproxy
    group haproxy

defaults
    timeout connect 10s
    timeout client 50s
    timeout server 50s

frontend fe_https_tomcat
    mode tcp
    bind *:443 ssl crt /path/cert.pem alpn h2,http/1.1
    default_backend be_tomcat

backend be_tomcat
    mode tcp
    server localhost localhost:8081 check

The issue I have is that WebSocket do not seem to get through. My guess was that in tcp mode everything would pass through. Looks like it doesn't ... :-)

The server responds with an error 403 when the WebSocket connection is getting established.

Note that with the following http-mode setup, the WebSocket just works:

frontend fe_http_8080
    mode http
    bind *:8080
    default_backend be_tomcat_8080

backend be_tomcat_8080
    mode http
    server localhost localhost:8081 check

Note that I need tcp-mode to have http/2 working.


Solution

  • The issue was not related to HAProxy at the end, but to the WebSocket setup in Spring. This fixed it:

    -registry.addHandler(webSocketHandler, "/ws");
    +registry.addHandler(webSocketHandler, "/ws").setAllowedOrigins("*");