Spring WebSocket:握手因升级头无效而失败:null

时间:2015-07-03 17:35:56

标签: spring websocket spring-websocket

我正在使用wss(安全网络套接字)和来自后端的弹簧以及用于javascript客户端的STOMP。

有谁知道为什么得到:

Handshake failed due to invalid Upgrade header: null

4 个答案:

答案 0 :(得分:16)

我遇到了与tomcat的nginx https代理相同的问题。这是因为我不支持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";
    }
}

答案 1 :(得分:2)

以下解决方案对我有用

<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

答案 2 :(得分:0)

最后,我找到了解决方案。

我必须在tomcat中打开一个https端口来处理wss请求。

答案 3 :(得分:0)

这篇文章似乎已经关闭,但我在这里没有找到确切的解决方案,所以我会发布以供将来需要

我用反向代理配置解决了(在我的例子中是 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],如果你使用安全网络套接字,用“wss”替换“ws”

仅此而已!

如果您在服务器中做了正确的事情,您可以阅读以下标题

upgrade: WebSocket
connection: Upgrade