Troubleshooting

502 Bad Gateway Error: causes and fix

Quick answer: A 502 means a gateway such as a CDN or Nginx got a bad response from the site's main server. Visitors should wait and refresh; site owners should check PHP and server services.

General Medium About 15 min 8 steps Internet, Browser & Website Errors
502 Bad Gateway Error: causes and fix — A young man talks on the phone at night in a coworking space.

Symptoms

  • A plain "502 Bad Gateway" page, often with "nginx" underneath
  • A Cloudflare "Bad gateway" page showing which link failed between Browser, Cloudflare and Host
  • The site works sometimes and shows 502 at other times

Likely causes

  1. The site's origin server (PHP-FPM, the app or the database) has crashed or is down.
  2. The server is overloaded and doesn't respond in time.
  3. There's a connection problem or SSL mismatch between the CDN and the host.
  4. The site is being updated or its services are restarting, so the error is temporary.
  5. A temporary issue with your own network or browser cache (rare).

Step-by-step fix

Start from step one and test again after each step; steps go from the simplest to the most technical.

  1. Wait and refresh

    A 502 is almost always a server-side problem and often clears up on its own within minutes. Press F5; if it still fails, try again in 5 to 10 minutes.

  2. Confirm it's not just you

    Open the site in an Incognito window or on mobile data, or check it on a site-status checker such as Down for Everyone or Just Me. If it's down for everyone, all you can do is wait or let the site's support know.

  3. Clear the site's cache and cookies

    If only you get the 502, clear that site's cache and cookies in "Delete browsing data" and restart your router once.

  4. Site owners: check services

    First confirm the server is running. On shared hosting, contact your host; on a VPS, log in over SSH and check PHP-FPM, the web server and the database (for example systemctl status php-fpm nginx mysql). Restart any service that has stopped.

  5. Site owners: read the error log

    Open the Nginx or Apache error log (usually error.log in the server's log folder, or "Errors" in the control panel). Messages like "upstream timed out" or "connect() failed" mean PHP or the app isn't responding or is too slow.

  6. Site owners: check resources and plugins

    If the 502s started after a new plugin, theme or traffic spike, disable that change and check RAM and CPU usage. Raise PHP limits such as max_execution_time, memory_limit and the number of PHP-FPM workers in your hosting panel, or upgrade your plan.

  7. Site owners: check CDN settings

    In Cloudflare, confirm the DNS records point to the correct host IP and the SSL mode matches your server's setup. To diagnose, temporarily switch the record to "DNS only" (gray cloud); if the site loads, the problem is between the CDN and your host, otherwise the host itself is down.

  8. Site owners: contact your host

    If services and logs look normal but the error continues, send your host the exact time, the page URL and the log lines. Sometimes the problem is the server node, its firewall or overall load, which only they can see.

If nothing worked

If you're a visitor and still get a 502 after an hour, the site really is having problems; check the service's official channels (social accounts, support email) for updates. Don't pay anyone who offers to "open the site for you".

If you own the site and 502s keep coming back, either your hosting resources are too low or a heavy plugin or piece of code keeps crashing PHP; keep the logs and take that evidence to your host, or upgrade your plan.

Don't blindly edit server files based on random tutorials, and always back up before changing anything.

Frequently asked questions

What's the difference between 502, 503 and 504 errors?

A 502 means the gateway got an invalid response from the origin server (for example, PHP crashed). A 503 means the server is deliberately unavailable for now (maintenance or overload). A 504 means the origin didn't answer in time. All three are server-side, and the fixes for site owners are similar.

Why does the 502 appear on only one page?

That page probably does heavy processing, such as a complex report or search, and takes longer than the server's timeout. The site owner should lighten or cache that page, or raise the PHP and web server timeouts.

Related problems

Terms used on this page

More Internet, Browser & Website Errors problems

All problems Internet, Browser & Website Errors

Last updated:

Type a few letters of the topic; spelling variants are treated as the same.