一、问题描述: 公司有个项目用到了websocket,在本地环境测试没有问题,因为公司后台websocket是微服务搭建,我们需要nginx进行一层代理,结果出现如下错误
Handshake failed due to invalid Upgrade header: null
二、问题解决 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/1.1通信协议,这是websoket必须要使用的协议)文章来源:https://www.toymoban.com/news/detail-515487.html
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";(告诉nginx,当它想要使用WebSocket时,响应http升级请求)文章来源地址https://www.toymoban.com/news/detail-515487.html
三、配置展示
server {
listen 8018 ssl ;
server_name _;
location /{
root /app/html/aligxdp/live/;
index index.html index.html;
try_files $uri $uri/ /index.html;
add_header Access-Control-Allow-Origin *;
}
location /byYtjEnv/ {
rewrite ^/byYtjEnv/(.*) /$1 break;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
# For WebSocket upgrade header
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://app-service/;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
到了这里,关于websocket 线上环境报错:Handshake failed due to invalid Upgrade header: null的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!