如果 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/

相关文章:

  • 2021-06-30
  • 2022-02-11
  • 2022-12-23
  • 2023-03-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-22
  • 2021-09-18
  • 2021-11-14
  • 2022-12-23
  • 2021-05-10
  • 2022-02-05
相关资源
相似解决方案