Free auditStart free
GuideError · CDN and hosting

AWS 502 Bad Gateway

Fix an AWS 502 Bad Gateway: tell ALB from CloudFront from the logs, then check keep-alive vs idle timeout, target responses, Lambda targets and origin certificates.

4 min
read
6
sources
Oct 21, 2026
updated
Scaplet Editorial Team
author
In one line

On AWS, a 502 Bad Gateway comes from the Application Load Balancer or CloudFront when the target or origin behind it resets the connection, sends an invalid response, or fails TLS. Find which layer sent it from the ALB access log or the CloudFront X-Cache header, then check that the target's keep-alive timeout is longer than the load balancer's idle timeout.

30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode
02ALB causes listed in the AWS troubleshooting guide

Application Load Balancer 502 causes

Cause

TCP RST from the target when the load balancer connects, or an unexpected reply such as host unreachable

Fix
Check that traffic is allowed from the load balancer subnets to the targets on the target port
Cause

The target closed the connection with RST or FIN while a request was outstanding

Fix
Make the target's keep-alive duration longer than the load balancer's idle timeout
Cause

The target response is malformed or has invalid HTTP headers

Fix
Fix the app or the web server in front of it
Cause

The target response header exceeded 32 K for the entire header

Fix
Reduce the size of the response headers the target sends
Cause

The deregistration delay elapsed for a request on a deregistered target

Fix
Increase the deregistration delay so long operations can finish
Cause

Lambda target: body over 1 MB, no reply before the function timeout, an error, or throttling

Fix
Shrink the response, raise the function timeout, fix the error; see Lambda timeout
Cause

SSL handshake error when connecting to a target

Fix
Check the TLS configuration of the target

The keep-alive mismatch is the one to check first on intermittent 502s. AWS recommends configuring the application's idle timeout to be larger than the load balancer's, and says the default load balancer idle timeout is 60 seconds.

Raise the ALB idle timeout (valid range 1 to 4000 seconds)
aws elbv2 modify-load-balancer-attributes \
  --load-balancer-arn <load-balancer-arn> \
  --attributes "Key=idle_timeout.timeout_seconds,Value=120"
Questions

Asked a lot.

4 questions people ask next, answered from the sources on this page.

01How can I fix a 502 Bad Gateway error on AWS?

Decide whether the ALB or CloudFront sent it, using the access log fields or the X-Cache header. For an ALB, check security groups, make the target's keep-alive longer than the idle timeout, and validate the target's response headers. For CloudFront, check the origin certificate, ports and DNS.

02Is a 502 Bad Gateway my fault?

As a visitor, no. As the owner of the AWS setup, the cause is in the load balancer, CDN or target configuration, so it is yours to fix.

03Will error 502 fix itself?

Only if the cause was brief, such as a target restart. Intermittent 502s from a keep-alive mismatch repeat until the timeouts are changed.

04What is the default ALB idle timeout?

60 seconds. You can change it from 1 to 4000 seconds with the idle_timeout.timeout_seconds attribute.

Sources · 6

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • rfc-editor.org
  • amazon.com
  • google.com
  1. 01RFC 9110: HTTP Semanticsrfc-editor.org · checked 2026-10-25
  2. 02Troubleshoot your Application Load Balancers (AWS docs)docs.aws.amazon.com · checked 2026-10-25
  3. 03Edit attributes for your Application Load Balancer (AWS docs)docs.aws.amazon.com · checked 2026-10-25
  4. 04Access logs for your Application Load Balancer (AWS docs)docs.aws.amazon.com · checked 2026-10-25
  5. 05HTTP 502 status code (Bad Gateway) - Amazon CloudFront (AWS docs)docs.aws.amazon.com · checked 2026-10-25
  6. 06How HTTP status codes affect Google's crawlersdevelopers.google.com · checked 2026-10-25
Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane