ALB 502 Bad Gateway vs 504 Gateway Timeout: The Keep-Alive Race Behind Random 502s, Why a Lambda Timeout Is a 502, and How to Tell Which One You Have

Logeshwaran
—

On an AWS Application Load Balancer (ALB), 502 Bad Gateway means the load balancer got a broken answer from your target, or the connection to it failed: the target reset or closed the connection, sent a malformed response, or failed a TLS handshake. 504 Gateway Timeout means it waited too long: it couldn't connect to the target within 10 seconds, or the target didn't answer within the idle timeout, 60 seconds by default. Before you change anything, check whether the error came from the load balancer or from your app: the HTTPCode_ELB_5XX_Count and HTTPCode_Target_5XX_Count metrics tell you in a minute. Here's the cause behind most "random" 502s: your web server closes idle connections sooner than the load balancer expects. Node.js's built-in server closes an idle connection after about 6 seconds, Gunicorn's threaded worker after 2, and a stock Tomcat after 20, while the ALB happily keeps the same connection for 60 seconds and sometimes sends a request down it just as your server hangs up. Two more surprises: a Lambda target that times out produces a 502, not a 504, and raising the ALB idle timeout to cure 504s can create brand-new 502s. This page shows how to tell which error you have, every cause of each, and the exact setting to change in Node.js, Gunicorn, nginx and Tomcat.

Ethan, a developer, runs the booking site for his friend Jake's phone repair shop: a Node.js app on two small servers behind an Application Load Balancer. Twice a day, Jake got a 502 Bad Gateway on the bookings page. He'd press refresh and it would work. Nothing in the app's logs mentioned an error. Separately, the end-of-month sales report, which takes over a minute to build, always ended in a 504. Ethan's first instinct was to raise every timeout he could find. This page is why he didn't, and what fixed both.

⚡ Quick Answer

• 502: the target closed, reset or garbled the connection. 504: the target was too slow to connect (10 s) or to answer (idle timeout). Tell them apart.

• Random 502s, app looks fine? Set your server's keep-alive timeout above the ALB idle timeout, for example 65 s against 60 s. The keep-alive race.

• Lambda target? Timeouts, errors and bad response shapes are all 502. Lambda fixes.

• 504 on slow pages? Speed up the page or make it asynchronous before raising the idle timeout. The 504 causes.

• Whose error is it? HTTPCode_ELB_5XX_Count = load balancer; HTTPCode_Target_5XX_Count = your app. Metrics and logs.

Comparing server settings? Node.js, Gunicorn, nginx and Tomcat defaults.

New to load balancers? An ALB sits in front of your servers and passes each visitor's request to one of them, its "targets." It keeps connections to those targets open and reuses them, which is efficient and also the root of the most confusing 502s. Our guides to EC2 servers and security groups cover the pieces involved.

🧭 NEW HERE? READ THESE FIRST

New to load balancers and AWS monitoring? These five pages make the rest of this one easy:

📌 Bookmark this; the cause tables and the server settings are the parts you will come back for.

First: did the load balancer send the error, or your app?

A 502 or 504 can come from two places. Your app itself can return one, for example when it's a proxy to another service, and the ALB just passes it along. Or the ALB generates it because something went wrong between it and your target. The fixes are completely different, so check this first.

CloudWatch metricCounts
HTTPCode_ELB_502_Count, HTTPCode_ELB_504_Count502s and 504s the load balancer generated itself
HTTPCode_ELB_5XX_CountAll 5XX errors the load balancer generated, not including any from targets
HTTPCode_Target_5XX_Count5XX responses your targets sent, which the ALB forwarded
TargetConnectionErrorCountConnections the ALB couldn't establish to a target (not used for Lambda targets)
TargetResponseTimeSeconds from the request leaving the ALB until the target started its response

Pull the last day of load-balancer-generated 502s from the command line. The LoadBalancer value is the end of your load balancer's ARN, starting with app/:

