Amaze UI Logo

码动指尖



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 即可生效

 nginx

作者  :  奕弈

喵喵喵,你在心上



评论


About ME

about me

奕弈

为了最初的心,努力奋斗,从不懈怠的学习。

我不想成为一个庸俗的人。十年百年后,当我们死去,质疑我们的人同样死去,后人看到的是裹足不前、原地打转的你,还是一直奔跑、走到远方的我?

Contact ME