spring使用stomp over websocket引导:由于升级头无效,握手失败:null

jvlzgdj9  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(462)

我在spring引导应用程序中使用ws(非安全websocket)。一旦客户端尝试连接到服务器,就会出现以下错误: Handshake failed due to invalid Upgrade header: null springboot应用程序在一个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>

这个 application.properties 对于spring应用程序,如下所示:

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 .

c8ib6hqw

c8ib6hqw1#

我终于找到了错误。表中语句的顺序 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>

相关问题