Hi everyone,
We're running an on-prem microservice stack on Debian 12 (Docker Engine 26.1, kernel 6.1.0-21-amd64) behind an Nginx reverse proxy to handle front-desk reception kiosks and badge printers.
As part of deploying an on-prem visitor management system across our main lobby terminals, each kiosk maintains a persistent WebSocket connection (wss://) to a Node.js event gateway backed by Redis Pub/Sub for instant badge-print triggers and host notifications.
Expected Behavior
During morning check-in bursts (around 40–60 concurrent badge scans per minute), WebSocket connections should stay persistent and route events cleanly through Nginx to the upstream Node container.
Actual Behavior
Under burst load, kiosks randomly disconnect with code 1006 and fail to receive print-job ACKs for ~15–20 seconds before reconnecting.
Relevant snippet from /var/log/nginx/error.log:
text
2026/11/25 08:42:11 [error] 29#29: *1482 upstream prematurely closed connection while reading response header from upstream, client: 10.20.4.31, server: vms-gw.internal, request: "GET /ws/kiosk HTTP/1.1", upstream: "http://172.19.0.4:4000/ws/kiosk"
2026/11/25 08:42:11 [warn] 29#29: *1485 an upstream response is buffered to a temporary file /var/cache/nginx/proxy_temp/4/00/0000000004 while reading upstream
Current nginx.conf location block:
nginx
location /ws/ {
proxy_pass http://ws_gateway:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_read_timeout 60s;
}
What We've Tried
- Increased proxy_read_timeout from 60s to 300s (disconnects still happen right when Redis Pub/Sub fans out payloads).
- Verified container memory/CPU limits (docker stats shows < 35% utilization on both nginx and ws_gateway).
- Checked sysctl net.core.somaxconn on the host (currently 4096).
Has anyone hit upstream prematurely closed connection on Nginx WebSocket upgrades when proxy_buffering isn't explicitly disabled, or could Redis Pub/Sub backpressure in the Node worker be starving the event loop long enough for Nginx to drop the socket?
Top comments (1)
The key line in your log is "upstream prematurely closed connection". That means the Node container closed the socket, not Nginx. So the proxy settings are probably not the root cause, and raising proxy_read_timeout was not going to fix it. I would check these in order:
Is the Node process blocked or crashing? Check
docker logs ws_gatewayanddocker inspectfor restarts or OOM kills around 08:42:11. Also log event loop lag (perf_hooks.monitorEventLoopDelay). If a fan-out handler does heavy JSON or sync work, thewsping/pong can miss its window and the server terminates the client.Redis side. A Pub/Sub subscriber that can't keep up gets disconnected by Redis when it exceeds
client-output-buffer-limit pubsub(default 32mb hard, 8mb for 60s). Runredis-cli INFO clientsand check the Redis log for "scheduled to be closed ASAP for overcoming of output buffer limits". If your gateway reconnects to Redis and drops its sockets in the process, you would see exactly this pattern.Backpressure on the sockets. With
ws, checksocket.bufferedAmountbefore sending and skip or queue for slow kiosks, instead of writing to every socket without limit.On the Nginx side, a few changes are still worth making:
Add server-side ping frames every 20-30 seconds as well, so idle kiosks are not dropped by NAT or other middleboxes.
If you can share the ws_gateway logs and
redis-cli INFO clientsoutput from during a burst, it should be easy to tell which of these it is.