Opens in a new tab

502 Bad Gateway: what it means and how to fix it

The server you reached is a middleman — nginx in front of PHP, a proxy in front of an app, or Cloudflare in front of your server — and the program behind it did not give a valid answer. Usually that program is not running, crashed while answering, or sent headers too big to pass on. The middleman’s error log names which.

502 Bad Gateway
nginx
How 5 other clients word the same error

Apache

Bad Gateway
The proxy server received an invalid response from an upstream server.

Apache mod_proxy

Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request
Reason: Error reading from remote server

Cloudflare

Bad gateway
Error code 502

ASP.NET Core on IIS

HTTP Error 502.5 - ANCM Out-Of-Process Startup Failure

curl -I

HTTP/2 502
Error502 Bad Gateway
Usually fixed byThe site owner or host
Status502 (RFC 9110 §15.6.3)
Comes fromThe server in the middle — nginx, a proxy, a load balancer or Cloudflare
Usual causeThe application behind it (PHP-FPM, Node, a container) is down or crashed

Check the site from outside

Enter the address: we load it from our network and show the status code, how long it took, and the error if it failed.

What it means

A gateway or proxy passes requests on to the program that actually builds the page — the upstream. A 502 means the upstream’s answer was missing or invalid: the connection failed, was cut, or brought back something that is not HTTP. nginx gives 502 for every upstream failure except a timeout, which is 504.

On Cloudflare the page tells you which side failed: a Cloudflare-styled page (“Bad gateway — Error code 502”, with the host marked “Error”) passes on your server’s own 502, while a plain white page with “cloudflare” under “502 Bad Gateway” comes from Cloudflare itself.

Why it happens

  • PHP-FPM (or the Node, Python or .NET app) is stopped, crashed, or listening on a different socket or port than the web server expects.
  • The application was killed mid-request — by PHP-FPM’s request_terminate_timeout, or by the system running out of memory.
  • Response headers bigger than the proxy’s buffer, usually from large cookies.
  • A firewall between the proxy and the backend.

What the error log says

nginx, PHP-FPM is not running (its socket is missing)

connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory) while connecting to upstream

nginx, nothing listening on the backend’s port

connect() failed (111: Connection refused) while connecting to upstream

nginx, headers too big for its buffer

upstream sent too big header while reading response header from upstream

nginx, the backend crashed or was killed mid-answer

upstream prematurely closed connection while reading response header from upstream

nginx, the backend reset the connection

recv() failed (104: Connection reset by peer) while reading response header from upstream

Apache mod_proxy

AH01102: error reading status line from remote server 127.0.0.1:8080

PHP-FPM, a request killed for running too long

[pool www] child 12345, script '/var/www/index.php' (request: "GET /index.php") execution timed out (30.1 sec), terminating

How to fix it

  1. Read the proxy’s error log (nginx: error.log) at the time of the error — the lines below say which case it is.
  2. Restart PHP-FPM or the application, and check its listen address matches the web server’s fastcgi_pass or proxy_pass. On cPanel, PHP-FPM is in MultiPHP Manager.
  3. If it keeps dying, read the application’s own log: memory, a fatal error, a time limit.
  4. For “too big header”, raise fastcgi_buffer_size or proxy_buffer_size (16k or 32k), and find what sets such large cookies.

If you are visiting the site

  • Nothing on your side causes it. Reload after a minute; if it stays, the site owner has to fix it.

See it for yourself

tail -n 50 /var/log/nginx/error.log
systemctl status php8.3-fpm

The first shows why the proxy gave up; the second whether PHP-FPM is running (the service name carries the PHP version).

Questions

Is a Cloudflare 502 Cloudflare’s fault?

Usually not. Cloudflare passes on a 502 from your server in its own styling, with the host marked “Error”. Only the plain page with “cloudflare” printed under the heading comes from Cloudflare itself.

Sources

Error text and meaning checked on 25 September 2026 against these official sources:

Know when your site goes down — before your visitors do

Add your site and we check it every few minutes, confirm a failure from another location — Europe, North America or Asia — before calling it down, and email you when it stops answering and when it is back. Free for two sites.

Create a free account