Free auditStart free
GuideError · CDN and hosting errors

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.

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 withChatGPTClaudePerplexityGoogleAI Mode
02Fix 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
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)
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
  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.

03How 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
04Your 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"]
Questions

Asked a lot.

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

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

02Why 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.

03What 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. 01Google Cloud: Troubleshoot external Application Load Balancerscloud.google.com · checked 2026-11-01
  2. 02Google Cloud: Health check conceptscloud.google.com · checked 2026-11-01
  3. 03Google Cloud: Create health checkscloud.google.com · checked 2026-11-01
  4. 04Google Cloud: External Application Load Balancer logging and monitoringcloud.google.com · checked 2026-11-01
  5. 05nginx documentation: ngx_http_core_modulenginx.org · checked 2026-11-01
  6. 06How HTTP status codes affect Google's crawlersdevelopers.google.com · checked 2026-11-01
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