Search DevFox

Search tools and pages.

Nginx WebSocket Proxy Generator

Generate an nginx config for proxying WebSocket connections with Upgrade and Connection headers and long read timeouts

Loading tool...

What is Nginx WebSocket Proxy Generator

Written by Giorgos Kostas. Last reviewed:

Nginx WebSocket Proxy Generator emits the canonical websocket-aware proxy block — including the `map $http_upgrade $connection_upgrade` directive that Nginx's docs require.

It tunes the parts that trip up websocket setups: `proxy_http_version 1.1`, `proxy_buffering off`, and a configurable `proxy_read_timeout` for long-idle sockets.

Why use it

  • Get the `map` block right the first time — websockets break silently when `Connection` isn't set to the upgraded value.
  • Stop sockets from being killed by the default 60s `proxy_read_timeout` — bump it from a single field.
  • Disable buffering so server-pushed messages reach clients immediately.
  • Optionally pair the websocket location with a fallback `/` proxy to the same app.

Features

  • `map $http_upgrade $connection_upgrade` block included
  • `proxy_http_version 1.1` + Upgrade/Connection forwarding
  • `proxy_buffering off` for instant message delivery
  • Configurable idle timeout for long-lived sockets
  • Optional fallback `/` proxy_pass for REST + WS on one domain

How to use Nginx WebSocket Proxy Generator

  1. Set the path. Pick the websocket location (`/ws`, `/socket.io/`, etc.).
  2. Point at upstream. Set the websocket upstream URL (`http://...`, even for `wss`).
  3. Set idle timeout. Raise to 3600s (or longer) for chat / pub-sub.
  4. Deploy. Copy + drop into Nginx, `nginx -t`, reload.

Example (before/after)

Form input

path     = /ws
upstream = http://127.0.0.1:8080
idle     = 3600s

Generated config

map $http_upgrade $connection_upgrade {
  default upgrade;
  ''      close;
}
server {
  listen 80;
  server_name app.example.com;
  location /ws {
    proxy_pass http://127.0.0.1:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header Host $host;
    proxy_buffering off;
    proxy_read_timeout 3600s;
    proxy_send_timeout 3600s;
  }
}

Common errors

WebSocket disconnects after ~60 seconds of silence

Default `proxy_read_timeout` kills idle sockets.

Fix: Bump 'Idle timeout' in this generator (e.g. 3600s) and consider keep-alive pings from the client.

Server messages arrive in batches

Nginx is buffering responses before forwarding to the client.

Fix: Generator emits `proxy_buffering off;` — keep it on. Don't override it elsewhere.

Client sees 400 / 426 Upgrade Required

The `Upgrade`/`Connection` headers aren't being forwarded.

Fix: Confirm the generated `map` block is included and that nothing rewrites those headers downstream.

FAQ

Why is the `map` directive needed?

Nginx forwards `Connection` literally by default. WebSockets need `Connection: upgrade`, but only when the client requested an upgrade. The `map` translates `$http_upgrade` to either `upgrade` or `close`, which the proxy block then forwards.

Where should the `map` block go?

At the http {} level. The generator emits it at the top of the snippet — paste it into a file under `/etc/nginx/conf.d/` so it loads inside the http context.

How does this combine with HTTPS / wss://?

The websocket location works the same on HTTPS — paste the location block into the HTTPS server generated by the SSL Config Generator. Clients connect with `wss://`.

Does it support both REST and WebSocket on the same domain?

Yes — set the optional fallback `proxy_pass` and the generator emits a `location /` block alongside the websocket location.

Should buffering be off everywhere or just here?

Just here. `proxy_buffering off` is appropriate for streaming/long-poll/websocket responses; keep it on for normal REST traffic.

What are realistic idle timeouts?

1 hour (3600s) is a reasonable upper bound for chat/realtime apps. If you go higher, also configure TCP keep-alive on the upstream so dead connections are reaped.