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

PlatformAny website Doing it yourself10 to 40 minutes, and some of it is a host ticket Checked

Written and checked by Sajid, Founder & Web Engineer

The short answer

A 502 means one server asked another for your page and got a broken answer back. On a typical site that is the web server not getting a proper reply from PHP, often because PHP timed out, ran out of memory, or crashed on a heavy request. It is usually a server or hosting problem, not your content. Many 502s are brief; if it sticks, the cause is a timeout, a memory limit, a plugin, or the host itself.

Is this your problem?

  • A plain "502 Bad Gateway" page, often styled by the server or by Cloudflare
  • It comes and goes, or appears only on heavy pages or at busy times
  • It started after a traffic spike, a long import, or a plugin update
  • The admin dashboard and the front of the site are both affected
  • Cloudflare shows the error with a "Host Error" note and a ray ID

What usually causes it

1
PHP timed out or crashed on a slow request

How to tell: A long operation (an import, a report, a heavy plugin) ran past the time the web server waits, so it gave up.

2
PHP ran out of memory

How to tell: A heavy page or process hit the memory limit and the PHP worker died mid-request.

3
Too few PHP workers for the traffic

How to tell: A spike used up all the available PHP processes, so new requests had nothing to answer them.

4
A plugin, theme or custom-code fault

How to tell: Something is making a request the server cannot complete, often added just before it started.

5
A problem at the host or the CDN

How to tell: The server is down or restarting, or Cloudflare cannot reach it. That part is the host to fix.

Before you touch anything

  • Note whether it is constant or intermittent, and whether it follows heavy pages or busy times. That points straight at timeouts and limits.
  • Check your host status page and, if you use Cloudflare, whether the error is from Cloudflare or from your server. The error page says which.

How to fix it, step by step

  1. 1

    Reload, and wait a minute

    Many 502s are a brief hiccup while the server restarts or clears a spike. Refresh, and try again in a minute. If it clears and stays clear, it was transient.

  2. 2

    Rule out Cloudflare or your CDN

    Pause Cloudflare, or set it to development mode, and load the site directly. If it works without Cloudflare, the issue is the connection between Cloudflare and your server, often the server being slow or blocking it.

  3. 3

    Read the server error log

    In the host panel, open the PHP or server error log. Messages about "upstream timed out", "memory exhausted" or a named plugin file tell you which cause you have.

  4. 4

    Raise PHP time and memory limits

    If the log shows timeouts or memory, raise the PHP max_execution_time and memory_limit in the host panel. On managed hosting this may be a support request. It is the fix for 502s on long operations.

  5. 5

    Turn off the suspect plugin

    If it started after a plugin change, or the log names one, deactivate it, renaming its folder if you cannot reach wp-admin, and see if the 502 stops.

  6. 6

    Ask the host about PHP workers

    If it hits only at busy times, you may be out of PHP workers for your plan. The host can raise them, or it is a sign to move to a plan with more capacity.

Stop and get help if

  • The error log points at the server, PHP-FPM or the host, which only the host can act on. Open a ticket and quote the log line.
  • It is a shop or booking site losing orders while it flaps in and out.
  • It only happens under load and you do not control the server capacity.
Fix it for me

Stop it happening again

  • Run heavy jobs such as imports and backups at quiet times, and in batches if the tool allows.
  • Keep PHP time and memory limits sensible for your heaviest page.
  • Size hosting for your real traffic, spikes included, not only the average.
  • Watch uptime so a flapping 502 reaches you before customers report it.

Questions people ask

Is a 502 my fault or the host problem?

Often the server or host, but a heavy plugin or a long job on your side can trigger it. The error log tells you which.

Why does it come and go?

Intermittent 502s usually mean timeouts, or running out of PHP workers at busy moments, not a site that is fully down.

Is 502 different from 503 and 504?

Yes. 502 is a broken reply from the upstream, 504 is the upstream taking too long, and 503 means the server is unavailable or in maintenance. The causes overlap but the logs differ.

Not sure where your site stands?

Paste the address. Security, speed, SEO and how old the tech is, in about ten seconds.

Still broken? We can fix it.