aws cloudwatch get-metric-statistics \
  --namespace AWS/ApplicationELB \
  --metric-name HTTPCode_ELB_502_Count \
  --dimensions Name=LoadBalancer,Value=app/booking-alb/50dc6c495c0c9188 \
  --start-time 2026-10-12T00:00:00Z --end-time 2026-10-13T00:00:00Z \
  --period 3600 --statistics Sum

Swap the metric name for HTTPCode_Target_5XX_Count to see your app's own errors. If the ELB counts are the ones climbing, keep reading. If only the target count climbs, the problem is inside your app, and its own logs will say why.

The access log tells you per request

ALB access logs, which you turn on in the load balancer's attributes and which land in S3, record both status codes for every request. Two fields settle the question:

  • elb_status_code (the 9th field) is what the client got.
  • target_status_code (the 10th field) is what your target sent. It's - when the ALB never got a response from the target.

So 502 - means the load balancer made the 502 because your target never answered properly; 502 502 means your app sent the 502 itself. The three timing fields before them are set to -1 when the ALB couldn't send the request or got no response. With a few log files downloaded, one line lists every 502 and 504 with the target's status code, the three timings and the requested URL:

zcat *.log.gz | awk '$9 == 502 || $9 == 504 {print $9, $10, $6, $7, $8, $14}'

For whole days of logs, query them in place with Athena; our Athena guide walks through it. One limit to know: the access log's error_reason field is filled in for Lambda, authentication and AWS WAF failures, not for EC2 or container targets, so for those, the status codes and timings are your evidence.

Jake: "So first you find out who's complaining."

Ethan: "Right. If the ALB made the 502, the app logs will be clean, which is exactly what we saw. That was the first clue."

A five-minute diagnosis

Before reading every cause below, these five questions narrow it down to one or two:

  1. Who generated it? If HTTPCode_ELB_502_Count or HTTPCode_ELB_504_Count is climbing, it's the load balancer. If only HTTPCode_Target_5XX_Count climbs, it's your app.
  2. Is it a 502 or a 504? A 502 means a broken connection or answer; a 504 means waiting too long.
  3. How fast did it fail? Instantly points to a refused or reset connection; about 10 seconds to a connection that couldn't be made; exactly the idle timeout (60 seconds by default) to a target that never answered.
  4. Is it every request, or now and then? Every request means the port, a crashing app or TLS. Now and then, with a healthy app, means the keep-alive race.
  5. Is the target a Lambda function? Then the access log's error_reason names the exact problem.

Every reason an ALB returns 502 Bad Gateway

These are the causes for load-balancer-generated 502s, with what to check for each:

CauseWhat to check
The target closed the connection (TCP FIN or RST) while the ALB had a request outstandingYour server's keep-alive timeout is shorter than the ALB idle timeout. The keep-alive race
The target reset the connection (TCP RST) when the ALB tried to connectNothing is listening on the target port, or the app is restarting or crashing
An "ICMP Destination unreachable" reply when connectingWhether traffic from the load balancer's subnets can reach the targets on the target port
The response is malformed or has invalid HTTP headersA buggy proxy or app writing bad headers
Response headers larger than 32 KB in totalHuge cookies or debug headers
The deregistration delay ran out while a request was still running on a deregistered targetLong requests during deployments; raise the deregistration delay
A TLS handshake error with an HTTPS targetThe target's certificate and ciphers
A Lambda target timed out, returned an error, was throttled, or sent a response over 1 MBLambda targets

If your 502s are constant, look at the bottom rows: the port, crashes, certificates. If they're occasional and the app seems healthy, it's almost always the first row.

Random 502s: the keep-alive race

To save time, the ALB keeps connections to your targets open and reuses them for later requests. Each side has its own idea of how long an unused connection should stay open. On the ALB, that's the connection idle timeout, 60 seconds by default. On your server, it's the keep-alive timeout.

When your server's timeout is shorter, this happens: a connection sits idle for, say, 6 seconds. Your server decides it's done and closes it. At almost the same moment, a new visitor arrives, and the ALB, which still believes the connection is good for another 54 seconds, sends the request down it. The connection is already closing, the request is lost, and the visitor gets a 502. Your app never saw the request, so its logs are clean. It only happens when a connection goes quiet for just the wrong length of time, which is why it looks random and why a refresh always works.

