How to Fix the 502 Bad Gateway Error

A 502 Bad Gateway error means one server, acting as a middleman, asked another server for your page and got back a broken or empty reply. It usually points to a busy or crashed server, a bad CDN setting, or a timed-out request — not your computer. Most 502s clear on their own within minutes.

Key takeaways

  • A 502 is a server-side signal: one server got an invalid response from another server behind it.
  • Many 502s are temporary — refresh after a minute before doing anything else.
  • Common causes are an overloaded or crashed server, a PHP-FPM timeout, or a misconfigured CDN/proxy like Cloudflare.
  • Rule out your own device first: try another browser, network, or device.
  • If the whole server keeps throwing 502s, it is a host-side problem — contact your hosting provider.

"My site suddenly shows 502 Bad Gateway"

You loaded your website (or someone else's) and instead of the page you got a short message: 502 Bad Gateway. Sometimes the page also shows the name of the web server software or a cloud provider's branded error screen. Nothing you clicked caused it, and refreshing may or may not help.

This guide is for beginners and small business owners who just want their site back. The good news: a 502 is almost always fixable, and often it fixes itself. It is one of a family of server errors — the "5xx" codes that mean something went wrong on the server side, not with your browser. We will find out what is causing yours and clear it step by step.

How to tell what's causing your 502

Start with two quick questions. First, is the site down for everyone, or just you? Paste the address into a free "is it down right now" checker, or ask a friend on a different network to open it. If it loads fine for them, the problem is on your side — your browser, connection, or local DNS cache. If it is down for everyone, the problem is on the server or the layer in front of it.

Second, does turning off the CDN change anything? A CDN (Content Delivery Network) is a network of servers that sits in front of your site to speed it up and shield it. Services like Cloudflare are CDNs. If you temporarily pause the CDN and the site loads, the proxy layer was the culprit. If it still 502s, the origin server itself is the problem.

Is it your site or your host? If only some pages 502 (or it clears when you disable a plugin), the cause is usually your site — a slow plugin, theme, or PHP timeout you can fix yourself. If every site on the server is down, or 502s keep coming back with no change from you, the cause is the host or the proxy in front of it — and that is where you should focus.

What's actually happening

Modern websites rarely run on a single machine. Your request usually passes through a gateway — a server that receives your request and forwards it to another server for the real work. A gateway is like a receptionist who takes your question and walks it to the right department.

The server that does the real work is the upstream server — the "department" further back that generates the actual page. On a typical hosting stack, a web server such as Nginx acts as the gateway and passes the request to an upstream application server (for example, PHP running your WordPress site).

A 502 Bad Gateway happens when the gateway forwards your request but gets back an answer it cannot understand — an empty reply, a dropped connection, or garbage instead of a proper page. The gateway is working, so it does not stay silent; it reports "the server behind me gave me a bad answer." That message is the 502.

The most common reasons the upstream reply goes bad are: (1) the origin server is overloaded or briefly down, so it cannot answer in time; (2) PHP-FPM crashed or the request timed outPHP-FPM (FastCGI Process Manager) is the service that runs your PHP code, and if it dies or takes too long, the gateway gets nothing usable; (3) a faulty CDN or reverse-proxy configuration, such as a wrong Cloudflare setting; (4) a firewall or security rule blocking the request between the two servers; (5) a DNS problem pointing traffic to the wrong or unreachable place; or (6) a plugin or theme causing a long-running request that never finishes before the timeout.

Your options at a glance

Work from the outside in — easiest and most likely first, deeper server checks last. Here is the order and who each check helps.

If the 502 is… Likely cause What to try
Gone after a refresh A brief server hiccup Nothing — it was temporary
Only on your device Browser cache or local DNS Clear cache, flush DNS, try another network
Gone when CDN is off CDN/proxy misconfiguration Review Cloudflare/CDN settings
On some pages only Slow plugin/theme or PHP timeout Deactivate plugins, raise PHP timeouts
Across the whole server Host-side outage or overload Contact your hosting provider

A 502 and its cousins overlap, so it helps to know the neighbours. A 500 Internal Server Error means the server hit a code-level fault; the 503 and 504 errors mean the server is unavailable or the upstream took too long to answer. If your symptom does not match a 502, those guides may fit better.

How to fix a 502 Bad Gateway, step by step

  1. Refresh the page. Wait about a minute, then reload. Many 502s are caused by a momentary server overload and vanish on their own. Use Ctrl+R (Windows) or Cmd+R (Mac).
  2. Check whether the site is down for everyone. Paste the address into a free "is it down right now" service, or ask someone on another connection to open it. This tells you instantly whether the problem is site-wide or just yours.
  3. Clear your browser cache. An old cached copy of the error can stick around. Clear your browsing data for the last hour, or use a hard reload with Ctrl+Shift+R (Windows) / Cmd+Shift+R (Mac).
  4. Try the site in an incognito/private window. A private window ignores your extensions and most cached data. If the page loads there, the problem is on your side, not the server.
  5. Flush your local DNS cache. Your computer stores recent DNS lookups, and a stale entry can misroute you. On Windows run ipconfig /flushdns; on macOS run sudo dscacheutil -flushcache. Then reload the site.
  6. Test on another device or network. Open the site on your phone using mobile data instead of Wi-Fi. If it works there, your original connection or router is the issue, not the website.
  7. Temporarily pause or disable your CDN/proxy. If you use Cloudflare or another proxy, switch it to a bypass or development mode (or set the affected records to "DNS only"). If the 502 disappears, a proxy-layer setting is the cause — review it before turning the CDN back on.
  8. Check the server error log. In your hosting control panel (or via your host's dashboard), open the site's error log. Look for entries mentioning upstream, timeout, or PHP-FPM around the time of the error — they point straight at the failing piece.
  9. Deactivate plugins or switch to a default theme. If your site runs WordPress, deactivate all plugins, then reload. If the 502 clears, reactivate them one at a time to find the plugin causing a long-running request. Test with a default theme too, to rule out the theme.
  10. Increase your PHP timeouts. If a script is simply slow, raise max_execution_time and max_input_time so requests finish before the gateway gives up. See our PHP settings guide for exactly where to change these safely.
  11. Contact your host if it persists across the whole server. If every site is down or the 502 keeps returning with no change from you, the fault is server-side. Send your host the time of the error and any log lines — they can check upstream services and server load for you.

Common mistakes to avoid

Watch out: before you deactivate plugins, edit theme files, or raise PHP limits, make a full backup. A quick website backup means that if a change makes things worse, you can restore in minutes instead of scrambling.

The three mistakes that waste the most time with a 502 are:

  • Assuming it is always the host's fault. Many 502s come from your own site — a heavy plugin, a slow query, or a PHP timeout. Blaming the host first sends you down the wrong path.
  • Forgetting the CDN layer. If Cloudflare or another proxy sits in front of your site, it can produce a 502 even when your origin server is perfectly healthy. Always test with the proxy paused.
  • Not checking your own device or network first. People rebuild servers over a 502 that was really a stale local DNS entry or a flaky router. Confirm the site is down for others before touching anything on the server.

How to prevent 502 errors in future

You cannot stop every hiccup, but you can make 502s rare. Three habits do most of the work.

  • Keep your CDN configuration clean. Use a reliable CDN and change proxy settings deliberately. Note what you change so you can undo a setting that starts causing 502s.
  • Give the server enough resources. If your traffic has grown, make sure your plan has the memory and processing power to match. Chronic overload is a leading cause of repeated 502s.
  • Avoid runaway plugins and slow code. Remove plugins you do not use, keep the rest updated, and watch for any that run long background tasks. Pair sensible PHP timeout settings with lean code so requests finish comfortably.

Finally, keep a recent backup on hand. It will not prevent a 502, but it turns any future troubleshooting into a low-risk task.

Frequently asked questions

What does 502 Bad Gateway mean in simple terms?

It means one server passed your request to another server and got back a broken or empty reply. The first server is a middleman (a gateway); the second does the real work (the upstream server). "Bad Gateway" is the middleman's way of saying "the server behind me gave me a bad answer."

Is a 502 error my fault?

Usually not directly. A 502 is generated on the server side, not by your browser. It can be caused by your own site's plugins or PHP timeouts if you run the site, but as a visitor there is nothing you did to trigger it. Your only job is to rule out your device, then wait or report it.

Can Cloudflare cause a 502?

Yes. Cloudflare is a CDN and proxy that sits in front of your site, so it can return a 502 when it cannot get a good reply from your origin server — or when a Cloudflare setting is misconfigured. Temporarily pausing Cloudflare (or setting records to "DNS only") tells you whether the proxy layer is the cause.

Is a 502 Bad Gateway error permanent?

Rarely. Most 502s are temporary and clear within minutes as the overloaded or restarting server recovers. A 502 that keeps returning points to an ongoing cause — a crashing service, a bad proxy setting, or an under-resourced server — which is fixable once you find it using the steps above.

How do I know if a 502 is my host's problem?

Check whether it is site-wide and persistent. If every site on the server is down, or the 502 keeps coming back with no change from you and the error log mentions upstream or PHP-FPM failures, it is a host-side issue. Contact your hosting provider with the error time and log lines so they can investigate.

What's the difference between a 502 and a 504 error?

Both involve a gateway and an upstream server. A 502 means the upstream sent back an invalid reply; a 504 means the upstream sent back no reply in time (a timeout). Our 503 and 504 guide covers timeouts in detail.

Summary

A 502 Bad Gateway is a server-side message: a gateway server got an invalid reply from the upstream server behind it. Start by refreshing and ruling out your own device, then check whether the CDN or your site's plugins and PHP timeouts are to blame. If the whole server keeps returning 502s, it is a host-side problem worth reporting. Most of the time you will be back online within minutes.

Next step: before you make any server-side change, set up a reliable safety net with our guide to backing up and restoring your website — so every fix you try is easy to undo.

References

Bitrich777 Hosting Team
About the author

The editorial team behind the Bitrich777 Hosting Help Center — practical, tested guides on web hosting, WordPress, servers, DNS, SSL, email, security and migration. Every walkthrough is reproduced on a live host before it is published.

Spotted an error? Tell us