【问题标题】:Spring Boot using Stomp over WebSocket: Handshake failed due to invalid Upgrade header: nullSpring Boot 使用 Stomp over WebSocket:由于升级标头无效,握手失败:null
【发布时间】:2021-03-11 01:58:24
【问题描述】:

我在 Spring Boot 应用程序中使用 ws(非安全 WebSocket)。一旦客户端尝试连接到服务器,就会出现以下错误:

Handshake failed due to invalid Upgrade header: null

Spring Boot 应用程序在 Apache 代理后面的 Ubuntu 服务器上运行。 Apache配置如下:

<VirtualHost *:80>
    DocumentRoot /var/www/myapp/public/
    ServerAdmin webmaster@localhost
    ServerName  app.myapp.biz

    RewriteEngine       On
    RewriteCond         %{HTTP:Upgrade}^websocket$ [NC,OR]
    RewriteCond         %{HTTP:Connection}^upgrade$ [NC]
    RewriteRule         .* "ws:/127.0.0.1:8096/$1" [P,QSA,L]

    ProxyPreserveHost   On
    ProxyRequests       Off
    ProxyPass           / http://127.0.0.1:8096/
    ProxyPassReverse    / http://127.0.0.1:8096/
    RequestHeader       set X-Forwarded-Proto http
    RequestHeader       set X-Forwarded-Port 80

    SetEnv mongo_username           aUser
    SetEnv mongo_password           aPassword
</VirtualHost>

Spring 应用的application.properties 如下:

debug=false
server.port=8096
server.address=127.0.0.1

server.forward-headers-strategy=native
server.tomcat.use-relative-redirects=true
server.tomcat.remoteip.protocol-header=x-forwarded-proto
server.tomcat.remoteip.remote-ip-header=x-forwarded-for
server.tomcat.remoteip.port-header=x-forwarded-port

在我的开发人员机器上运行应用程序时,一切正常。一旦部署在服务器上,就会发生上述错误。我用谷歌搜索了很多,但找不到特定于这个问题的东西。我希望有人可以提供帮助。

信息:尝试连接的前端应用是使用 ng2-stompjs 的 Angular 应用。

【问题讨论】:

    标签: java angular spring apache spring-websocket


    【解决方案1】:

    我终于设法找到了错误。 apache.conf 中语句的顺序 很重要!工作的 conf 文件如下所示:

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName  app.myapp.biz
    
        ProxyPreserveHost   On
        ProxyRequests       Off
        ProxyPass           / http://127.0.0.1:8096/
        ProxyPassReverse    / http://127.0.0.1:8096/
    
        RewriteEngine       On
        RewriteCond         %{HTTP:Upgrade} websocket [NC]
        RewriteCond         %{HTTP:Connection} upgrade [NC]
        RewriteRule         ^/?(.*) "ws://127.0.0.1:8096/$1" [P,L]
    
        SetEnv mongo_username           aUser
        SetEnv mongo_password           aPassword
    </VirtualHost>
    

    【讨论】:

      猜你喜欢
      • 2015-09-21
      • 2021-11-11
      • 2015-12-13
      • 2014-12-14
      • 2018-11-26
      • 2022-08-09
      • 2016-06-28
      • 2014-10-18
      • 2015-03-16
      相关资源
      最近更新 更多