A backend behind nginx moved to a new IP (DNS updated), but nginx keeps sending traffic to the old IP until reload.
AdvancedTroubleshootingLoad balancers and reverse proxies
Answer
Likely causes → When proxy_pass uses a literal hostname (or an upstream with hostnames in open-source nginx), nginx resolves it once at startup/reload and caches the IPs forever, ignoring TTL. Common with ELB hostnames, whose IPs change.
Diagnose →
dig +short backend.internal
sudo ss -tnp | grep nginx | awk '{print $5}' | sort | uniq -c # still connecting to old IP
Fix → Force runtime resolution by using a variable plus a resolver:
resolver 10.0.0.2 valid=30s ipv6=off;
location / {
set $backend "http://backend.internal:8080";
proxy_pass $backend;
}
nginx 1.27.3+ also supports server backend.internal resolve; inside an upstream block with a zone.
Prevent → Never proxy to ELB/cloud hostnames without dynamic resolution; reload nginx in deploy pipelines when upstreams change.
More on Load balancers and reverse proxies
nginx returns `502 Bad Gateway` and the error log says `connect() failed (111: Connection refused) while connecting to upstream`.Beginnernginx returns `504 Gateway Time-out` with `upstream timed out (110: Connection timed out) while reading response header from upstream`.BeginnerAn ALB target group shows all targets `unhealthy` with `Health checks failed with these codes: [404]`.BeginnerAn ALB returns sporadic `502`s, and access logs show `elb_status_code=502` with `target_status_code=-`. Load is low.Intermediatenginx logs `upstream prematurely closed connection while reading response header from upstream`.Intermediatenginx error log shows `no live upstreams while connecting to upstream` and every request gets 502.Intermediate