目录
一、问题描述:
公司有个项目用到了websocket,在本地环境测试没有问题,因为公司后台websocket是微服务搭建,我们需要nginx进行一层代理,结果出现如下错误
错误1:WebSocket failed: Error during WebSocket handshake: Unexpected response code: 400
错误2:Incompatibile SockJS! Main site uses: "1.1.5", the iframe: "1.0.0"
二、问题解决
1、首先找到自己nginx的配置文件—>配置下面代码 ——> nginx重启
location /consultation-websocket/ {
proxy_pass http://127.0.0.1:8098/consultation-websocket/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
# For WebSocket upgrade header
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
2、其中最重要的是下面这三行
proxy_http_version 1.1;(告诉nginx使用HTTP

本文详细介绍了如何在Nginx中正确配置WebSocket代理,解决了因代理设置不当导致的WebSocket连接失败问题。通过修改Nginx配置文件,启用HTTP/1.1协议,并设置必要的升级头,成功实现了WebSocket服务的正常运行。
最低0.47元/天 解锁文章
1484

被折叠的 条评论
为什么被折叠?



