# 502 Bad Gateway: when the server behind the proxy fails

> 502 Bad Gateway means a proxy or gateway got an invalid response, or none, from the upstream server it relies on.

URL: https://yes-seo.com/http-status-codes/502

**502 Bad Gateway** comes from a server that sits in front of another one, such as a reverse proxy, load balancer or CDN, when the server behind it sends back an invalid response or none at all. The front server is working; the application behind it isn’t answering properly.

## Definition

[RFC 9110, section 15.6.3](https://www.rfc-editor.org/rfc/rfc9110#section-15.6.3): the server, “while acting as a gateway or proxy, received an invalid response from an inbound server it accessed while attempting to fulfill the request.” On a typical stack that’s Nginx (the gateway) talking to PHP-FPM, Node.js, Gunicorn or another app server. If the app answers correctly but too slowly, you get [504](https://yes-seo.com/http-status-codes/504) instead.

## Common causes

- **The app server is down**: PHP-FPM stopped, a Node process crashed, a container restarted.

- **A wrong upstream address**, such as Nginx pointing at `php8.1-fpm.sock` after an upgrade to PHP 8.3.

- **The app died mid-request**: out-of-memory kills, crashes or fatal errors that close the connection.

- **Response headers too big** for the proxy’s buffers, often large cookies or long security headers.

- **Deploys without graceful restarts**, producing short bursts of 502s.

The Nginx error log usually names the cause:

```
connect() to unix:/run/php/php8.1-fpm.sock failed (2: No such file or directory)
connect() failed (111: Connection refused) while connecting to upstream
upstream prematurely closed connection while reading response header from upstream
upstream sent too big header while reading response header from upstream
```

## SEO impact

Google treats 502 like other server errors, according to [its documentation](https://developers.google.com/search/docs/crawling-indexing/http-network-errors): Googlebot reduces its crawl rate in proportion to the number of failing URLs, keeps already indexed pages for a while, and drops URLs that keep failing. Occasional 502s during deploys are mostly harmless, but they add up on large sites and show in Search Console’s Crawl Stats and “Server error (5xx)” reports.

## How to fix a 502

### For site owners

Reload after a minute. If the error persists, contact your host with the time and the URL. On managed WordPress hosting, a plugin that crashes PHP is a common trigger.

### For developers

```
# is the app server running?
systemctl status php8.3-fpm
sudo systemctl restart php8.3-fpm

# does Nginx point at the right socket?
grep -R "fastcgi_pass" /etc/nginx/

# was something killed for lack of memory?
dmesg -T | grep -i -E "killed process|out of memory"
```

For “too big header” errors, raise the buffers:

```
fastcgi_buffer_size 32k;
fastcgi_buffers 16 16k;
# or, for proxy_pass upstreams
proxy_buffer_size 32k;
proxy_buffers 16 16k;
```

Use health checks on your load balancer and rolling or graceful restarts so deploys don’t drop requests. Behind Cloudflare, an origin failure may surface as a Cloudflare-specific code instead, such as [520](https://yes-seo.com/http-status-codes/520) or [521](https://yes-seo.com/http-status-codes/521).

## How to check

The [HTTP status checker](https://yes-seo.com/http-status-checker) shows whether the error affects many URLs or just a few, and the `Server` header in the [HTTP header checker](https://yes-seo.com/http-header-checker) tells you which layer produced it.

```
HTTP/1.1 502 Bad Gateway
Server: nginx
Content-Type: text/html
Content-Length: 150
```

## Frequently asked questions

### What is the difference between 502 and 504?

A 502 means the gateway got an invalid response or none, often because the app crashed or isn’t running. A 504 means the app was reachable but didn’t answer before the gateway’s timeout.

### Is a 502 Bad Gateway my fault or my host’s?

It’s a server-side problem, so visitors can’t fix it. On your own server, check the app process and the proxy configuration; on managed hosting, send your host the URL and time of the error.

### Does a 502 error hurt SEO?

Short bursts rarely matter, but frequent or long-lasting 502s make Googlebot crawl less and can get URLs removed from the index.

