
A WordPress 504 error on an Nginx VPS usually means one thing. Nginx waited on PHP-FPM (or another upstream), and the response didn’t arrive in time.
The delay often comes from slow database queries, too few PHP workers, low timeout values, or a VPS that’s out of CPU/RAM.
The goal in this guide is simple: find the choke point, fix it, and confirm the improvement with logs and timings.
What you need before you start
- A WordPress site running on Nginx + PHP-FPM (Ubuntu 24.04/26.04, Debian 12/13, AlmaLinux 9/10, Rocky 9/10 all work)
- Root or sudo access to the server
- Your domain pointed to the VPS (or a hosts-file override for testing)
- Ability to reproduce the timeout (traffic spike, heavy page, slow admin action, etc.)
If the 504s happen because your plan is short on CPU or RAM, tuning will only go so far.
Moving to a properly sized VPS is often the biggest single improvement.
HostMyCode’s HostMyCode VPS is a good fit when you want predictable resources and root access for tuning.
Confirm it’s a WordPress 504 error (and not Cloudflare or a load balancer)
First, confirm who is generating the 504. A CDN or proxy can return a 504 even when your origin server is healthy.
-
Run a direct request to your origin server. If you use Cloudflare, bypass it by resolving the domain to your VPS IP:
curl -I --resolve example.com:443:YOUR_VPS_IP https://example.com/Verify: Check the
serverheader and response. If you still get 504 and the server header points to Nginx, the problem is likely on your VPS. -
Check whether Cloudflare is producing the timeout:
curl -I https://example.com/ | egrep -i 'server:|cf-ray|x-cache|via|date'Hint: If you see
server: cloudflareand you can’t reproduce the issue with--resolve, focus on edge-to-origin connectivity or an origin that’s responding too slowly.
From here on, assume the 504 is happening on the VPS (Nginx waiting for PHP-FPM).
Capture proof: correlate Nginx, PHP-FPM, and system pressure
Resist the urge to “just raise timeouts.” Take a minute to capture evidence so you fix the real limiter.
1) Watch the Nginx error log during a 504
On Ubuntu/Debian, logs are usually under /var/log/nginx/.
On AlmaLinux/Rocky, it’s often the same if you installed Nginx from standard repos.
sudo tail -f /var/log/nginx/error.log
What you’re looking for:
upstream timed out (110: Connection timed out) while reading response header from upstreamconnect() failed (111: Connection refused) while connecting to upstreamrecv() failed (104: Connection reset by peer) while reading response header from upstream
upstream timed out means Nginx waited and PHP-FPM didn’t respond fast enough.
It can also mean PHP-FPM was blocked and couldn’t return headers.
Connection refused usually means PHP-FPM is down, restarting, or too overloaded to accept new connections.
2) Check PHP-FPM status and recent errors
First, confirm the PHP-FPM service name.
Common examples include php8.2-fpm, php8.3-fpm, and php8.4-fpm.
sudo systemctl list-units | grep -E 'php.*fpm'
sudo systemctl status php8.3-fpm --no-pager
Next, locate the PHP-FPM logs (paths vary by distro):
- Ubuntu/Debian:
/var/log/php8.3-fpm.logor/var/log/php8.3-fpm/www-error.log - Alma/Rocky: often under
/var/log/php-fpm/
sudo ls -lah /var/log | grep -i php
sudo find /var/log -maxdepth 2 -type f -iname '*fpm*' -o -iname '*php*' | head
sudo tail -n 200 /var/log/php8.3-fpm.log
What you’re looking for:
server reached pm.max_children setting(workers exhausted)child exited on signal 9 (SIGKILL)(OOM killer or memory limit issues)- Slow script warnings if enabled
3) Check for CPU/RAM/disk bottlenecks at the time of the 504
uptime
free -h
df -h
sudo vmstat 1 5
sudo iostat -xz 1 3 2>/dev/null || true
If free -h shows almost no available memory and swap is churning, PHP workers will stall or get killed.
If disk is pegged (high %util in iostat), both the database and PHP file I/O slow down fast.
If you suspect storage is part of the problem, this HostMyCode tutorial is directly useful: Disk Full on VPS: Find Large Files and Free Space Safely.
Fix #1: Tune Nginx FastCGI timeouts the right way
Timeouts rarely cause the underlying slowness. They mainly decide what users experience.
You either get a 504 now, or a slow response that eventually completes.
Set them high enough to avoid “false” 504s during legitimate slow operations (imports, cache warms).
Keep them low enough that a dead upstream fails quickly.
Locate your WordPress server block
Common locations:
- Ubuntu/Debian:
/etc/nginx/sites-available/and symlinked into/etc/nginx/sites-enabled/ - Alma/Rocky: often under
/etc/nginx/conf.d/
sudo nginx -T | sed -n '1,120p'
Tip: Search the output for your server_name to find the active config file.
Apply FastCGI timeout adjustments
Edit your site config and update the PHP location block:
sudo nano /etc/nginx/sites-available/example.com
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
# Timeouts: reduce “false 504” during heavy admin tasks
fastcgi_connect_timeout 10s;
fastcgi_send_timeout 60s;
fastcgi_read_timeout 60s;
# Buffers: avoids header issues on big responses
fastcgi_buffer_size 32k;
fastcgi_buffers 16 16k;
}
Good starting point: a 60s read timeout for most WordPress sites.
If you have known long-running tasks (large WooCommerce exports, backups kicked off from wp-admin), 120s can be a temporary band-aid.
Treat it as a signal to optimize.
Verify:
sudo nginx -t
sudo systemctl reload nginx
Reproduce the issue.
If the 504s stop but requests now hang near the timeout, you’ve confirmed PHP is slow.
Next step: PHP-FPM capacity.
Fix #2: Stop PHP-FPM worker exhaustion (pm.max_children)
On busy WordPress installs, 504s often come from a queue problem.
A traffic spike fills every PHP-FPM worker, new requests wait in line, and Nginx times out.
Check if PHP-FPM is hitting its process limits
Search for max_children warnings:
sudo grep -R "max_children" -n /var/log | grep -i fpm | tail -n 20
If you repeatedly see server reached pm.max_children, you need more capacity, faster requests, or both.
Calculate a safe pm.max_children for your RAM
Every PHP-FPM worker uses memory.
On typical 2026 WordPress stacks (PHP 8.3/8.4 plus real-world plugins), a busy worker often lands around 60–150 MB RSS.
Don’t guess—measure on your server while it’s under load.
-
Check worker memory use:
ps -ylC php-fpm8.3 --sort:rss | awk 'NR==1{print} NR<=12{print}'If your process name differs (common on RHEL-family), use:
ps aux --sort=-rss | grep -E 'php-fpm|php-fpm:' | head -n 15 -
Pick a conservative average (say 100MB per worker). Then reserve memory for MySQL/MariaDB, Redis, and the OS.
Example: On a 2GB VPS, you might keep ~900MB for PHP workers. 900MB / 100MB ≈ 9 workers, so set
pm.max_childrento 8–10—not 30.
Edit the PHP-FPM pool config
Typical pool file locations:
- Ubuntu/Debian:
/etc/php/8.3/fpm/pool.d/www.conf - Alma/Rocky:
/etc/php-fpm.d/www.conf(PHP version may vary)
sudo nano /etc/php/8.3/fpm/pool.d/www.conf
A solid baseline for WordPress on a small-to-mid VPS:
; Dynamic mode is usually fine for WordPress
pm = dynamic
pm.max_children = 10
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 6
; Prevent requests from hanging forever inside PHP
request_terminate_timeout = 60s
; Helps recycle leaky plugin code over time
pm.max_requests = 500
Common mistake: cranking pm.max_children up on a small VPS.
You don’t get more throughput—you get OOM kills and instability.
Verify:
sudo systemctl restart php8.3-fpm
sudo systemctl status php8.3-fpm --no-pager
Re-test under load.
You want fewer 504s and fewer max_children events.
Fix #3: Add PHP slow logs so you can see what’s actually stalling
Raising limits can hide symptoms. It doesn’t fix the workload.
Slow logs show which code path is dragging—plugin, theme, or core call.
Enable PHP-FPM slowlog in the pool
Edit the same pool file:
sudo nano /etc/php/8.3/fpm/pool.d/www.conf
Add the following (adjust paths if needed):
request_slowlog_timeout = 5s
slowlog = /var/log/php8.3-fpm-slow.log
Create the log file and set permissions (Ubuntu/Debian typically run FPM as www-data):
sudo touch /var/log/php8.3-fpm-slow.log
sudo chown www-data:adm /var/log/php8.3-fpm-slow.log
sudo chmod 640 /var/log/php8.3-fpm-slow.log
Restart PHP-FPM:
sudo systemctl restart php8.3-fpm
Trigger the problem and inspect the slow log
sudo tail -f /var/log/php8.3-fpm-slow.log
What you’ll see: stack traces pointing at plugin files, theme functions, or heavy WordPress calls.
If one plugin dominates, disable it temporarily and re-test.
For WooCommerce, watch whether slow traces line up with checkout spikes, admin reports, or scheduled actions.
Fix #4: Reduce upstream latency with Redis object cache (WordPress-side speedup)
Many timeouts under load come from repeated database lookups.
Redis won’t rescue broken code, but it often cuts query overhead enough to keep PHP workers available.
HostMyCode has an end-to-end guide for this stack. Use it once you’ve stabilized the basics: Set Up Redis for WordPress With Nginx and PHP-FPM.
Verification target: after enabling Redis, average PHP execution time should drop.
You should also see fewer PHP-FPM workers pinned at 100% CPU during bursts.
Fix #5: Check database saturation (MariaDB/MySQL) before you blame Nginx
If PHP is waiting on the database, Nginx can still report upstream timeouts.
This is common on small VPS instances and on sites running heavy search/filter/report plugins.
Quick database health checks
sudo systemctl status mariadb --no-pager || sudo systemctl status mysql --no-pager
Log into MySQL/MariaDB:
sudo mysql
Then run:
SHOW GLOBAL STATUS LIKE 'Threads_running';
SHOW GLOBAL STATUS LIKE 'Threads_connected';
SHOW FULL PROCESSLIST;
Interpretation:
- If
Threads_runningstays high (e.g., 20+) on a small server, you’re saturated. - If many queries show “Sending data” or “Copying to tmp table,” you’re dealing with missing indexes or expensive query patterns.
Common mistake: increasing PHP-FPM workers while the database is already at its limit.
That just creates a larger queue.
Optional: add a “real” WordPress page timing log at Nginx
If you want cleaner proof of where time is going, add a log format that records upstream timing.
This is a fast way to separate “PHP is slow” from “network/client is slow.”
Edit /etc/nginx/nginx.conf and inside the http block add:
log_format wp_timing '$remote_addr - $host [$time_local] "$request" '
'$status $body_bytes_sent rt=$request_time '
'uct=$upstream_connect_time urt=$upstream_response_time '
'uht=$upstream_header_time';
Then in your server block:
access_log /var/log/nginx/wp_timing.log wp_timing;
Reload Nginx:
sudo nginx -t && sudo systemctl reload nginx
Verify: Hit a slow page, then inspect:
sudo tail -n 20 /var/log/nginx/wp_timing.log
If rt and urt are both high, upstream/PHP is slow.
If rt is high but upstream is low, look at client/network conditions or response size.
Fix #6: Handle “legitimate long requests” separately (wp-cron, backups, imports)
Some WordPress tasks simply shouldn’t run inside front-end requests.
They can occupy PHP workers for too long.
During a traffic spike, that can turn into a site-wide timeout problem.
Move wp-cron to a real cron job
Disable WP’s pseudo-cron (runs on page hits) and schedule it on the server.
-
Edit
wp-config.php:sudo nano /var/www/example.com/public/wp-config.phpdefine('DISABLE_WP_CRON', true); -
Add a system cron (runs every 5 minutes):
sudo crontab -u www-data -e*/5 * * * * php -d detect_unicode=0 /var/www/example.com/public/wp-cron.php >/dev/null 2>&1
Verify: scheduled actions should process steadily, and front-end traffic spikes should produce fewer “random” slow requests.
Don’t run backups inside wp-admin during traffic
If a backup plugin builds large archives via a web request, timeouts are expected.
Prefer server-side scheduled backups, and make sure restores are tested.
For database-focused sites, this guide is straightforward: Set Up Automatic MySQL Backups With Tested Restores.
Fix #7: Raise limits only where it’s safe (and keep the blast radius small)
Sometimes the server is healthy and the slow path is narrow.
It might be one report screen, one export route, or one admin tool.
In that case, widen timeouts for that route. Avoid loosening the entire site if you don’t have to.
Example: increase timeouts only for wp-admin
Add a dedicated location for admin PHP requests (adjust carefully; don’t duplicate conflicting regex blocks):
location ~* ^/wp-admin/.*\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_read_timeout 120s;
fastcgi_send_timeout 120s;
}
Verify: admin long tasks stop failing, while normal front-end pages still fail quickly if upstream breaks.
Fix #8: Put guardrails around PHP so a few slow requests don’t take down the site
These settings keep slow requests from occupying workers indefinitely.
That reduces the risk of chain-reaction timeouts.
Set a request timeout in PHP-FPM (already done) and match it with WordPress/PHP limits
You set request_terminate_timeout = 60s in the pool config.
Now make sure PHP’s own limits aren’t wildly higher.
Check what PHP is actually using:
php -i | egrep -i 'loaded configuration file|memory_limit|max_execution_time|max_input_time'
For many production WordPress sites, a sensible starting point is:
memory_limit: 256M (raise only with a clear reason)max_execution_time: 60
Common mistake: setting max_execution_time to 300+ seconds to “fix” 504s.
That often turns a short bottleneck into a sustained resource drain.
Fix #9: Verify your fix under load (without guessing)
After each change, validate it with a controlled test.
You’re answering one question: did error rate and latency improve?
Run a quick HTTP load test from your laptop (or a second VPS)
If you have wrk installed:
wrk -t4 -c40 -d30s https://example.com/
Or use ab (ApacheBench) if it’s available:
ab -n 2000 -c 40 https://example.com/
Verify: you want fewer 5xx responses and stable latency. While the test runs, watch:
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/nginx/wp_timing.log
sudo systemctl status php8.3-fpm --no-pager
Common 504 patterns and what they usually mean
- Nginx error: upstream timed out + PHP-FPM log shows max_children: increase PHP capacity (carefully) and reduce per-request work (Redis, caching, plugin cleanup).
- Nginx error: connection refused: PHP-FPM crashed, was restarted, or hit file descriptor limits; check
journalctl -u php8.3-fpm. - PHP-FPM workers OK but MySQL queries pile up: database bottleneck; fix slow queries/indexes or scale CPU/RAM/IO.
- 504 spikes at the same minute marks: cron-triggered tasks or wp-cron storms; move cron to system scheduler.
When it’s time to scale the server (and how to do it cleanly)
If you consistently see CPU saturation, swapping, or heavy disk I/O waits during peak traffic, tuning hits diminishing returns.
A larger VPS (or a dedicated server for busy WooCommerce stores) gives you headroom so PHP-FPM and the database can keep up.
If you’d rather not own patching, tuning, and monitoring, managed VPS hosting is a practical option for WordPress operators who want the site to stay fast without babysitting it.
If your WordPress site is timing out under load, start with reliable compute and enough RAM for PHP-FPM + MariaDB. HostMyCode offers right-sized VPS hosting and hands-off managed VPS hosting designed for production sites where 504s aren’t acceptable.
FAQ
Should I just increase fastcgi_read_timeout to fix a WordPress 504 error?
Only after you’ve confirmed the upstream is slow (Nginx error log plus upstream timings).
If PHP-FPM is out of workers or the database is saturated, larger timeouts mostly hide the real problem.
How do I know if PHP-FPM is the bottleneck?
Check PHP-FPM logs for server reached pm.max_children and check Nginx timing logs for high upstream_response_time.
Also look for pegged CPU with steady traffic.
Why do 504s happen mostly in wp-admin?
Admin actions tend to do heavier work: plugin updates, reports, imports, image regeneration.
Use slow logs to catch the exact function, and consider raising timeouts only for admin routes.
Can Redis really help with 504s?
Yes—when timeouts are driven by repeated database reads and expensive WordPress object lookups.
Redis object caching often reduces DB pressure per request and keeps PHP workers available.
What’s the safest first change if I’m seeing 504s right now?
Collect evidence first (Nginx error log + PHP-FPM log).
Then set a sane fastcgi_read_timeout (like 60s) and right-size pm.max_children to match your RAM.
Both are easy to roll back and easy to verify.
Summary: the reliable way to eliminate 504s
Fixing 504s starts with proving where requests spend time.
Use Nginx errors and upstream timing, enable PHP-FPM slow logs, and then right-size PHP-FPM.
Next, remove unnecessary work (Redis, wp-cron changes, plugin cleanup, database checks).
Once the baseline is stable, scaling becomes a deliberate upgrade instead of an emergency response.
If you want WordPress on infrastructure you can tune (and upgrade without drama), start with a HostMyCode VPS and keep the stack predictable.