Proxying websockets via nginx

May 29, 2022
manav@ente.io

Be me. Have an existing nginx config setup that works for HTTP, but now big guy ask me to also tunnel websocket connections through it.

In the nginx conf, in the http section, add these

http {
    ...

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

    ...
}

In the nginx conf, in the http > server > location section, add

http {
    ...

    server {
        ...

        location ... {
            ...

            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;

            ...
        }
    }
}

Poke nginx

systemctl restart nginx

Everything works!

Be me. Go back to watching TV.