Web Development

NGINX returns 502 only for long-running Node.js API endpoints while short requests succeed

Environment

Environment

Nginx 1.18 reverse-proxy on Ubuntu 20.04 in front of a Node.js 16 Express app (PM2 cluster with 4 workers). Problem observed on endpoints that perform CPU-bound image processing and stream large responses (~10–100s response time).

Symptoms

Symptoms

Clients see HTTP 502 Bad Gateway after a gap of 30–90s on long requests; Nginx access log shows 502 for those URIs while short endpoints return 200. The Node.js process handling the request does not log a proper error; sometimes it logs 'write EPIPE' or the worker exits.

Observations

Observations

Nginx error.log shows 'upstream prematurely closed connection while reading response header from upstream'. PM2 reports worker restarts around the same time for high CPU tasks. Increasing proxy_read_timeout temporarily delays the 502 but it still appears for heavier tasks. No OOMKilled on host; CPU is saturated on a single core when processing images.

Already attempted

Already attempted

Increased proxy_read_timeout to 300s, enabled proxy_buffering off for the endpoint, and set keepalive to upstream with higher timeout. Reworked Node handler to stream in smaller chunks; still observed intermittent 502s. PM2 max memory and cluster settings reviewed.

Question

Question

Is this Nginx timing/buffering config, Node event-loop blocking causing upstream close, or OS-level resource limit? How to reliably prevent 502 on long-running processing endpoints without exposing DoS risk?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Node.js worker blocks event loop with synchronous CPU-bound processing, causing upstream to close or crash under load 85%
Upstream process crashes (unhandled exception) during heavy processing and closes connection; Nginx sees abrupt EOF 60%
Nginx upstream timeouts or buffer exhaustion misconfigured for streaming large responses; proxy_read_timeout or proxy_buffering causing aborts under certain timing 40%

Summary

The 502 and 'upstream prematurely closed connection' message indicate the upstream process closed the TCP connection before Nginx received a valid response header; the Node worker crashing (EPIPE and restarts) during CPU-bound work suggests the event loop is blocked or the process is being terminated. Increasing timeouts only masks the symptom. Likely causes: synchronous CPU-bound work blocking event loop (most likely), hitting PM2 or Node process crash (e.g., unhandled exception or signal), or hitting per-process file/socket limits causing abort. Nginx buffer/timeout settings can aggravate but are not root.

Suggested Fixes

Diagnostic step 1
Instrument the Node handler: replace synchronous processing with asynchronous or offload to worker pool (worker_threads or separate service). Add try/catch to log exceptions and ensure the worker does not crash silently.
Diagnostic step 2
Run a repro locally with the same payload and monitor process (strace, node --trace-uncaught) to catch crashes; check PM2 logs for reasons of restart.
Diagnostic step 3
Temporarily enable proxy_buffering off and raise proxy_read_timeout to confirm Nginx isn't dropping due to timeout; do not leave infinite timeouts in production.

Continue troubleshooting

Matched from the title, description and tags