Servers

PM2-managed Node.js behind NGINX exhausts ephemeral ports (TIME_WAIT spike) during traffic bursts causing intermittent 502s

alexnord
4w ago
Environment

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

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

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

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

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?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
antonbuilds Answer
user
Posted 4w ago • 0 confirmations

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.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

Default ephemeral port range too small for peak concurrent outbound connections leading to port exhaustion (TIME_WAIT sockets occupy ports). 85%
Application creates many short-lived outbound connections (no keepalive/reuse) from Node.js, causing local side to close and accumulate TIME_WAIT. 80%
Kernel TIME-WAIT timeout (tcp_fin_timeout/tcp_tw_timeout) set high, delaying port reuse and increasing TIME_WAIT count. 60%

Summary

Symptoms point to ephemeral local port exhaustion on backend hosts used for outgoing upstream connections or for backend worker processes establishing many short connections to other services. Large number of TIME_WAIT sockets indicates many TCP connections are being closed actively by local side; default local ephemeral range plus TIME_WAIT timeout leads to port reuse exhaustion. Contributing factors include short-lived connection patterns, low ephemeral port range, long tcp_fin_timeout/tcp_tw_reuse/tcp_tw_recycle kernel settings, and possible lack of upstream connection reuse in Node.js HTTP clients. NGINX 502s occur when proxy cannot allocate local ephemeral port to connect upstream or when upstream actively refuses due to resource limits.

Suggested Fixes

Diagnostic step 1
Measure outgoing connection count and TIME_WAIT with ss -s and ss -tanp during a controlled load test to reproduce and quantify limits.
Diagnostic step 2
Enable and configure keepalive reuse in Node.js HTTP clients (set agent.keepAlive = true and increase maxSockets) and in NGINX upstream (keepalive directive) to reduce new TCP handshakes.
Diagnostic step 3
Increase ephemeral port range: sudo sysctl -w net.ipv4.ip_local_port_range='20000 65000' and persist in /etc/sysctl.conf; monitor for improvement.

Continue troubleshooting

Matched from the title, description and tags