Guide Errors & Troubleshooting

How to fix a 502 Bad Gateway error

One server passed your request to another and got back garbage — here is how to work out which of the two is actually at fault.

Updated 7 min read Intermediate

A 502 Bad Gateway error means a server acting as a gateway or proxy passed your request along to another server, and got back a response that made no sense — or got no response at all because the other server dropped the connection. It is not, strictly, an error from your website. It is a complaint from whatever sits in front of it about what came back.

That "whatever sits in front of it" might be a CDN like Cloudflare, a load balancer, or a proxy layer that is part of the normal hosting stack even when you never interact with it directly. Working out which side actually failed is most of the job.

Your data is not at risk

A 502 is a connection and process problem, not a data problem. Nothing about it implies files or a database have been damaged — it means a request could not be completed, not that anything behind it was lost.

The causes, in the order worth checking

1. The application process behind the gateway has crashed or timed out

On most modern hosting, PHP runs as a separate worker process (commonly PHP-FPM) that the web server hands requests to. If that worker crashes — from a fatal error, from running out of memory, or from a runaway script — the web server gets nothing usable back and reports it to the visitor as a 502. This is the most common cause on ordinary hosting and it points at the site's own code rather than the platform.

Open the error log section of your control panel and look at the timestamp of the 502. A PHP fatal error, a segmentation fault, or an "out of memory" entry logged at the same moment confirms this is your cause, and names the specific script responsible.

2. A CDN or proxy cannot reach the origin server at all

If you run the site through Cloudflare or a similar service, a 502 there often means Cloudflare's edge could not establish a connection to your actual server — because of a firewall rule blocking the proxy's IP ranges, a certificate mismatch on the connection between the two, or the origin server being briefly unreachable. Check the CDN's own status and SSL mode settings before assuming the problem sits on the hosting side.

3. The server is overloaded

A sudden spike in traffic, or a single heavy process consuming most of the available resources, can exhaust the pool of application workers. When every worker is busy, new requests have nothing to be handed to, and the gateway reports 502 rather than queuing indefinitely. This shows up as an intermittent error — some requests succeed, some fail — rather than a constant one, which is the tell that points here rather than at broken code.

4. Recently changed code is the direct cause

If the 502 started right after a plugin update, a theme change, or a code deployment, treat that as the leading suspect regardless of what else is on this list. Disable the most recently changed component and reload. On WordPress, if the admin area is also affected, renaming the wp-content/plugins folder deactivates every plugin at once without needing to log in, which is often the fastest way to confirm or rule this out.

5. A malformed .htaccess or server configuration change

Less common than the above, but a directive the gateway can't reconcile with the application layer behind it can produce this exact error. If nothing else fits, and a configuration file was edited recently, reverting that specific change is worth testing before moving on to broader troubleshooting.

Reload once before troubleshooting

Because 502s are frequently transient — a worker restarting, a momentary spike — a straightforward reload after a short pause rules out a problem that has already resolved itself before you spend time chasing it.

Telling a platform issue from a site issue

A 502 affecting a single site, correlated with a specific plugin update, a specific script, or a specific time of unusually high traffic to that one site, points at the application. A 502 appearing across unrelated sites on the same infrastructure at the same time, with nothing having changed on any of them, points at the platform — and is worth reporting rather than debugging further on your own, since there is nothing in your files to fix.

If you can't identify the cause

Give us the exact time the 502 occurred and the URL that returned it. That is enough to check the server-side logs directly, which will show whether the upstream process crashed, timed out, or was never reachable in the first place — the three explanations above cover the great majority of what turns up. Contact support with those two details rather than a description of the symptom alone.

Frequently asked questions

Is a 502 always the host's fault?

No, and it is one of the more misunderstood errors for exactly that reason. A 502 means a gateway — which can be a CDN, a proxy, or a component inside the hosting stack — received an invalid response from whatever sits behind it. That "behind it" component is very often the site's own application code crashing or a plugin exhausting resources, not the platform itself.

How is a 502 different from a 504?

A 504 means the gateway gave up waiting for a response — the upstream was too slow. A 502 means the gateway got a response and it was invalid or broken, or got no response at all because the upstream process died outright. Slow versus broken is the distinction.

Related reading