The fix is a single rule: make your server's keep-alive timeout longer than the ALB idle timeout. Then the ALB always closes idle connections first, cleanly, and never sends a request into a connection that's about to disappear. With the default 60-second idle timeout, 65 seconds on the server is a common choice.

How long popular servers keep idle connections

Here's how long common servers actually keep an idle connection open by default, and what that means behind an ALB with the default 60-second idle timeout:

ServerDefault idle keep-aliveBehind a 60-second ALB
Node.js 22 http server (also Express, Fastify on it)Advertises 5 s, actually closes after about 6 s (a built-in 1-second buffer)502 risk
Gunicorn, threaded gthread worker2 s502 risk
Gunicorn, default sync workerDoesn't keep connections: answers Connection: closeNo race, but a new connection for every request
Tomcat with the standard server.xml20 s (keep-alive falls back to connectionTimeout, set to 20000 ms)502 risk
nginx, official Docker image65 s (keepalive_timeout 65)Safe, until you raise the ALB idle timeout above 65

Watch it happen. This connects to a server, makes one request, waits 8 seconds without closing the connection, then reuses it, exactly what the ALB does:

import http.client, sys, time

port, idle = int(sys.argv[1]), float(sys.argv[2])
c = http.client.HTTPConnection("127.0.0.1", port)
c.request("GET", "/"); r = c.getresponse(); r.read()
time.sleep(idle)
try:
    c.request("GET", "/"); r = c.getresponse(); r.read()
    print(f"reused connection after {idle:.0f}s idle -> {r.status}")
except (http.client.RemoteDisconnected, ConnectionResetError, BrokenPipeError) as e:
    print(f"reused connection after {idle:.0f}s idle -> {type(e).__name__}: {e}")

Run it as python3 reuse.py 3000 8 against a Node.js server with default settings and you get RemoteDisconnected: Remote end closed connection without response. That's the moment an ALB turns into a 502. Against the same server with the fix below, the second request returns 200.

Fix the keep-alive timeout in Node.js, Gunicorn, nginx and Tomcat

Roll the change out the same way whatever your server is:

  1. Check the ALB's current idle timeout: aws elbv2 describe-load-balancer-attributes --load-balancer-arn ARN --query "Attributes[?Key=='idle_timeout.timeout_seconds']".
  2. Set the server's keep-alive timeout a few seconds above it, using the setting for your server below.
  3. Deploy to one target first and confirm the response now carries the new Keep-Alive: timeout= value, where the server sends one.
  4. Watch HTTPCode_ELB_502_Count for a day; random 502s from the race stop within the first busy period.

Node.js (and Express)

const http = require("http");

const server = http.createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "text/plain" });
  res.end("OK\n");
});

server.keepAliveTimeout = 65000;
server.headersTimeout = 66000;
server.listen(3000);

With Express, set the same two properties on the server that app.listen() returns: const server = app.listen(3000); server.keepAliveTimeout = 65000; server.headersTimeout = 66000;. After the change, the server answers with Keep-Alive: timeout=65 and holds idle connections for about 66 seconds, safely past the ALB's 60.

Gunicorn (Python)

gunicorn app:app --worker-class gthread --threads 4 --keep-alive 75

--keep-alive is in seconds and only matters for workers that keep connections open, such as gthread; the default sync worker closes every connection after one response, so it never races. Don't confuse it with --timeout (30 seconds by default), which is how long a worker may stay silent before Gunicorn restarts it, and which can itself cut off slow requests.

nginx

http {
    keepalive_timeout 75s;

    server {
        listen 80;
        location / {
            proxy_pass http://127.0.0.1:3000;
        }
    }
}

keepalive_timeout controls idle connections from clients, which here means from the ALB. If nginx sits in front of your app on the same server, nginx is the target the ALB talks to, so this is the setting that counts. Test the file with nginx -t before reloading.

Tomcat (Java)

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           keepAliveTimeout="75000"
           redirectPort="8443" />

In server.xml, keepAliveTimeout is in milliseconds. Without it, Tomcat uses connectionTimeout for keep-alive too, and the standard file sets that to 20 seconds. For Spring Boot's embedded Tomcat, the equivalent is the server.tomcat.keep-alive-timeout property.

