nginx反向代理websocket相关配置
问题:
今天开发项目的时候遇到了一个问题,在vue里配置websocket的url, 后端提示:
websocket: the client is not using the websocket protocol: 'upgrade' token not found in 'Connection' header
仔细检查了一番,发现原来是我做了nginx反向代理,但是又没有设置正确的websocket反向代理,设置的只是http的,所以就需要专为它配个了。
打开 nginx的配置文件
首先要作如下修改
http {
...
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
...
}
然后 再加:
server {
listen 80;
server domian.com;
location /ws/ {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Connection $connection_upgrade;
proxy_pass http://127.0.0.1:8086/;
}
}
最重要的是以下两句:
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
然后重启 nginx 即可生效