PM2-managed Node.js behind NGINX exhausts ephemeral ports (TIME_WAIT spike) during traffic bursts causing intermittent 502s
Environment
Three-node Ubuntu 22.04 app cluster running Node.js 18 with PM2 process manager; fronted by an NGINX 1.22 reverse proxy (keepalive on). Each app instance runs on a container with host networking; sysctl net.ipv4.ip_local_port_range is default (32768-60999). Observed under sudden traffic bursts from a load test hitting same backend endpoints repeatedly.
Symptoms
During traffic bursts (5–10k concurrent short-lived connections) NGINX returns sporadic 502 Bad Gateway. NGINX error log shows 'connect() failed (111: Connection refused) while connecting to upstream'. App processes healthy; new connections intermittently fail for ~30–120s and recover.
Observations
On affected app hosts: netstat/ss shows thousands of sockets in TIME_WAIT and many ephemeral ports exhausted. ss -s shows high TCP: in-use sockets and TIME-WAIT. lsof shows app listening, but outgoing connect() fails with EADDRNOTAVAIL at times. pm2 status shows no crashes. No container restart during incidents.
Already attempted
Increased NGINX proxy_connect_timeout and keepalive_timeout; tuned PM2 max listeners; restarted NGINX (temporary improvement). Added keepalive connections to upstream but left connection reuse default. Tried increasing ulimit -n to 65536 (no change in TIME_WAIT accumulation behavior).
Question
What is the safest way to stop ephemeral port exhaustion under bursty short- lived upstream connections without changing client behavior? Which kernel/tcp settings and NGINX/Node changes reduce TIME_WAIT pressure while keeping correctness?
Verified community troubleshooting
First check whether your Node app or NGINX is creating new upstream TCP connections for every request. Enable keep-alive: in Node.js use an Agent with keepAlive:true and larger maxSockets, and add an NGINX upstream keepalive directive. While testing, widen the ephemeral port range (sysctl net.ipv4.ip_local_port_range='20000 65000') and monitor ss -s to confirm TIME_WAIT drops. If still high, cautiously set net.ipv4.tcp_tw_reuse=1 and reduce tcp_fin_timeout to 30–60s, but avoid tcp_tw_recycle on NATted hosts. Verify by running the same load and confirming 502s stop; rollback sysctl changes if they worsen connections.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Servers43% match
Node.js + PM2 server exhausts ephemeral ports (TIME_WAIT) during traffic bursts causing upstream connection failures and 502s
Open0 answers - Servers29% match
Nginx returns 502 Bad Gateway when proxying to local Node.js upstream intermittently
Open0 answers - Web Development25% match
NGINX returns 502 only for long-running Node.js API endpoints while short requests succeed
Open0 answers - Web Development24% match
NGINX reverse proxy returns 413 for Transfer-Encoding: chunked uploads to Next.js API while direct Node accepts them
Open0 answers - Web Development23% match
NGINX returns 504 Gateway Timeout for long-running uploads through a Node backend — how to diagnose?
Open1 answer