Environment
Nginx 1.22 reverse proxy on Ubuntu 22.04, upstream is Node.js app running on same host on 127.0.0.1:3000 managed by PM2. Nginx proxy_pass configured with keepalive 32 and proxy_http_version 1.1.
Symptoms
Intermittent 502 responses in access logs; Nginx error log shows 'connect() failed (111: Connection refused) while connecting to upstream' occasionally and 'upstream sent invalid header' in other cases. Node logs show occasional restarts during spikes.
Observations
systemd and PM2 show Node process exit/restart around times of increased traffic. netstat shows many TIME_WAIT/TCP_CLOSE states. Max open file descriptors system-wide seems high but per-process limit for Node may be low.
Already attempted
Increased Nginx proxy_read_timeout and keepalive settings, restarted Nginx, verified that Node's max listeners not exceeded, and temporarily increased PM2 restart delay. Problem recurs under load testing.
Diagnostic steps
- Correlate Nginx error timestamps with Node/PM2 logs and system dmesg: identify if Node crashed near 502 times and capture stack traces or stderr logs.
- What the result means: If Node crashed at the same times as 502s, fix the crash cause (OOM, uncaught exception). If Node remained alive, continue to network-level checks.
- Monitor socket states: ss -tn state time-wait '( sport = :3000 )' and ss -s to see counts during load. Also check system ephemeral port range and tcp_tw_recycle/timeouts.
- What the result means: If TIME_WAIT count is extremely high and ephemeral ports depleted, new TCP connections may fail transiently. Adjust client-side reuse, increase ephemeral range, or use keepalive pooling to reduce churn.
- Check Node's open file descriptor limit: sudo cat /proc/$(pgrep -f node)/limits and increase PM2/uLimit if low; then repeat load test.
- What the result means: If Node's Max open files is low and the server hits it, the process will fail to accept connections. Raising limit then retesting should reduce or eliminate 502s if FD exhaustion was root cause.
- Temporarily switch proxy_pass to a unix domain socket and update Node to listen on that socket; test under load to see if 502s persist.
- What the result means: If errors disappear with unix socket, issue is likely TCP socket churn or ephemeral-port related. If errors persist, problem likely with Node process or application-level handling.
Confirmed fixes
No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.
Question
What is causing intermittent 502s: upstream refusing connections, Nginx keepalive socket reuse issues, or Node process capacity limits? How to diagnose precisely and mitigate without major config churn?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Web Development39% match
NGINX returns 504 Gateway Timeout for long-running uploads through a Node backend — how to diagnose?
Open1 answer - Web Development38% match
Nginx returns 504 Gateway Timeout only for backend requests over ~45s to my Node API
Open1 answer - Web Development38% match
NGINX returns 502 only for long-running Node.js API endpoints while short requests succeed
Open0 answers - Web Development35% match
NGINX reverse proxy returns 413 for Transfer-Encoding: chunked uploads to Next.js API while direct Node accepts them
Open0 answers - Networking32% match
CORS preflight (OPTIONS) passes on localhost but fails when routed through Nginx reverse proxy — why?
Open1 answer