All blogs

WordPress 502 and 504 errors behind Nginx: how to debug

A 502 means Nginx got no valid answer from PHP-FPM, a 504 means it waited too long. How to read the logs, tell them apart and fix the usual causes.

A 502 means Nginx could not get a valid response from PHP-FPM, usually because it is down or crashing. A 504 means PHP took longer than the timeout. Read the Nginx error log first, then the PHP-FPM log, and check workers and timeouts.

A 502 and a 504 look alike in the browser, but they point at different problems, and the Nginx error log tells you which one you have. Start there rather than restarting services at random.

What the two codes mean

  • 502 Bad Gateway. Nginx passed the request to PHP-FPM and got no valid answer back. PHP-FPM is down, crashed, or the socket is not reachable.
  • 504 Gateway Timeout. Nginx passed the request on and PHP-FPM did not answer before the timeout. The service is alive, but the request is too slow.

Read the Nginx error log

On most servers it is /var/log/nginx/error.log, or a per-site log. The wording decides the next step:

connect() to unix:/run/php/php8.3-fpm.sock failed
upstream timed out (110: Connection timed out)

The first line is a 502 cause: PHP-FPM is not accepting connections. The second is a 504 cause: it accepted the request and did not finish in time.

If PHP-FPM is down

Check the service, using the name your PHP version uses:

systemctl status php8.3-fpm

Restarting it is fine as a first aid, but find out why it stopped. A kernel out-of-memory kill is a common reason, and dmesg or the system journal will show it.

If workers run out

The PHP-FPM log may warn that pm.max_children was reached. Then requests queue behind a fixed number of workers. Raising the value helps only as far as memory allows: each worker uses real RAM, and too many will push the server into swap and make everything slower.

Check the timeouts line up

Several limits interact: fastcgi_read_timeout in Nginx, max_execution_time in PHP, and request_terminate_timeout in the PHP-FPM pool. If one is much shorter than the others, it decides when the request dies. Raising a timeout is a stopgap, not a fix, because a slow request still holds a worker.

Find the slow request

Turn on the PHP-FPM slow log for the pool. It records a stack trace for any request that runs longer than the limit you set, and it usually names the plugin or function responsible: an import, a backup job, or a call to a slow external API. Fix or schedule that work, and the 504s go away.

Frequently asked questions

What is the difference between a 502 and a 504?

A 502 means Nginx got no valid reply from the upstream, such as PHP-FPM being down. A 504 means the upstream did not reply before the timeout.

Should I just increase the timeout?

Only after you know why requests are slow. A larger timeout hides the problem and ties up workers longer.

Why do 502 errors come and go?

Intermittent 502s usually mean PHP-FPM workers are running out or crashing under load, often from memory pressure.

Can a plugin cause a 504?

Yes. A plugin that calls a slow external API or runs a heavy import can hold a request past the timeout.