Handshake failed due to invalid Upgrade header: null 解决方案

  微信小程序webSocket使用,本地开发环境没问题,QA环境报错:

org.springframework.web.socket.server.support.DefaultHandshakeHandler : Handshake failed due to invalid Upgrade header: null

  前端报错:

failed: Error during WebSocket handshake: Unexpected response code: 400

  经过比较后发现,QA环境使用nginx反向代理服务器忽略了headers中的:Upgrade:websocket。

  解决办法:在  Nginx  ,location 中添加以下红色代码:

  proxy_set_header Upgrade $http_upgrade;

  proxy_set_header Connection  "upgrade";
 1 server {
 2         listen 80;
 3         server_name localhost;
 4  
 5  
 6         location / {
 7                 proxy_set_header X-Real-IP $remote_addr;
 8                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 9                 proxy_set_header Host $http_host;
10                 proxy_set_header X-NginX-Proxy true;
11  
12                 # prevents 502 bad gateway error
13                 proxy_buffers 8 32k;
14                 proxy_buffer_size 64k;
15  
16                 proxy_pass http://127.0.0.1:8080;
17                 proxy_redirect off;
18  
19                 # enables WS support
20                 proxy_http_version 1.1;
21                 proxy_set_header Upgrade $http_upgrade;
22                 proxy_set_header Connection  "upgrade";
23         }
24 }

  使得请求中包含Upgrade。

posted @ 2018-04-17 14:19  ngulc  阅读(7702)  评论(0编辑  收藏  举报