# 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.

Canonical: https://scaplet.com/errors/cdn/aws-502-bad-gateway

Guide Error · CDN and hosting

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 with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fcdn%2Faws-502-bad-gateway%20(%22AWS%20502%20Bad%20Gateway%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fcdn%2Faws-502-bad-gateway%20(%22AWS%20502%20Bad%20Gateway%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fcdn%2Faws-502-bad-gateway%20(%22AWS%20502%20Bad%20Gateway%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fcdn%2Faws-502-bad-gateway%20(%22AWS%20502%20Bad%20Gateway%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

On this page

## 3 parts, 4 minutes.

1. [Which AWS layer sent the 502 01](#which-aws-layer-sent-the-502)
2. [Application Load Balancer 502 causes 02](#application-load-balancer-502-causes)
3. [CloudFront 502 causes 03](#cloudfront-502-causes)

_01_Tell the layers apart

## Which AWS layer sent the 502

RFC 9110 defines 502 as a gateway receiving an invalid response from an [inbound](/glossary/inbound-marketing) server. On AWS the gateway is usually an Application Load Balancer (ALB) or CloudFront. The general meaning is on [502 Bad Gateway](/errors/http/502); for the web server behind the gateway, see [nginx](/errors/http/502/nginx) and [Apache](/errors/http/502/apache).

Layer

### ALB

Where to look

Access log fields `elb_status_code` and `target_status_code`

What it shows

`elb_status_code` is the status of the response the load balancer generated. `target_status_code` is recorded only if a connection was made and the target replied; otherwise it is `-`

Layer

### CloudFront

Where to look

The `X-Cache` response header

What it shows

AWS says CloudFront sets `X-Cache` to `Error from cloudfront` for several origin TLS failures

Layer

### The target itself

Where to look

The app's own log

What it shows

The ALB forwards valid HTTP responses from targets to the client, so a 502 your app sends passes through

Check the response headers

```
curl -sSI https://www.example.com/ | grep -i -E "^HTTP|x-cache|server|via"
```

_02_ALB 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](/glossary/rate-limiting)

Fix

Shrink the response, raise the function timeout, fix the error; see [Lambda timeout](/errors/cdn/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"
```

**502 and 504 are different on an ALB**A 504 means the load balancer could not connect within its 10 second connection timeout, or the target did not respond before the idle timeout. That is a timeout, not an invalid reply; see [504 Gateway Timeout](/errors/http/504).

_03_CloudFront 502 causes

## CloudFront 502 causes

AWS documents a CloudFront 502 as CloudFront being unable to serve the object because it couldn't connect to the origin server. The listed causes:

Cause

### SSL/TLS negotiation failure with a custom origin

What to check

The origin certificate must include a domain name that matches the Origin Domain on the distribution or the Host header of the origin request

Cause

### Origin does not respond with supported ciphers or protocols

What to check

Compare the origin with the ciphers and protocols CloudFront supports to origins

Cause

### Expired, invalid or self-signed certificate, or chain in the wrong order

What to check

If the full chain with the intermediate is missing, CloudFront drops the connection

Cause

### Origin not responding on the ports in the origin settings

What to check

The ports default to TCP 80 and 443. Check that firewalls do not block the CloudFront IP ranges and that the web server is running on the origin

Cause

### Lambda@Edge function response malformed or invalid

What to check

A Lambda validation error: check the function's response format

Cause

### CloudFront function changes a read-only header

What to check

The error shows up only after you deploy the function and run a request, not in testing. Update the function

Cause

### DNS error for the origin (`NonS3OriginDnsError`)

What to check

Check that the origin domain resolves

Test the origin's TLS the way CloudFront connects

```
openssl s_client -connect origin.example.com:443 -servername www.example.com
```

For an ALB origin, AWS sends you to its ALB 502 guide; for API Gateway with Lambda proxy integration, to its Knowledge Center article. Google treats 502 as a server error; see [5xx server errors](/seo-issues/server-errors-5xx).

Questions

## Asked a lot.

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

_01_ How 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.

_02_ Is 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.

_03_ Will 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.

_04_ What 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. 01 [RFC 9110: HTTP Semantics](https://www.rfc-editor.org/rfc/rfc9110.html) rfc-editor.org · checked 2026-10-25
2. 02 [Troubleshoot your Application Load Balancers (AWS docs)](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-troubleshooting.html) docs.aws.amazon.com · checked 2026-10-25
3. 03 [Edit attributes for your Application Load Balancer (AWS docs)](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/edit-load-balancer-attributes.html) docs.aws.amazon.com · checked 2026-10-25
4. 04 [Access logs for your Application Load Balancer (AWS docs)](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html) docs.aws.amazon.com · checked 2026-10-25
5. 05 [HTTP 502 status code (Bad Gateway) - Amazon CloudFront (AWS docs)](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/http-502-bad-gateway.html) docs.aws.amazon.com · checked 2026-10-25
6. 06 [How HTTP status codes affect Google's crawlers](https://developers.google.com/crawling/docs/troubleshooting/http-status-codes) developers.google.com · checked 2026-10-25

## Keep learning

[All errors](/errors)

[Learn AWS Lambda timeout CDN and hosting errors](/errors/cdn/lambda-timeout) [Related 502 Bad Gateway in nginx HTTP status codes](/errors/http/502/nginx) [Read next 502 Bad Gateway HTTP status codes](/errors/http/502) [Do Run a free site audit Free, about two minutes](/site-audit)

Other 502 errors
[502 Bad Gateway in nginx](/errors/http/502/nginx) [502 Bad Gateway on Azure](/errors/http/502/azure) [502 Bad Gateway on Cloudflare](/errors/http/502/cloudflare) [AWS Lambda 502 Bad Gateway](/errors/cdn/aws-lambda-502) [Azure Front Door 502](/errors/cdn/azure-front-door-502)

More Aws errors
[AWS EC2: Connection timed out](/errors/cdn/aws-ec2-connection-timed-out)

More in CDN and hosting errors
[Vercel 413: FUNCTION_PAYLOAD_TOO_LARGE](/errors/cdn/vercel-payload-too-large) [Vercel 429: Too Many Requests](/errors/cdn/vercel-429) [Vercel 500: Internal Server Error](/errors/cdn/vercel-500) [Vercel build failed](/errors/cdn/vercel-build-failed) [Vercel domain not working](/errors/cdn/vercel-domain-not-working) [Vercel FUNCTION_INVOCATION_TIMEOUT](/errors/cdn/vercel-function-invocation-timeout) [Vercel: too many redirects](/errors/cdn/vercel-too-many-redirects) [Akamai reference number](/errors/cdn/akamai-reference-number) [ALB 503 Service Unavailable](/errors/cdn/alb-503) [ALB 504 Gateway Timeout](/errors/cdn/alb-504-gateway-timeout) [API Gateway 403 Forbidden](/errors/cdn/api-gateway-403-forbidden) [API Gateway 504 Gateway Timeout](/errors/cdn/api-gateway-504) [AWS 502 Bad Gateway](/errors/cdn/aws-502-bad-gateway) [AWS Amplify 404 Not Found](/errors/cdn/amplify-404) [AWS EC2: Connection timed out](/errors/cdn/aws-ec2-connection-timed-out) [AWS Lambda 502 Bad Gateway](/errors/cdn/aws-lambda-502) [AWS Lambda timeout](/errors/cdn/lambda-timeout) [Azure Front Door 502](/errors/cdn/azure-front-door-502) [Cloud Run 503 error](/errors/cdn/cloud-run-503) [Cloudflare 504 Gateway Timeout](/errors/cdn/cloudflare-504) [Cloudflare Pages 404 Not Found](/errors/cdn/cloudflare-pages-404) [Cloudflare too many redirects (ERR_TOO_MANY_REDIRECTS)](/errors/cdn/cloudflare-too-many-redirects) [Cloudflare verify you are human loop](/errors/cdn/cloudflare-verify-you-are-human-loop) [CloudFront 400: Bad Request](/errors/cdn/cloudfront-400)

Terms on this page
[Inbound marketing](/glossary/inbound-marketing) [Rate limiting](/glossary/rate-limiting)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

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

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
