Error 502 Bad Gateway and 504 Gateway Timeout: what they mean and where to look

The short answer: in both cases there is one server in front (the one that receives the visitor) and another behind (the one that runs PHP or the application). With a 502 the one behind gave an invalid answer or stopped answering; with a 504 it took too long. Almost always it is a slow, stopped or starved program, not a domain problem.

What the site needs

What What to do
502 Bad Gateway The server behind failed or closed the connection halfway: a PHP process that died, a stopped application, a wrong port in the proxy.
504 Gateway Timeout The server behind took longer than allowed: a heavy program, a slow query, a request to an outside service that does not answer.
One page, or the whole site? One page: usually that program. The whole site: the service behind is stopped or out of resources.
On a VPS The application (Node.js, Python, PHP-FPM) may be stopped, or the proxy points to the wrong port: Nginx in front of a container.
With Cloudflare in front The error may come from Cloudflare when it cannot reach your server: Cloudflare errors 520 to 526.

Getting started

1 Wait a minute and reload. A traffic peak or a restart passes by itself.
2 See whether it is just one page and whether the site answers elsewhere: if so, the problem is that program.
3 Read the error log for that moment: Reading cPanel’s Errors pages.
4 Deactivate plugins or recent changes, one at a time: Disabling plugins.
5 Check the limits: exhausted memory, time and processes give this error: PHP limits and The limits nobody advertises.
6 On a VPS, see whether the service is up: Reading systemctl status.
If the error comes and goes at the same hour, it is load: a heavy scheduled task, a bot crawling the site or a campaign. Measure before raising limits: What makes a difference when the site is slow.
The general picture of error codes is in Website errors explained. For a real peak: Preparing for a traffic spike.

The error persists and you do not know why? Open a ticket with the address, the time and what you were doing.

Open a support ticket

SEE ALSO

Website errors explained: 500, 403, 404, 508 and what to do

Reading the Errors and Raw Access pages in cPanel when a page fails

Disabling plugins, including when you cannot get in

PHP limits: memory, time, upload size and the one that fails silently

The limits nobody advertises: inodes, processes and memory

Nginx as a reverse proxy in front of a container

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from $6.59/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?