An NLB shows all targets healthy, but clients from the internet time out connecting through it.
IntermediateTroubleshootingLoad balancers and reverse proxies
Answer
Likely causes → With instance targets (or IP targets with client IP preservation on), the NLB passes the client's IP through. Health checks come from the NLB's private IPs (allowed), but the target's security group doesn't allow the client CIDRs, so real traffic is dropped.
Diagnose →
aws elbv2 describe-target-group-attributes --target-group-arn <tg> \
--query "Attributes[?Key=='preserve_client_ip.enabled']"
aws ec2 describe-security-groups --group-ids <target-sg> --query 'SecurityGroups[0].IpPermissions'
# VPC Flow Logs on the target ENI will show REJECT for client IPs
Fix → Allow the client CIDRs (or 0.0.0.0/0 for public services) on the target SG for the listener port, or attach a security group to the NLB and reference it from the target SG.
Prevent → Use NLB security groups (supported since 2023) and template SG rules together with the listener.
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