Spring WebSocket:握手失败,因为升级标头无效:零值

hmmo2u0o  于 2022-11-11  发布在  Spring
关注(0)|答案(4)|浏览(366)

我正在使用wss(安全的网络套接字)与Spring从后端和STOMP的javascript客户端。
有没有人知道为什么一得到:

Handshake failed due to invalid Upgrade header: null
bbmckpt7

bbmckpt71#

我遇到了同样的问题,nginx https代理到tomcat.这是因为我还没有支持的wss请求.为了支持wss请求,我使用如下配置:


# WebSocketSecure SSL Endpoint

# 

# The proxy is also an SSL endpoint for WSS and HTTPS connections.

# So the clients can use wss:// connections

# (e.g. from pages served via HTTPS) which work better with broken

# proxy servers, etc.

server {
    listen 443;

    # host name to respond to
    server_name ws.example.com;

    # your SSL configuration
    ssl on;
    ssl_certificate /etc/ssl/localcerts/ws.example.com.bundle.crt;
    ssl_certificate_key /etc/ssl/localcerts/ws.example.com.key;

    location / {
        # switch off logging
        access_log off;

        # redirect all HTTP traffic to localhost:8080
        proxy_pass http://localhost:8080;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # WebSocket support (nginx 1.4)
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
ac1kyiln

ac1kyiln2#

以下解决方案对我有效

<VirtualHost _default_:443>

    ServerName my.app
    ServerAdmin admin@my.app
    DocumentRoot /var/www/html
    SSLCertificateFile ...
    SSLCertificateKeyFile ...
    SSLCertificateChainFile ...

    ProxyPreserveHost on
    RequestHeader set X-Forwarded-Proto https
    RequestHeader set X-Forwarded-Port 443

    RewriteEngine on
    RewriteCond %{HTTP:Upgrade}^websocket$ [NC,OR]
    RewriteCond %{HTTP:Connection}^upgrade$ [NC]
    RewriteRule .* wss:/127.0.0.1:8081%{REQUEST_URI} [P,QSA,L]
    RewriteCond %{REQUEST_URI} ^/api/wsendpoint/%{var1}/%{var2}/websocket [NC,OR]

    ....

</VirtualHost>

我希望,这有助于...上面的解决方案是以下链接的一部分:https://forum.mattermost.org/t/solved-apache2-reverseproxy-with-websocket-https/437/3

carvr3hs

carvr3hs3#

这个职位似乎关闭,但我没有找到一个确切的解决方案,所以我会为未来的需要后
我用反向代理配置解决了这个问题(在我的例子中是apache httpd)
首先,您必须在httpd.conf中启用一些mods

LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule rewrite_module modules/mod_rewrite.so

然后进入您的虚拟主机配置并使用以下规则

RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule /(.*) "ws://[YOUR_URL]:[YOUR_PORT]/$1" [P,L]

显然,用真实的url和端口替换[YOUR_URL]和[YOUT_PORT],如果使用的是安全Web套接字,则用“wss”替换“ws”
就是这样!
如果您在服务器中做了正确的事情,您可以阅读以下标题

upgrade: WebSocket
connection: Upgrade
fcg9iug3

fcg9iug34#

DefaultHandshakeHandler -“由于无效的升级标头,握手失败:空”
此处的URL应包含端口号
例如:ws://或wss://{baseUrl}:8080/您的WebSocket终结点
并在服务器上使用以下命令启用代理

$ sudo a2enmod proxy
$ sudo service apache2 restart
$ sudo a2enmod proxy_wstunnel
$ sudo service apache2 restart

您可以在www.example.com上阅读本文https://github.com/spring-projects/spring-framework/issues/16978#issuecomment-453418847

相关问题