配置 nginx 以支持 websocket

如果 websocket 中间有反向代理服务器,是不能直接通信的,需要进行配置,以下配置均在 nginx.conf 文件中进行。

首先在 http 节点下新增:

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
}

然后在相应的 server 节点下新增:

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    server {
        listen       8020;
        server_name  localhost;

        location / {
            proxy_pass http://localhost:8010;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
        }
    }
}

大概是这样,然后就可以了~

参考文档:https://www.nginx.com/blog/websocket-nginx/

posted @ 2021-01-22 18:49  不是豆豆  阅读(895)  评论(0编辑  收藏  举报
友情链接:迷途