一、問題描述: 公司有個項目用到了websocket,在本地環(huán)境測試沒有問題,因為公司后臺websocket是微服務(wù)搭建,我們需要nginx進(jìn)行一層代理,結(jié)果出現(xiàn)如下錯誤
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通信協(xié)議,這是websoket必須要使用的協(xié)議)文章來源:http://www.zghlxwxcb.cn/news/detail-515487.html
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";(告訴nginx,當(dāng)它想要使用WebSocket時,響應(yīng)http升級請求)文章來源地址http://www.zghlxwxcb.cn/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 {
}
}
?
到了這里,關(guān)于websocket 線上環(huán)境報錯:Handshake failed due to invalid Upgrade header: null的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!