Jake: "So the server was hanging up after six seconds, and the load balancer kept calling back on the dead line."

Ethan: "Every so often, a visitor was the one on that call. One line in the server fixed it."

Every reason an ALB returns 504 Gateway Timeout

CauseWhat to check
The ALB couldn't connect to the target within 10 secondsSecurity groups, a target that's overloaded or frozen, a wrong target IP
The target accepted the connection but didn't respond within the idle timeoutSlow pages, stuck database queries, workers busy
The network ACL doesn't allow return traffic from targets to the ALB on ports 1024 to 65535The target subnets' network ACL outbound rules
The target's Content-Length header promises more bytes than it sendsAn app or proxy computing the length wrong; the ALB waits for bytes that never come
The TLS handshake with an HTTPS target took more than 10 secondsAn overloaded target, or a TLS setting it struggles with
A Lambda target: the Lambda service didn't respond before the connection timeoutRare; a Lambda function that runs too long is a 502, not a 504

Notice the difference between a blocked port and a closed one. A security group that doesn't allow the ALB in drops the packets silently, so the ALB waits out its 10-second connection timeout: 504. A server where nothing is listening on the port answers instantly with a reset: 502. The error code alone tells you which network problem to look for.

Should you raise the ALB idle timeout?

The idle timeout is 60 seconds by default and can be set from 1 to 4,000 seconds:

aws elbv2 modify-load-balancer-attributes \
  --load-balancer-arn arn:aws:elasticloadbalancing:us-east-1:111122223333:loadbalancer/app/booking-alb/50dc6c495c0c9188 \
  --attributes Key=idle_timeout.timeout_seconds,Value=120

Before you do, two cautions. First, it doesn't make anything faster: a page that takes 90 seconds still takes 90 seconds, and your visitors wait for it, and browsers, proxies and phone apps often give up sooner anyway. Fix the slow page, cache it, or turn it into a background job that the browser checks on. Our API Gateway timeout guide shows the asynchronous pattern, which works the same behind an ALB.

Second, and this is the trap: raising the ALB idle timeout can create 502s. The keep-alive rule is relative. An nginx target with its default 65 seconds is safe behind a 60-second ALB, but raise the ALB to 120 and nginx is suddenly the one closing idle connections first, and the random 502s begin. Whenever you raise the idle timeout, raise every target's keep-alive timeout above it in the same change.

Jake: "So making the timeout longer can make it worse."

Ethan: "If you only change one side. The sales report needed fixing, not a longer wait. I moved it into a background job and emailed you the file instead."

Lambda targets: why a timeout is a 502

With a Lambda function as the target, the ALB invokes it directly instead of over a network connection, and almost every failure surfaces as a 502:

  • The function didn't finish before its own timeout. That's a 502, not a 504. Raise the function's timeout (its default is only 3 seconds) or make the work faster.
  • The function threw an error or was throttled. Also 502. The access log's error_reason shows LambdaUnhandled or LambdaThrottling.
  • The response was larger than 1 MB. 502, with LambdaResponseTooLarge. Requests over 1 MB are refused before the function runs.
  • The response has the wrong shape. 502, with LambdaInvalidResponse. This one catches functions written for API Gateway.
  • The load balancer isn't allowed to invoke the function. LambdaAccessDenied. The console adds that permission for you; with the CLI you add it with aws lambda add-permission for the elasticloadbalancing.amazonaws.com principal.

For an ALB, the response must include statusCode, headers and isBase64Encoded; the body is optional. A function that returns only statusCode and body may work behind API Gateway but isn't complete for an ALB. A response that works everywhere:

import json


def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "statusDescription": "200 OK",
        "isBase64Encoded": False,
        "headers": {"Content-Type": "application/json"},
        "body": json.dumps({"bookings": 12}),
    }

One more rule: if you turn on multi-value headers for the target group, the function must return multiValueHeaders (with lists as values) instead of headers. Mixing the two is another way to get LambdaInvalidResponse. If the same function also sits behind API Gateway, its own response rules apply there; our API Gateway 502 guide covers that side.

