Handshake failed due to invalid Upgrade header: null 解决方案以及连接60s,信息不交互,连接断开
1. 问题背景:因为后端用了nginx代理,所以websocket连接的过程中报错:Handshake failed due to invalid Upgrade header: null
2. 解决办法:在nginx中添加以下代码:
启用支持websocket连接
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 3600s; // 超时设置
说明:
proxy_http_version: 1.1;
(告诉nginx使用HTTP/1.1通信协议,这是websocket必须要使用的协议)
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
(告诉nginx,当它想要使用Websocket时,响应http升级请求)
proxy_read_timeout 3600s;
( 超时设置, 如果不设置的话,默认60s,60s内没有信息交互,将自动断开
)
代码截图: