Web Development
VERIFIED FIXES

Nginx returns 502 for long-lived GraphQL subscription requests only when upstream keepalive is enabled

matte_dev
1mo ago
Environment

Environment

NGINX (1.22) reverse proxy in front of Node.js GraphQL server (express + websockets for subscriptions). upstream configured with keepalive 16 and proxy_http_version 1.1. Subscriptions use HTTP upgrade to websocket.

Symptoms

Symptoms

Subscription requests succeed briefly then NGINX returns 502 Bad Gateway during the handshake or shortly after; short HTTP queries and mutations are unaffected. Logs show occasional "upstream prematurely closed connection" entries when keepalive is set.

Observations

Observations

Node.js logs show the websocket upgrade reaches the app in successful cases. When NGINX keepalive enabled, upstream connections are reused and some connections close unexpectedly; disabling keepalive removes 502s but increases CPU and connection churn. proxy_read_timeout currently low (60s).

Already attempted

Already attempted

Tried disabling proxy buffering and increasing proxy_read_timeout to 300s; toggling proxy_http_version between 1.0/1.1; disabling keepalive removed the error but had performance cost. No TCP-level resets visible in kernel logs.

Question

Question

Why do long-lived websocket/GraphQL subscriptions fail only when using NGINX upstream keepalive, and what NGINX config changes can support persistent subscriptions without 502s or excessive backend connections?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
aliceadmin Answer
user
Posted 1mo ago • 0 confirmations

Start by treating subscriptions as a different traffic class: put websocket/GraphQL subscription endpoints in their own location and use an upstream without keepalive for that path. In the subscription location add: 'proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade;' (use a small map to set Connection to 'upgrade' when $http_upgrade present), turn off buffering and raise proxy_read_timeout. Restart nginx and exercise a subscription — it should stay open. This preserves keepalive benefits for normal HTTP API routes while avoiding connection reuse issues that cause 502s. Rollback is simple: restore the previous location block.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

NGINX reuses keepalive sockets for new requests while the backend expects a dedicated connection for websocket upgrade, causing the upgrade to fail. 75%
Backend closes idle keepalive sockets; NGINX hands a closed socket to client leading to 502. 55%
proxy_read_timeout too low for long-lived subscriptions causing NGINX to drop connection. 40%

Summary

502s for long-lived websocket connections with upstream keepalive usually indicate connection reuse being applied to upgrade-capable endpoints that expect dedicated long-lived sockets. NGINX may hand a kept-alive socket to a new client or reuse a socket that the app closed, causing 'upstream closed' errors during upgrade. Proper settings: ensure proxy_http_version 1.1, add 'proxy_set_header Connection "Upgrade"' and 'proxy_set_header Upgrade $http_upgrade' for websocket paths, set 'proxy_buffering off' and raise 'proxy_read_timeout' sufficiently. For upstream keepalive with websockets, use a dedicated upstream block for ws paths without keepalive or configure NGINX stream module for raw TCP if appropriate.

Suggested Fixes

Diagnostic step 1
Separate routes: create an upstream (or location) dedicated for websocket/subscription paths without 'keepalive' set, and keep keepalive for short API requests.
Diagnostic step 2
Ensure websocket proxy settings in subscription location: 'proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade;' and define 'map $http_upgrade $connection_upgrade { default upgrade; '' close; }'.
Diagnostic step 3
Set 'proxy_buffering off' and increase 'proxy_read_timeout' to match expected subscription lifetime (e.g., 3600s).

Continue troubleshooting

Matched from the title, description and tags