← All posts

How to enable Web Socket on Nginx

WebSockets need a little extra help to get through Nginx. By default the proxy won’t upgrade the connection, so your socket never connects. Here’s the small config change that fixes it.

Add the HTTP and header config to nginx.conf

Drop this into your nginx.conf:

location /live {
    proxy_pass http://127.0.0.1:4200
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

Restart Nginx

Then restart Nginx to pick up the change:

$ sudo systemctl restart nginx

Reference: https://nginx.org/en/docs/http/websocket.html