A proxy runs out of ephemeral ports. How do you fix it?
AdvancedProductionLogs, Limits & High-Traffic Tuning
Answer
Symptoms: connect: Cannot assign requested address (EADDRNOTAVAIL), tens of thousands of TIME-WAIT sockets to the same upstream IP:port. Each outgoing connection needs a unique (src IP, src port, dst IP, dst port) tuple. Fixes:
- Reuse connections: HTTP keepalive to upstreams (nginx
keepalive 64;in the upstream block). - Widen
net.ipv4.ip_local_port_rangeto1024 65535. net.ipv4.tcp_tw_reuse=1(safe for outgoing connections).- Add more source IPs or upstream addresses.
Never use tcp_tw_recycle; it broke clients behind NAT and was removed in kernel 4.12.
More on Logs, Limits & High-Traffic Tuning
How does log management work on a modern Linux server?BeginnerWhy centralize logs instead of reading them on each server?BeginnerWrite a logrotate config for an application log.BeginnerWhat is the difference between `copytruncate` and `create` + postrotate signal?IntermediateLogs rotated but disk usage did not drop. Why?IntermediateHow often does logrotate run, and why does `maxsize` sometimes not trigger in time?Intermediate