An ALB returns sporadic `502`s, and access logs show `elb_status_code=502` with `target_status_code=-`. Load is low.
IntermediateTroubleshootingLoad balancers and reverse proxies
Answer
Likely causes → The target closed an idle keep-alive connection at the same moment the ALB sent a new request on it. This happens when the backend's keep-alive timeout is shorter than the ALB idle timeout (60 s default), e.g. Node.js keepAliveTimeout 5 s, gunicorn --keep-alive 2.
Diagnose →
aws elbv2 describe-load-balancer-attributes --load-balancer-arn <arn> \
--query "Attributes[?Key=='idle_timeout.timeout_seconds']"
# check access logs: target_processing_time = -1 and 502 on low-traffic periods
Fix → Make the backend keep-alive longer than the ALB's idle timeout:
server.keepAliveTimeout = 65000;
server.headersTimeout = 66000;
For nginx targets: keepalive_timeout 75s;.
Prevent → Rule of thumb: each inner hop's idle timeout must exceed the outer hop's.
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]`.Beginnernginx 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.IntermediateAn NLB shows all targets healthy, but clients from the internet time out connecting through it.Intermediate