java - Spring Boot 在 WebSocket 上使用 Stomp : Handshake failed due to invalid Upgrade header: null

标签 java angular spring apache spring-websocket

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

由于升级 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 应用.

最佳答案

我终于找到了错误。 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>

关于java - Spring Boot 在 WebSocket 上使用 Stomp : Handshake failed due to invalid Upgrade header: null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65051666/

相关文章:

angular - 错误 TypeError : this. 验证器不是 FormArray 函数

java - 如何将Eureka客户端注册到带有负载均衡器的Eureka服务器集群?

java - 扩展 Eclipse 调试功能

java - 设置 DrJava 通过 Friedman/Felleisen 工作 "A Little Java"

java - 在设备开机或重新启动时启动 Android 服务

css - 15949 处的无效字体值警告 :4. 忽略:Angular4 产品构建

angular - 'CKEditor 4.x is missing (http ://ckeditor. com/)' 使用 ng2-ckeditor 时的警告,angular 4

java - 在 Spring JPA 中避免 SQL 注入(inject)

java - SpringBootTest @EnabledWebSocket 忽略

java - websphere + SSLv3 SSLContext 不可用