AWS ALB 502, 503 and 504 errors: what each one means and how to fix it
An Application Load Balancer returns its own 5xx errors when targets misbehave. How to tell ALB errors from app errors, and the usual fixes for 502, 503 and 504.
· 7 min read · By the Spot Downtime team
When a site behind an AWS Application Load Balancer (ALB) returns a 502, 503 or 504, the first question is who generated it: your application, or the load balancer itself. The fixes are completely different. This guide shows how to tell them apart in a few minutes, then walks through the usual causes of each error.
For the generic meaning of these codes on any stack, see 502, 503 and 504 errors. This post is specific to the ALB.
First: did the ALB or your app send it?
CloudWatch separates the two. Under the AWS/ApplicationELB namespace:
HTTPCode_ELB_5XX_Count, and the per-codeHTTPCode_ELB_502_Count,HTTPCode_ELB_503_CountandHTTPCode_ELB_504_Count: errors generated by the load balancer.HTTPCode_Target_5XX_Count: 5xx responses your targets generated and the ALB passed through.
If the ELB counts are climbing, the load balancer could not get a valid answer from a target. If the Target count is climbing, your application is returning errors itself and the ALB is only relaying them: debug the app, not the network.
Access logs give the same answer per request. Each line has both elb_status_code and target_status_code. A 502 with a target_status_code of - means the ALB never got a usable response from a target. Access logs are off by default; enable them in the load balancer attributes and point them at an S3 bucket.
502 Bad Gateway
The ALB reached a target but got something it couldn't use: a connection closed unexpectedly, a malformed response, or an invalid header. Common causes:
The target closes idle connections before the ALB does
This is the classic intermittent 502. The ALB reuses connections to targets. Its idle timeout defaults to 60 seconds. If your server closes a keep-alive connection sooner, the ALB can send a request down a connection that is just being closed and gets a reset. The rule: the target's keep-alive timeout must be longer than the ALB's idle timeout.
// Node.js: keep connections open longer than the ALB's 60s idle timeout
const server = app.listen(3000);
server.keepAliveTimeout = 65 * 1000;
server.headersTimeout = 66 * 1000; // must be larger than keepAliveTimeout# nginx behind an ALB
keepalive_timeout 65;Gunicorn's default keep-alive is 2 seconds, which causes exactly this. Set --keep-alive 65 (and use threaded or async workers, because sync workers don't keep connections alive at all).
The application crashes or resets the connection
A process killed by the out-of-memory killer, a worker restart during a deploy, or a crash mid-request all look like a closed connection to the ALB. Correlate the 502 timestamps with container restarts and with out-of-memory kills. Draining connections during deploys (a deregistration delay long enough for in-flight requests) removes deploy-time 502s.
The wrong protocol or a TLS mismatch
If the target group protocol is HTTPS but the app listens on plain HTTP (or the reverse), the ALB gets garbage and returns 502. Check that the target group's protocol and port match what the process actually serves. With HTTPS to targets, the ALB does not validate the certificate, but the target must still complete a TLS handshake it supports.
An invalid response
A response with a malformed header, or a body longer than the declared Content-Length, also yields a 502. Lambda targets return 502 when the function's response doesn't have the shape the ALB expects (a statusCode integer, and body as a string) or when the function throws.
503 Service Unavailable
For an ALB, a 503 almost always means it has no healthy target to send the request to. Check these in order:
- Target group health. In the EC2 console, open the target group and look at the Targets tab. If every target is
unhealthy, the ALB has nowhere to send traffic. (When all targets in a group are unhealthy, the ALB may route to them anyway in some configurations, so you can see 502 or 504 instead of 503. Zero registered targets gives a 503.) - No registered targets. An ECS service scaled to zero, an Auto Scaling group with no instances, or a deploy that deregistered the old targets before the new ones passed health checks.
- No listener rule matches and there is no default action that forwards. The default action should forward to a target group or return a fixed response you chose.
When targets are unhealthy, the reason is on the same tab. The common ones:
Health checks failed with these codes: [404]: the health check path doesn't exist. Fix the path or widen the success codes (for example200-299).Request timed out: the security group on the targets doesn't allow the ALB's security group on the health check port, or the app is too slow to answer within the timeout.Connection refusedstyle failures: nothing is listening on the port, usually after a crash or a wrong port in the target group.
Don't make the health check do too much
504 Gateway Timeout
The ALB couldn't connect to the target, or the target didn't answer before the idle timeout. Two situations:
Connection timeout to the target
The ALB waits up to 10 seconds to establish the connection. A timeout here is nearly always network configuration: a security group or network ACL blocking the ALB's traffic to the target port, a target in a subnet the ALB can't route to, or a host that is overloaded and not accepting connections.
Response takes longer than the idle timeout
If the target accepts the request but doesn't send anything for longer than the idle timeout (60 seconds by default), the ALB gives up with a 504. Slow reports, large exports and long-running API calls hit this. Options:
- Make the work asynchronous: return 202 with a job ID and let the client poll.
- Raise the idle timeout in the load balancer attributes (
idle_timeout.timeout_seconds). Then raise the target's keep-alive timeout above it too, or you will trade 504s for 502s. - For streaming responses, send data or keep-alive bytes regularly so the connection never sits idle.
# Check and change the idle timeout with the AWS CLI
aws elbv2 describe-load-balancer-attributes --load-balancer-arn $ALB_ARN \
--query "Attributes[?Key=='idle_timeout.timeout_seconds']"
aws elbv2 modify-load-balancer-attributes --load-balancer-arn $ALB_ARN \
--attributes Key=idle_timeout.timeout_seconds,Value=120The ALB-only codes you might also see
- 460: the client closed the connection before the ALB could respond. It is mostly harmless noise from impatient clients, but a spike usually means slow responses. See 499 Client Closed Request for the same idea on nginx.
- 463: the
X-Forwarded-Forheader contained more than 30 IP addresses. - 561: the ALB's built-in authentication got an error from the identity provider.
A quick triage order
- Compare ELB 5xx and Target 5xx in CloudWatch to see who is at fault.
- 503: look at target group health first.
- 504: look at security groups and request duration against the idle timeout.
- 502: look at keep-alive settings, crashes and protocol mismatches.
- Line up the first error with a deploy, a scaling event or a restart.
Seeing the errors from outside
CloudWatch tells you the ALB is unhappy, but not what a visitor experiences. A check from outside AWS shows the real status code and whether the failure is constant or intermittent.
HTTP headers checkerSee the status code and response headers your load balancer returns from outside your network.Intermittent 502s from idle-timeout mismatches are easy to miss because most requests succeed. An uptime monitor on a real page, with checks confirmed from three regions before alerting, gives you a record of when the errors started, which makes it much quicker to match them to a deploy or scaling event.
Get new guides by email
Practical posts on monitoring, outages, error fixes and on-call, when they're published. No spam.
Keep reading
- HTTP errors · Troubleshootingnginx 502 Bad Gateway: fixing "connect() failed" to PHP-FPM and other upstreamsnginx logs connect() failed (111), (2) or (13) when it can't reach your app. What each errno means, and how to fix PHP-FPM sockets, ports, permissions and SELinux.November 13, 2026 · 7 min read
- HTTP errors · Troubleshooting407 Proxy Authentication Required: what it means and how to fix itA 407 comes from a proxy, not the website. Why it appears in browsers, curl and CI jobs, how it differs from 401, and how to send proxy credentials correctly.November 12, 2026 · 6 min read