Other places a 502 or 504 can come from

  • Elastic Beanstalk runs nginx in front of your app behind its load balancer, and a 502 there usually means nginx can't reach your app's port. Our Beanstalk 502 guide has the fix.
  • CloudFront in front of the ALB has its own origin timeouts and returns its own 502s and 504s; check whether the error page comes from CloudFront or the ALB. Our CloudFront 502 and 504 guide separates the two.
  • Deployments. 502s that cluster around releases come from targets being stopped while requests are still running. Raise the target group's deregistration delay so in-flight requests can finish, and make the app stop accepting new connections before it exits.
  • ECS tasks that fail health checks get replaced, and requests caught in between can fail; our ECS health check guide covers the cycle.

Troubleshooting by symptom

Occasional 502s, the app logs show nothing, a refresh works

The keep-alive race. Set the server's keep-alive timeout above the ALB idle timeout, for example 65 s against 60 s.

Every request returns 502

Nothing is listening on the target port, the app is crash-looping, or the TLS setup with an HTTPS target is broken. Check the target group's port against what the app listens on.

504 after exactly 60 seconds

The target didn't answer within the idle timeout. Find the slow request with the access log's timings, then fix it or make it asynchronous; raise the idle timeout only together with your servers' keep-alive.

504 after about 10 seconds

The ALB couldn't connect. Check that the target's security group allows the load balancer on the target port, and that the subnets' network ACLs allow return traffic on ports 1024 to 65535.

502s started right after raising the idle timeout

Your targets now close idle connections before the ALB. Raise their keep-alive timeouts above the new idle timeout.

Lambda target returns 502

Check the access log's error_reason: LambdaUnhandled, LambdaInvalidResponse, LambdaResponseTooLarge or LambdaThrottling. For timeouts, raise the function's timeout.

target_status_code is 502 too

Your app sent the 502 itself, usually because it's a proxy to something else that failed. Look at the app and whatever it calls.

ALB 502 and 504 errors: frequently asked questions

What is the difference between 502 and 504 on an AWS load balancer?

A 502 Bad Gateway means the ALB got a broken response or connection from the target: it closed or reset the connection, sent malformed data, or failed TLS. A 504 Gateway Timeout means the target was too slow to connect (10 seconds) or to respond (the idle timeout, 60 seconds by default).

Why does my ALB return random 502 errors?

Most often your server closes idle keep-alive connections sooner than the ALB, so the ALB occasionally sends a request down a connection that's closing. Set the server's keep-alive timeout higher than the ALB idle timeout, for example 65 seconds against 60.

What should keepAliveTimeout be behind an AWS ALB?

Longer than the ALB's idle timeout. With the default 60 seconds, set Node.js server.keepAliveTimeout to 65000 and headersTimeout a little higher, such as 66000. If you raise the ALB idle timeout later, raise keepAliveTimeout above it too.

What is the default ALB idle timeout?

60 seconds. You can set it from 1 to 4,000 seconds with the idle_timeout.timeout_seconds attribute in the load balancer's attributes or with aws elbv2 modify-load-balancer-attributes.

How do I fix a 504 Gateway Timeout on an ALB?

If it's after about 10 seconds, the ALB couldn't connect: check security groups and network ACLs. If it's after the idle timeout, the target was too slow: speed up the request or make it asynchronous, and raise the idle timeout only with your servers' keep-alive timeouts.

Why does a Lambda target return 502 instead of 504 when it times out?

Because the ALB invokes Lambda directly, a function that runs past its own timeout is treated as a failed response, which is a 502. Errors, throttling, responses over 1 MB and malformed responses are also 502s.

What response format does an ALB need from Lambda?

The response must include statusCode, headers and isBase64Encoded; body and statusDescription are optional. With multi-value headers enabled on the target group, return multiValueHeaders instead of headers. A malformed response gives a 502 with LambdaInvalidResponse.

How do I know if the 502 came from the ALB or my application?

Compare HTTPCode_ELB_5XX_Count (generated by the load balancer) with HTTPCode_Target_5XX_Count (sent by your targets). In access logs, a target_status_code of "-" means the ALB generated the error because the target never answered.

