背景
最近项目新增了一个 websocket 服务,用 nginx 做了一个简单的端口转发,然后调用的时候发现报错:
error: Unexpected server response: 426
搜索引擎走一波,找到几篇相关文章:
https://nginx.org/en/docs/http/websocket.html
https://www.nginx.com/blog/websocket-nginx
解决方式也很简单,根据第一篇文章的说明,只要增加转发响应头的配置:
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
因为 websocket 的连接建立是基于 HTTP/1.1 的,所以有必要制定 http 协议:
proxy_http_version: 1.1
当然这不是必须的,如果服务默认是 HTTP/1.1 的话
具体配置如下:
http { map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream websocket { server 192.168.100.10:8010; } se