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.

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
- The site's origin server (PHP-FPM, the app or the database) has crashed or is down.
- The server is overloaded and doesn't respond in time.
- There's a connection problem or SSL mismatch between the CDN and the host.
- The site is being updated or its services are restarting, so the error is temporary.
- 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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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

403 Forbidden Error
A 403 means the server understood the request but refuses access. Visitors should clear cookies and try another connection; site owners should check permissions and .htaccess.

404 Not Found Error
A 404 means the site is up but that page doesn't exist. Visitors should check the URL and use the site's search; site owners should re-save permalinks and check redirects.

WordPress 500 Internal Server Error
A 500 error means the server hit a problem running your site without saying why. A broken .htaccess file, a faulty plugin or low PHP memory are the usual causes; check your host's error log first.

WordPress Allowed Memory Size Exhausted Error
PHP used up the memory allowed to build a page. Add define('WP_MEMORY_LIMIT', '256M'); to wp-config.php first; if that doesn't help, raise memory_limit in your hosting control panel.
Terms used on this page
More Internet, Browser & Website Errors problems
All problems Internet, Browser & Website Errors
Last updated: