502 Bad Gateway, 503 or 504 Gateway Timeout on a WordPress site
All three mean the same shape of problem. A server in front (your host's web server, a proxy, or Cloudflare) passed the visitor's request to PHP, which runs WordPress, and did not get a usable answer back. 502: PHP answered with nothing or crashed. 503: nothing was free to take the request, or the site said "not now" on purpose. 504: PHP took longer than the front server was willing to wait. The fix is to find which of those happened and why, not to raise the waiting time until the error goes quiet. Below is a rebuilt example (not a real client), how to find the stuck part, and the fix for each cause.
The order page
What the checks found
First: find which hop failed
Read the error page. A Cloudflare page draws three boxes - Browser, Cloudflare, Host - and marks the one that failed. "Host: Error" means your server, not Cloudflare. A plain page that says "nginx", "LiteSpeed" or "Apache", or carries your host's logo, comes from the host's front server. "Briefly unavailable for scheduled maintenance. Check back in a minute." is WordPress itself sending a 503 during an update.
Note when and where. Every page, all the time, points at PHP crashing or the server being down. Only at busy times points at too few PHP workers. Only one action - checkout, an import, saving a long page, a report in the admin - points at one slow request. The same time every day points at a scheduled job.
Read the logs. Your host's error log (cPanel: Metrics > Errors, or the PHP error log in the file manager) and the WordPress debug log (WP_DEBUG_LOG in wp-config.php) name a fatal error or a crashed plugin. For "workers busy" ask the host's support for the PHP-FPM log: a line saying max_children was reached is the answer. Your host's status page tells you if the whole server is down, which is theirs to fix.
The causes, most common first
PHP crashing (502, every page). Often right after a plugin update or a PHP version change in the host panel: one plugin uses something the new PHP no longer has, and the PHP process dies instead of showing an error. Switch PHP back to the version that worked (host panel, one click), or rename the newest plugin's folder in wp-content/plugins to switch it off, then update that plugin or replace it before moving PHP forward again.
All PHP workers busy (503, or 502/504 at busy times). The host gives each site a fixed number of PHP processes. Uncached pages, a slow plugin, or bots crawling every page use them all, and new visitors wait in line or are turned away. Some hosts call this "508 Resource Limit Reached". Turn on page caching so most visits never reach PHP, find the slow pages, and block the worst crawlers; buy a bigger plan only if real visitors still fill it.
One request too slow (504 at one action). A product import of thousands of rows, a checkout waiting on a payment or shipping service that is not answering, a big report, or a database query on an unindexed table runs past the front server's limit (often 60 seconds; Cloudflare waits about 100 and then shows its own "Error 524 - a timeout occurred", which is the same problem). Run imports in small batches or from the command line (WP-CLI), set a short timeout on the outside service so checkout fails fast and clearly, and fix the slow query rather than the limit.
A scheduled job at the wrong time (502/504 at the same hour). A backup plugin zipping gigabytes of uploads, an image optimiser working through the library, or a stuck WordPress cron task holds the workers and the disk. WP Crontrol lists every scheduled task and its next run. Move heavy jobs to the night, use the host's own backups instead of a plugin's, and leave old video or ZIP files out of the backup.
A leftover maintenance file (503 after an update). An update that was interrupted leaves .maintenance in the site's main folder, and WordPress keeps answering 503 with "Briefly unavailable". Delete that one file and the site is back; then finish the update that stopped.
The server or the host is down (502/503/504 everywhere, nothing in your logs). The host's status page or support confirms it. Nothing in WordPress will fix it; if it happens often, that is the reason to move host.
What you see, what it means, what it needs
What you see
Usual cause
Usual fix
Cloudflare 502/504 page with "Host: Error"
Your server, not Cloudflare
Host error log and status page; then the rows below
502 on every page after a plugin or PHP update
PHP crashing on one plugin
PHP back one version, or rename that plugin's folder
503 or 508 when traffic is high, fine at night
All PHP workers busy
Page caching, slow pages fixed, bad bots blocked
504 on checkout, an import, or one admin report
One request past the timeout
Batches or WP-CLI; short timeout on the outside service; fix the query
502/504 at the same time every day
Backup, optimiser or a stuck cron job
WP Crontrol; move it to the night; host backups
"Briefly unavailable for scheduled maintenance"
Leftover .maintenance file
Delete the file, finish the update
Everything down, nothing in your logs
Host outage
Host status page and support
What not to do
Do not just raise the timeouts to five minutes. The slow job still holds a PHP worker the whole time, so the 504 turns into 503s for everyone else.
Do not keep pressing "update" or "import" again while the error shows. Each try starts another slow request on top of the one still running.
Do not reinstall WordPress or restore last month's backup first. A gateway error comes from a crash, a queue or a slow job, and a restore throws away this month's orders and posts without touching any of those.