An app on an EC2 instance calls its own internal NLB, and connections time out only when the NLB picks that same instance.
AdvancedTroubleshootingLoad balancers and reverse proxies
Answer
Likely causes → Hairpinning with client IP preservation. The packet arrives at the instance with source = its own IP and destination = its own IP; the instance replies locally instead of via the NLB, the client socket sees a reply from an unexpected address, and the connection never completes.
Diagnose →
aws elbv2 describe-target-group-attributes --target-group-arn <tg> \
--query "Attributes[?Key=='preserve_client_ip.enabled']"
sudo tcpdump -ni any host <instance-ip> and port <port> # SYN from self, SYN-ACK goes direct
Roughly 1/N of calls fail, where N is the number of targets.
Fix → Disable client IP preservation on that target group (use PROXY protocol v2 if you need the client IP), use IP targets without preservation, or call the local service directly instead of through the NLB.
Prevent → Avoid services calling themselves through their own LB; document this NLB limitation in service templates.
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