使用 nginx 代理 WebSocket

WebSocket proxying with nginx

我对 Web 套接字代理的 nginx 文档感到困惑。

来自此处的文档:https://nginx.org/en/docs/http/websocket.html

A more sophisticated example in which a value of the “Connection” header field in a request to the proxied server depends on the presence of the “Upgrade” field in the client request header:

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

    server {
        ...

        location /chat/ {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }
    }

我想要类似的东西,如果预设了升级 header,我想在连接中传递升级,否则我想做相当于 proxy_set_header Connection "".

的事情

我认为文档中的示例正在做 proxy_set_header Connection close 如果没有升级 header?如果是这样,我该如何修改 map 来做我想做的事。看来我需要做 proxy_set_header Connection "" as

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

但感觉有些不对劲。

配置

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

确实如我所愿。