0
点赞
收藏
分享

微信扫一扫

Nginx作为WebSocket代理(Handshake failed due to invalid Upgrade header: null)

624c95384278 2022-03-10 阅读 71

使用WebSocket在测试环境中一切正常,在生产环境中后端出现如下错误:

因为WebSocket 是在客户端和服务器之间保持长连接的方式运行,与普通的HTTP短链接的方式不同,所以在NGINX需要通过使用Upgrade和Connection将连接从 HTTP 升级到 WebSocket 的 HTTP。
  
https://www.nginx.com/blog/websocket-nginx/

解决办法:在Nginx 的location中添加以下代码:

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection  "upgrade";
location /wsapp/ {
    proxy_pass http://wsbackend;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    
	# enables WS support
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
}
举报

相关推荐

0 条评论