Spring WebSocket在Nginx后面与Tomcat一起工作,但与Undertow不起作用。

huangapple go评论68阅读模式
英文:

Spring WebSocket behind Nginx works with Tomcat but not with Undertow

问题

我已经将Tomcat替换为Undertow,以减少我的Spring WebSocket应用程序的内存使用。在本地一切正常运行。然而,当我将我的应用程序放在Nginx代理下时,客户端无法连接到WebSocket端点,出现错误{"code":1006, "text": "unexpected EOF"}。

这是我的Nginx配置,对Tomcat有效但对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";
    }

我还在我的application.properties中添加了server.forward-headers-strategy=native。是否有人知道我是否遗漏了Undertow或Nginx的某些内容?

英文:

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?

答案1

得分: 0

在搜索了许多网站之后,我终于在这里找到了解决方案。将proxy_http_version 1.1;添加到配置中可以解决这个问题。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月22日 14:50:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定