我在 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/