Troubleshooting

How to fix a 502 Bad Gateway error

A 502 Bad Gateway error means one server got an invalid reply from another behind it. Learn what causes 502 errors on hosting, CDNs and VPS servers, and how to fix them.

BeginnercPanel hosting2 min read

A 502 Bad Gateway error means a server acting as a gateway or proxy – a CDN, a load balancer, or the web server in front of PHP – received an invalid response from the server behind it. The site's front door is open, but the backend that builds the page failed. Most 502 errors are short-lived; repeated ones need fixing at the backend.

Before you start

  • Note which page shows the 502 and what the error page looks like. A Cloudflare-branded page means Cloudflare is the gateway; a plain page means your server.
  • cPanel access, or SSH for a VPS.

Common causes of a 502 Bad Gateway

  • PHP processes crashing or being killed mid-request.
  • Resource limits reached, so PHP workers are unavailable.
  • A backend service stopped – PHP-FPM, Node.js app, or an upstream server on a VPS.
  • CDN cannot read the origin's response (see Cloudflare 520 and 502 pages).
  • Firewall blocking the proxy's requests to the backend.

How to fix it as a visitor

  1. Wait a minute and reload – a 502 during a restart clears quickly.
  2. Try a private window and another network to rule out local proxies.

How to fix it as a site owner on cPanel

  1. Check the error log in cPanel → Metrics → Errors for PHP fatal errors or crashes at the same time. See PHP errors and error_log.
  2. Check resource usage in cPanel → Metrics → Resource Usage. Entry process or memory faults mean the account is at its limit; see 508 resource limit reached.
  3. Disable the last plugin or change you made, then retest.
  4. Switch PHP version in Select PHP Version if a PHP extension crashes.
  5. For Node.js apps, restart the application in Setup Node.js App and check its log; see Node.js app errors.

How to fix it on a VPS

  1. Check the backend: systemctl status php8.3-fpm (adjust the version) or your app service.
  2. Read the web server error log for "upstream prematurely closed" or "connect() failed".
  3. Make sure socket paths and ports in the web server config match the backend. Our PHP-FPM 502 guide goes deeper.

Tip: If you use Cloudflare, test the origin directly with curl and --resolve. If the origin works, the 502 is between Cloudflare and your server, not inside it.

FAQ

Is a 502 error my fault?

Not as a visitor. For site owners it usually means a backend crash or limit.

How is 502 different from 504?

502: the backend gave a bad answer. 504: the backend took too long to answer.

Does a 502 hurt SEO?

Brief ones do not. Hours of 502s can cause pages to drop temporarily.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.