---
title: "WordPress 502 and 504 errors behind Nginx: how to debug"
date: "2026-11-26"
updated: "2026-11-26"
excerpt: "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."
tags: ["WordPress", "Nginx", "Debugging", "Hosting"]
draft: false
cover: "/images/blog/wordpress-502-504-nginx-debug.webp"
coverAlt: "Illustration of an Nginx server returning 502 and 504 errors with PHP-FPM status and log lines used to debug a WordPress site"
category: "guide"
tldr: "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."
howToSteps:
  - name: "Read the Nginx error log"
    text: "Check the site's error.log for upstream messages such as connect() failed or upstream timed out, which separate a 502 from a 504."
  - name: "Check that PHP-FPM is running"
    text: "Run systemctl status for your PHP-FPM service and restart it if it has stopped, then find out why it stopped."
  - name: "Look for worker exhaustion"
    text: "If the PHP-FPM log warns that max_children was reached, requests are queuing. Raise pm.max_children only as far as memory allows."
  - name: "Review the timeouts"
    text: "Compare fastcgi_read_timeout in Nginx with max_execution_time and request_terminate_timeout in PHP, and keep them consistent."
  - name: "Find the slow request"
    text: "Enable the PHP-FPM slow log and trace the script that runs long, which is often a plugin, an import or an external API call."
faqs:
  - question: "What is the difference between a 502 and a 504?"
    answer: "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."
  - question: "Should I just increase the timeout?"
    answer: "Only after you know why requests are slow. A larger timeout hides the problem and ties up workers longer."
  - question: "Why do 502 errors come and go?"
    answer: "Intermittent 502s usually mean PHP-FPM workers are running out or crashing under load, often from memory pressure."
  - question: "Can a plugin cause a 504?"
    answer: "Yes. A plugin that calls a slow external API or runs a heavy import can hold a request past the timeout."
---

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.
