# Google Cloud 502 Bad Gateway

> A Google Cloud load balancer 502 is explained by one log field, statusDetails. Match it to a cause, then fix the firewall, keep-alive or GKE shutdown timing.

Canonical: https://scaplet.com/errors/cdn/google-cloud-502

Guide Google Cloud Error · CDN and hosting errors

A Google Cloud load balancer 502 is explained by one log field, statusDetails. Match it to a cause, then fix the firewall, keep-alive or GKE shutdown timing.

5 min

read

6

sources

Oct 31, 2026

updated

Scaplet Editorial Team

author

In one line

A 502 from a Google Cloud external Application Load Balancer is either relayed from your backend or generated by the load balancer, and the Cloud Logging field `jsonPayload.statusDetails` says which. Values such as `failed_to_connect_to_backend` or `backend_connection_closed_before_data_sent_to_client` point to firewall rules, a listening port, or a backend keep-alive shorter than the load balancer's 600 seconds.

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%2Fgoogle-cloud-502%20(%22Google%20Cloud%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%2Fgoogle-cloud-502%20(%22Google%20Cloud%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%2Fgoogle-cloud-502%20(%22Google%20Cloud%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%2Fgoogle-cloud-502%20(%22Google%20Cloud%20502%20Bad%20Gateway%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

On this page

## 5 parts, 5 minutes.

1. [Relayed or generated: one field decides 01](#relayed-or-generated-one-field-decides)
2. [Check it in 2 minutes 02](#check-it-in-2-minutes)
3. [Fix a backend the load balancer cannot reach 03](#fix-a-backend-the-load-balancer-cannot-reach)
4. [Fix connections closed before the response 04](#fix-connections-closed-before-the-response)
5. [What a Google Cloud 502 does to search 05](#what-a-google-cloud-502-does-to-search)

_01_What statusDetails says about a 502

## Relayed or generated: one field decides

Google's troubleshooting guide for external Application Load Balancers makes the first split for you. A 5xx can come from a first-layer Google Front End (GFE), a second-layer GFE, or your backend. If your backend sends a 5xx, the load balancer relays it. If the problem is the connection between the load balancer proxy and the backend, the load balancer generates the error itself. The `statusDetails` field in the request log is how you tell them apart.

statusDetails

### `response_sent_by_backend`

Meaning in Google's guide

The load balancer relayed the 5xx from the backend

Where to look

Your backend's own logs

statusDetails

### `failed_to_pick_backend`, `failed_to_pick_backend_by_hash`

Meaning in Google's guide

The second-layer GFE could not select a healthy backend

Where to look

Health checks, firewall rule for probers, capacity

statusDetails

### `failed_to_connect_to_backend`

Meaning in Google's guide

The second-layer GFE could not get a SYN-ACK from a backend instance

Where to look

Firewall rules, whether the app listens on the backend service's port

statusDetails

### `backend_connection_closed_before_data_sent_to_client`

Meaning in Google's guide

The backend or something between closed the connection before a response was sent

Where to look

Backend keep-alive timeout, GKE pod shutdown timing

Request path and who can say 5xx

```
client --> first-layer GFE --> second-layer GFE --> your backend | | generates 5xx for connection sends its own 5xx, or pick-a-backend problems which is relayed as-is (statusDetails: response_sent_by_backend)
```

**Which status code you see depends on the load balancer type**Google's guide says the classic Application Load Balancer always uses 502 for load-balancer-generated errors, while the global and regional external Application Load Balancers generate more specific codes such as 503 and 504. So the same `statusDetails` can look like a different 5xx depending on which product you run.

_02_Fix it on your stack

## Check it in 2 minutes

The first command uses a public test service. The Cloud Logging commands need your project and the `gcloud` CLI; I did not run them here, so the syntax is adapted from the Cloud Logging resource type and fields named in the load balancer docs.

Public test endpoint: read a 502 status
Public test endpoint: read a 502 status

```
curl -sS -o /dev/null -w "%{http_code}\n" https://httpbin.org/status/502 # observed: 502
```

Your project: group recent load balancer 5xx by cause (needs gcloud and Logging access)
Your project: group recent load balancer 5xx by cause (needs gcloud and Logging access)

```
gcloud logging read \ 'resource.type="http_load_balancer" AND httpRequest.status>=500' \ --limit=200 \ --format='value(jsonPayload.statusDetails)' \ | sort | uniq -c | sort -rn
```

Output shape: a count per statusDetails value
Output shape: a count per statusDetails value

```
41 failed_to_connect_to_backend 3 response_sent_by_backend
```

The counts above show the shape only; they are not from a real project. Your top line tells you which row to read next. If it is `response_sent_by_backend`, stop reading load balancer docs and debug the application.

**Check health before the rest**Google says to verify backend health first, because unhealthy backends mean the second-layer GFE cannot forward requests even when everything else is right. Turn on health check logging and look for successful entries. On a new load balancer the backend starts as UNHEALTHY, so successes appear only after the prober gets a `200 OK`.

_03_How it works

## Fix a backend the load balancer cannot reach

`failed_to_pick_backend` and `failed_to_connect_to_backend` share the same first suspect: an ingress firewall rule. Google Cloud requires an allow rule for the health check probers, and with none, the implied deny rule blocks the probes and the backends turn unhealthy. For GFE-based external Application Load Balancers the IPv4 probe range in the docs is `35.191.0.0/16`, and the guide says to allow all probe ranges for your load balancer type because new probers can appear without notice.

1. 1

### Match the probe ranges to your product

Open the probe IP ranges table in the health check concepts page and copy the ranges for your load balancer type and backend type.
2. 2

### Check the port

The app must be running and listening on the port configured in the backend service. A health check on the wrong port, path or protocol fails the same way.
3. 3

### Check resources

Look at CPU, memory and connection metrics on the backends. Google lists resource exhaustion as a cause of failed connections.
4. 4

### Drain unhealthy groups

If `failed_to_pick_backend` continues while some instance groups are healthy, the guide says failover is limited to a few alternate groups, and to drain the unhealthy groups by setting their capacity to zero.

Allow health checks to tagged instances (pattern from the Google Cloud docs)

```
gcloud compute firewall-rules create fw-allow-health-checks \ --network=NETWORK_NAME \ --action=ALLOW \ --direction=INGRESS \ --source-ranges=35.191.0.0/16 \ --target-tags=allow-health-checks \ --rules=tcp:PORT
```

_04_Your call

## Fix connections closed before the response

`backend_connection_closed_before_data_sent_to_client` is the keep-alive race, and Google gives specific numbers. Set the HTTP keep-alive timeout on the backend web server to more than the load balancer's 600-second timeout, with a recommended 620 seconds. nginx's documented `keepalive_timeout` default is 75 seconds, which is well short of that number, so an nginx left at its default needs the change.

If this fits

### Change the backend when

- The log says `backend_connection_closed_before_data_sent_to_client`
- You run nginx or Apache with default keep-alive settings
- Errors cluster around pod scale-down or a rollout

Or

### Wait it out when

- `failed_to_pick_backend` appears right after you added or removed a backend service
- A 503 `retriable_error` follows an apply that recreated URL map rules
- Google says configuration changes can cause a brief period of 502s during propagation to the GFEs

nginx: keep-alive above the load balancer's 600 seconds

```
http { keepalive_timeout 620s; }
```

On GKE, Google ties the same error to pods that are scaling down or terminating when the load balancer backends are NEGs. The remedy is a longer `terminationGracePeriodSeconds` so existing connections can close. The guide's formula is Tgrace > TpreStop + Tdrain + Tbuffer, with a preStop sleep that is typically 120 seconds, connection draining typically 60 seconds, and a buffer of 30 to 45 seconds. For standard setups it recommends at least 210 seconds.

Pod spec: grace period above preStop plus drain plus buffer (adapted from Google's guidance)

```
spec: terminationGracePeriodSeconds: 210 containers: - name: app lifecycle: preStop: exec: command: ["sleep", "120"]
```

_05_Explained

## What a Google Cloud 502 does to search

Search crawlers back off from 5xx responses, according to Google's [status code](/glossary/http-status-codes) guide, and URLs already in the index can fall out if the errors persist. A short burst of `failed_to_pick_backend` during a configuration change is the documented harmless kind. A persistent `failed_to_connect_to_backend` is not, and an automation tool that deletes then recreates URL map rules can produce its own 503 window. Prefer in-place updates, as the guide says. See [5xx server errors](/seo-issues/server-errors-5xx) and [Cloud Run 503](/errors/cdn/cloud-run-503) for the serverless side.

Questions

## Asked a lot.

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

_01_ How do I tell whether a Google Cloud 502 came from my app?

Check `jsonPayload.statusDetails` in Cloud Logging. `response_sent_by_backend` means the load balancer relayed a 5xx your backend produced. Any other failure message means the load balancer generated the error.

_02_ Why do I get 502 for a minute after changing my load balancer?

Google's guide says configuration changes such as adding or removing a backend service can cause a brief period of 502 responses with `statusDetails` of `failed_to_pick_backend`, and that this is normal while the change propagates to the GFEs globally.

_03_ What backend keep-alive timeout does Google recommend?

Greater than the load balancer's 600-second timeout, with 620 seconds recommended. The guide names Apache and nginx as examples of backend web servers to configure.

Sources · 6

## Every fact, linked and dated.

Official docs first. The date is when we last opened each page.

- google.com
- nginx.org

1. 01 [Google Cloud: Troubleshoot external Application Load Balancers](https://cloud.google.com/load-balancing/docs/https/troubleshooting-ext-https-lbs) cloud.google.com · checked 2026-11-01
2. 02 [Google Cloud: Health check concepts](https://cloud.google.com/load-balancing/docs/health-check-concepts) cloud.google.com · checked 2026-11-01
3. 03 [Google Cloud: Create health checks](https://cloud.google.com/load-balancing/docs/health-checks) cloud.google.com · checked 2026-11-01
4. 04 [Google Cloud: External Application Load Balancer logging and monitoring](https://cloud.google.com/load-balancing/docs/https/https-logging-monitoring) cloud.google.com · checked 2026-11-01
5. 05 [nginx documentation: ngx_http_core_module](https://nginx.org/en/docs/http/ngx_http_core_module.html) nginx.org · checked 2026-11-01
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-11-01

## Keep learning

[All errors](/errors)

[Learn AWS 502 Bad Gateway CDN and hosting errors](/errors/cdn/aws-502-bad-gateway) [Related 5xx server errors Status codes and links](/seo-issues/server-errors-5xx) [Read next Cloud Run 503 error CDN and hosting errors](/errors/cdn/cloud-run-503) [Do Run a free site audit Free, about two minutes](/site-audit)

Other 502 errors
[AWS 502 Bad Gateway](/errors/cdn/aws-502-bad-gateway) [AWS Lambda 502 Bad Gateway](/errors/cdn/aws-lambda-502) [Azure Front Door 502](/errors/cdn/azure-front-door-502) [Render 502 Bad Gateway](/errors/cdn/render-502) [502 Bad Gateway](/errors/http/502)

More in CDN and hosting errors
[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) [CloudFront 404 Not Found](/errors/cdn/cloudfront-404) [CloudFront 503: Service Unavailable](/errors/cdn/cloudfront-503) [Firebase Hosting 404](/errors/cdn/firebase-hosting-404) [GitHub Pages 404 Not Found](/errors/cdn/github-pages-404) [Google Cloud 502 Bad Gateway](/errors/cdn/google-cloud-502) [Heroku 503 Service Unavailable](/errors/cdn/heroku-503) [Heroku H10: App crashed](/errors/cdn/heroku-h10-app-crashed) [Heroku H12: Request timeout](/errors/cdn/heroku-h12-request-timeout) [Heroku H13: Connection closed without response](/errors/cdn/heroku-h13) [Heroku H14: No web processes running](/errors/cdn/heroku-h14) [Netlify build failed](/errors/cdn/netlify-build-failed) [Netlify function timeout](/errors/cdn/netlify-function-timeout) [Render 502 Bad Gateway](/errors/cdn/render-502) [S3 Access Denied (403 Forbidden)](/errors/cdn/s3-access-denied) [Vercel 413: FUNCTION_PAYLOAD_TOO_LARGE](/errors/cdn/vercel-payload-too-large) [Vercel 429: Too Many Requests](/errors/cdn/vercel-429)

Terms on this page
[HTTP status codes](/glossary/http-status-codes)

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