Can increasing the ALB idle timeout cause 502 errors?

Yes. If you raise it above your servers' keep-alive timeouts, for example to 120 seconds with nginx at its default 65, the servers start closing idle connections first, and the random 502 race begins. Raise both together.

What is Node.js's default keepAliveTimeout?

5,000 milliseconds. On Node.js 22 the server also adds a 1-second keepAliveTimeoutBuffer, so idle connections actually close after about 6 seconds, still far below the ALB's default 60.

What is Gunicorn's default keep-alive?

2 seconds, used by workers that keep connections, such as gthread. The default sync worker closes the connection after each response. Use --keep-alive 75 with gthread behind a 60-second ALB.

Does nginx cause 502 errors behind an ALB?

Not with the official image's default keepalive_timeout of 65 seconds and the ALB's default 60. It can if you raise the ALB idle timeout above 65 without raising nginx's keepalive_timeout.

What does a target_status_code of "-" mean in ALB access logs?

The ALB didn't get a response from the target: the connection failed, was closed, or timed out. The 502 or 504 the client received was generated by the load balancer, not by your app.

Why do I get 502 errors during deployments?

Targets are being stopped while requests are still running on them. Increase the target group's deregistration delay so in-flight requests finish, and have the app stop accepting connections before it shuts down.

Can a security group cause a 502 or a 504?

A security group that blocks the ALB drops packets silently, so the ALB times out connecting after 10 seconds: a 504. A closed port that answers with a reset gives a 502.

How long does an ALB wait to connect to a target?

10 seconds. If the TCP connection, or the TLS handshake with an HTTPS target, isn't done by then, the client gets a 504.

For readers preparing for an AWS certification

We have been covering AWS certifications alongside our fixes, so here is a short extra for readers studying for one. If you only came for the fix, you already have it; feel free to skip this part.

Exams that test this topic: Solutions Architect – Associate (SAA-C03) • CloudOps Engineer – Associate (SOA-C03) • Developer – Associate (DVA-C03 from December 1, 2026).

Here are the exam-style questions this topic tends to raise, with the reasoning you need to answer them:

1. What does a 502 from an Application Load Balancer usually mean?

The target closed the connection or returned a malformed response, often because its keep-alive timeout is shorter than the ALB idle timeout.

2. What does a 504 mean?

The target did not respond before the ALB idle timeout, 60 seconds by default.

3. What does a 503 mean?

The target group has no registered or healthy targets.

4. How do you tell ALB errors from target errors in metrics?

HTTPCode_ELB_5XX_Count counts errors generated by the load balancer; HTTPCode_Target_5XX_Count counts errors returned by targets.

5. Where do you see the reason for each failed request?

ALB access logs in S3, which include error reason fields and target processing times.

6. A Lambda target returns 502. What is a common cause?

The function's response is not in the format the ALB expects, or the function errors or times out.

Exam codes and dates change often. Our AWS certification changes page keeps track of what is current.

πŸ“š ALSO READ

Where to go next, from 502s at other AWS front doors to the health checks that decide which targets get traffic:

📌 Bookmark this; the server table and the cause lists apply to every app behind an ALB.

Two changes fixed Jake's site. Ethan set keepAliveTimeout to 65 seconds on the Node.js server, and the twice-a-day 502s stopped; the HTTPCode_ELB_502_Count graph went flat the next morning. He left the ALB idle timeout at 60 and moved the month-end sales report into a background job that emails Jake a file when it's ready, so there's nothing left to time out. He wrote the line below in the project's README, under "Things that look random but aren't."

📌 If you keep one line from this page

Random ALB 502s mean your server hangs up on idle connections before the load balancer does; keep every target's keep-alive above the ALB idle timeout, and raise both together or neither.

502 = broken answer or connection; 504 = too slow (10 s to connect, idle timeout to answer); Lambda timeouts are 502s.

Revision note. Written October 13, 2026, for every 502 that vanished on refresh. "It takes two to tango," says the old saying; with keep-alive, both sides have to agree who leaves first.

Related