Search code examples
springspring-bootnginxspring-websocketundertow

Spring WebSocket behind Nginx works with Tomcat but not with Undertow


I have replaced Tomcat with the Undertow to reduce memory usage on my spring WebSocket application. Everything works fine locally. However, when I put my application under an Nginx proxy, the client cannot connect to the WebSocket endpoint with the error {"code":1006, "text": "unexpected EOF"}.

Here is my config for Nginx, this works for tomcat but not for Undertow.

    location /ws/ {
        proxy_pass http://chat:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE-HOST        $remote_addr;
        proxy_set_header X-Forwarded-Proto  $scheme;
        proxy_redirect off;
        # WebSocket
        proxy_connect_timeout 4s;
        proxy_read_timeout 15m;
        proxy_send_timeout 12s;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

I also add server.forward-headers-strategy=native to my application.properties. Does anyone know if something I missed for Undertow or Nginx?


Solution

  • After searching lots of websites, I finally found the solution in here. Adding proxy_http_version 1.1; to the config can resolve the problem.