小编Ant*_*bbs的帖子

Nginx代理websocket:升级为websocket后是否需要关闭与后端的连接?

基于我在 nginx 网站 https://www.nginx.com/blog/websocket-nginx/上读到的内容

他们给出的示例将关闭与后端的所有连接。这并不是我们真正想要的代理设置,强制在每个新客户端上重新打开与后端的连接。

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
 
    upstream websocket {
        server 192.168.100.10:8010;
    }
 
    server {
        listen 8020;
        location / {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是升级到websocket后是否需要关闭连接呢?

我们不能更改地图以使其保持与后端的“保持活动”连接吗?(对于所有非 websocket 请求)

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
 
    upstream websocket {
        server 192.168.100.10:8010;
    }
 
    server {
        listen 8020;
        location / {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade; …
Run Code Online (Sandbox Code Playgroud)

nginx websocket

5
推荐指数
1
解决办法
3281
查看次数

标签 统计

nginx ×1

websocket ×1