Your WordPress site, or one admin screen, spins for about a minute and then shows “504 Gateway Timeout” or “504 Gateway Time-out”. That page comes from a server in front of WordPress that waited for PHP to answer, ran out of patience and gave up, while PHP was often still working behind it. Raising the timeout rarely fixes anything, because the request that hung will hang again. Find which layer sent the 504 and which WordPress request it was waiting on. Fix what that request was stuck on, then set the timeouts so the layer closest to WordPress gives up first.
What you’ll accomplish
- Tell from the status code, headers and timing which layer gave up: Cloudflare, a load balancer, nginx or Apache.
- Pull the exact URL and timestamp of the hung request from that layer’s log.
- See the PHP function the request was stuck in, using the PHP-FPM slow log.
- Check the four usual WordPress causes: an outside API call, a cron task, a slow database query and a long import or backup.
- Order the timeouts so PHP stops a runaway request before the proxy does, and know when a longer timeout is the right call.
Prerequisites
You need SSH access with sudo or root on the server. You also need WP-CLI, so you can run WordPress jobs from the shell; wp --info prints its version if it is installed. Examples use a Debian or Ubuntu server with nginx in front of PHP-FPM 8.2. PHP-FPM (FastCGI Process Manager) is the service that keeps a pool of PHP worker processes ready to run WordPress. File paths differ on other systems, and the steps say where.
On a cPanel server, the front end is Apache, or cPanel’s own nginx proxying to Apache, and PHP-FPM pools are generated from YAML files. The slow-log step links to the cPanel method. If your server runs LiteSpeed, PHP runs through LSAPI (LiteSpeed Server Application Programming Interface), LiteSpeed’s own way of talking to PHP, so there is no PHP-FPM slow log. Steps 1, 2, 4 and 6 still apply.
How it works
A WordPress request passes through several layers before PHP runs. Each layer starts its own clock when it hands the request inward, and the first clock to run out decides what the visitor sees.
Three details in that picture catch most people out.
First, PHP’s own limit, max_execution_time, usually never fires. On Linux it counts only the time PHP spends computing. Time spent waiting for a database answer or an outside API is not counted, so a request stuck on a slow API can wait for minutes without reaching a 30-second limit.
Second, PHP-FPM’s limit, request_terminate_timeout, is off by default. With the value 0, nothing inside the server stops a hung request, so the proxy in front is the first to give up.
Third, the 504 does not stop PHP. The worker keeps running until its call returns or it next tries to send output. If visitors retry, each retry ties up another worker. A handful of hung requests can then fill the pool, and one slow URL turns into errors on every page.
The status code also tells you who gave up. When your origin (the server behind Cloudflare) is slow, Cloudflare does not answer with 504. It shows Error 524, “A timeout occurred”, after 100 seconds on non-Enterprise plans. A 504 that reaches you through Cloudflare almost always comes from your own server.
Step-by-step
1. Reproduce it and time it. Pick a URL that fails, or the admin action that fails, and time it from your terminal.
curl -s -o /dev/null -w "%{http_code} %{time_total}s\n" https://www.example.com/shop/
Example output:
504 60.087s
The time points at the layer. About 60 seconds matches the default of nginx, of Apache’s Timeout and of the AWS Application Load Balancer idle timeout. About 100 seconds with code 524 is Cloudflare. About 300 seconds usually means Apache on a cPanel server, where Timeout is set higher. You change it in WHM under Apache Configuration » Global Configuration. If it returns 200 quickly, the problem is intermittent, so read the logs from the time it failed in step 3.
2. Read who sent the 504. Look at the headers and the page title.
curl -s -D - -o /tmp/504.html https://www.example.com/shop/ | grep -iE "^HTTP|^server|^cf-ray"
grep -o "<title>[^<]*</title>" /tmp/504.html
Example output:
HTTP/2 504
server: nginx
<title>504 Gateway Time-out</title>
| What you see | Who gave up | Where to look next |
|---|---|---|
524, server: cloudflare, page says “A timeout occurred” | Cloudflare, after 100 s | Your server’s logs, steps 3 to 5 |
504, server: cloudflare, your server’s own 504 page | Your server, passed through Cloudflare | Repeat with the origin IP, below |
504, server: awselb/2.0 | AWS load balancer idle timeout | The servers behind it |
504, server: nginx, title “504 Gateway Time-out” | nginx | /var/log/nginx/error.log |
504, server: Apache, title “504 Gateway Timeout” | Apache’s proxy to PHP-FPM | Apache’s error log |
A cf-ray line means the response passed through Cloudflare. When Cloudflare is in front, it replaces your server header. Send the same request straight to your server to see what it says. Replace 203.0.113.10 with your server’s IP address.
curl -s -o /dev/null -w "%{http_code} %{time_total}s\n" --resolve www.example.com:443:203.0.113.10 https://www.example.com/shop/
Example output:
504 60.112s
A 504 around 60 seconds here confirms the origin gave up before Cloudflare’s 100 seconds ran out.
3. Get the URL and timestamp from the error log. nginx writes one line per timeout. In its wording, the “upstream” is the service it passed the request to, here PHP-FPM.
sudo grep "upstream timed out" /var/log/nginx/error.log | tail -n 3
Example output:
2026/10/09 10:41:07 [error] 2214#2214: *58119 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 198.51.100.23, server: www.example.com, request: "POST /wp-admin/admin-ajax.php HTTP/2.0", upstream: "fastcgi://unix:/run/php/php8.2-fpm.sock:", host: "www.example.com", referrer: "https://www.example.com/wp-admin/post.php?post=4127&action=edit"
“While reading response header” means nginx handed the request to PHP-FPM and got nothing back in time. Usually a worker took it and hung, which is what the rest of this article chases. It can also mean the request sat in PHP-FPM’s queue because every worker was busy, which the troubleshooting section covers. “While connecting to upstream” means nginx could not even open a connection to PHP-FPM.
On Apache the matching line contains AH01075 and (70007)The timeout specified has expired. The log is /usr/local/apache/logs/error_log on cPanel, /var/log/apache2/error.log on Debian and /var/log/httpd/error_log on RHEL-family systems.
4. Find which WordPress requests hang most. Count the 504s per URL in the access log. Field 9 is the status code in the standard “combined” log format that nginx and Apache both use. On cPanel, use the domain’s file in /var/log/apache2/domlogs/.
sudo awk '$9 == 504 {print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head
Example output:
38 /wp-admin/admin-ajax.php
7 /wp-json/wc/v3/orders?per_page=100
3 /wp-cron.php?doing_wp_cron=1791563412.2017660140991210937500
1 /shop/
admin-ajax.php is the endpoint plugins and the dashboard use for background requests. The action name travels in the POST body, so the log cannot tell you which plugin, but step 5 can. /wp-json/ is the REST API, the URL-based interface that the block editor, WooCommerce and mobile apps call. wp-cron.php is WP-Cron, the WordPress scheduler that runs due tasks during normal visits. A plain page URL means the theme or a plugin is slow while building that page.
5. Catch the stuck function with the PHP-FPM slow log. The slow log writes the PHP call stack of any request still running after a threshold. On cPanel, set it through the YAML files as shown in enabling the PHP-FPM slow log on cPanel. On Debian or Ubuntu, add two lines to the pool file /etc/php/8.2/fpm/pool.d/www.conf.
slowlog = /var/log/php8.2-fpm-slow.log
request_slowlog_timeout = 10s
Test the configuration, then reload.
sudo php-fpm8.2 -t && sudo systemctl reload php8.2-fpm
Example output:
[09-Oct-2026 10:52:33] NOTICE: configuration file /etc/php/8.2/fpm/php-fpm.conf test is successful
Repeat the failing action, then read the newest trace.
sudo tail -n 12 /var/log/php8.2-fpm-slow.log
Example output:
[09-Oct-2026 10:54:18] [pool www] pid 31877
script_filename = /var/www/html/wp-admin/admin-ajax.php
[0x00007f3a2c813f40] curl_exec() /var/www/html/wp-includes/Requests/src/Transport/Curl.php:205
[0x00007f3a2c813e60] request() /var/www/html/wp-includes/Requests/src/Requests.php:469
[0x00007f3a2c813d70] request() /var/www/html/wp-includes/class-wp-http.php:409
[0x00007f3a2c813c90] post() /var/www/html/wp-includes/http.php:189
[0x00007f3a2c813bf0] wp_remote_post() /var/www/html/wp-content/plugins/acme-crm-sync/includes/class-sync.php:88
[0x00007f3a2c813b40] push_contact() /var/www/html/wp-includes/class-wp-hook.php:324
[0x00007f3a2c813a80] apply_filters() /var/www/html/wp-includes/class-wp-hook.php:348
[0x00007f3a2c8139f0] do_action() /var/www/html/wp-includes/plugin.php:517
[0x00007f3a2c813960] do_action() /var/www/html/wp-admin/admin-ajax.php:192
Read it from the top. The top line is where the request is stuck, and the first path under wp-content names the plugin or theme responsible. Here it is a plugin that syncs contacts to a CRM (customer relationship management) service. curl_exec() means an outbound HTTP call. mysqli_query() under wpdb means a database query. A file function such as fread() or file_get_contents() points at slow storage, such as a network share.
6. Check the four usual WordPress causes. Step 5 usually names one of them. Confirm it here.
An outside API call. WordPress gives outbound requests a 5-second timeout by default, but plugins can raise it. Run this search from the WordPress root folder. It lists plugins that pass 20 seconds or more.
grep -rnE "'timeout'\s*=>\s*([2-9][0-9]|[0-9]{3})" wp-content/plugins/ | head
Example output:
wp-content/plugins/acme-crm-sync/includes/class-sync.php:84: 'timeout' => 120,
That plugin will outlast a 60-second proxy whenever its remote service is slow.
A cron task. List the scheduled events, then run the due ones yourself with timing.
wp cron event list --fields=hook,next_run_relative,recurrence
Example output:
+---------------------------+--------------------+------------+
| hook | next_run_relative | recurrence |
+---------------------------+--------------------+------------+
| acme_crm_full_sync | now | 1 hour |
| wp_version_check | now | 12 hours |
| wp_scheduled_delete | 6 hours 41 minutes | 1 day |
| delete_expired_transients | 6 hours 41 minutes | 1 day |
+---------------------------+--------------------+------------+
now means the event is due. Run the due events yourself and watch how long each takes.
wp cron event run --due-now
Example output:
Executed the cron event 'wp_version_check' in 0.412s.
Executed the cron event 'acme_crm_full_sync' in 74.218s.
Success: Executed a total of 2 cron events.
WP-CLI has no web timeout, so the 74-second task finishes here. During a visit it would hit the proxy limit.
A slow database query. While the hang is happening, list queries running longer than 5 seconds.
sudo mysql -e "SELECT id, user, time, state, LEFT(info, 90) AS query FROM information_schema.PROCESSLIST WHERE command <> 'Sleep' AND time > 5 ORDER BY time DESC;"
Example output:
+-------+---------+------+--------------+--------------------------------------------------------------------------------------------+
| id | user | time | state | query |
+-------+---------+------+--------------+--------------------------------------------------------------------------------------------+
| 81233 | wp_shop | 47 | Sending data | SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_postmeta ON ( wp_posts.I |
+-------+---------+------+--------------+--------------------------------------------------------------------------------------------+
An empty result means nothing was slow at that moment. A long wp_postmeta join usually comes from a filter or search plugin on a large catalogue.
A long import, export or backup. If the slow URL belongs to an import screen or a backup plugin, the job is too long for a web request by design. Run it from WP-CLI or a background queue instead. For Action Scheduler, the background queue that WooCommerce uses, wp action-scheduler run processes pending jobs with no web timeout. It ends with a line such as Success: 18 scheduled tasks completed.
7. Fix the cause. Match the fix to what step 5 or 6 found.
Slow outside API. Cap the wait so the plugin fails fast instead of holding the page. Save this as wp-content/mu-plugins/cap-crm-timeout.php, with your plugin’s API host in place of api.example-crm.com. Files in mu-plugins load on every request and cannot be switched off from the dashboard.
<?php
add_filter( 'http_request_args', function ( $args, $url ) {
if ( str_contains( $url, 'api.example-crm.com' ) ) {
$args['timeout'] = min( (float) $args['timeout'], 10 );
}
return $args;
}, 10, 2 );
Repeat the failing action. It should now fail or finish within about 10 seconds, and the plugin may show its own error instead of the 504. Then ask the plugin’s vendor to move the sync into a background job. A plugin that calls cURL directly instead of wp_remote_post() ignores this filter.
Heavy cron task. Stop WP-Cron from running during visits by adding define( 'DISABLE_WP_CRON', true ); to wp-config.php. Then run due events from the system scheduler every 5 minutes with wp cron event run --due-now, so long tasks run outside any web timeout.
Slow query. Fix the plugin setting or search feature that builds it, or add the missing index. Restarting MySQL only clears it until the next visit runs the same query.
Long job in the browser. Run it with WP-CLI or a queue, as in step 6.
8. Order the timeouts so PHP stops first. Once the cause is fixed, make the innermost layer give up first. PHP-FPM then logs the exact script and frees the worker, while the proxy in front only logs a URL. Give each outer layer at least 5 seconds more than the layer inside it.
| Layer | Setting | Value |
|---|---|---|
| Outbound HTTP calls | timeout argument | 10 s or less |
| PHP-FPM | request_terminate_timeout | 50s |
| Apache, when it sits between nginx and PHP | Timeout or ProxyTimeout | 55 |
| nginx | fastcgi_read_timeout or proxy_read_timeout | 60s |
| Load balancer | idle timeout | 65 s |
| Cloudflare | fixed at 100 s | nothing to change |
Add the line to the same pool file, test and reload as in step 5.
request_terminate_timeout = 50s
From now on a request that hangs past 50 seconds is stopped by PHP-FPM, and its log, /var/log/php8.2-fpm.log, names it.
Example output:
[09-Oct-2026 11:07:45] WARNING: [pool www] child 32094, script '/var/www/html/wp-admin/admin-ajax.php' (request: "POST /wp-admin/admin-ajax.php") execution timed out (50.004120 sec), terminating
The visitor now sees a 502 Bad Gateway at 50 seconds instead of a 504 at 60, because nginx reports the closed connection. That is expected. You gain a log line that names the script, and the worker is free again. For the nginx side of these values, see tuning nginx upstream timeouts.
Validate it works
Repeat the failing request and check that it finishes well inside the limits.
curl -s -o /dev/null -w "%{http_code} %{time_total}s\n" https://www.example.com/shop/
Expected: 200 and a time of a few seconds. Then confirm no new timeouts arrive over the next day.
sudo grep -c "upstream timed out" /var/log/nginx/error.log
sudo grep -c "execution timed out" /var/log/php8.2-fpm.log
Both counts should be unchanged when you run them again tomorrow.
Troubleshooting
Many different URLs time out at once. Every PHP-FPM worker was busy, so new requests waited in the queue until nginx gave up. Look for server reached pm.max_children setting in /var/log/php8.2-fpm.log. The busy workers are usually stuck on the hung requests from step 5, so fix those before adding workers.
The error log says “while connecting to upstream”. nginx could not open a connection to PHP-FPM at all. Run sudo systemctl status php8.2-fpm and expect active (running). If it is running, check that the fastcgi_pass address in your nginx site matches the listen line in the pool file.
The slow log stays empty, but 504s continue. Check the request reaches this pool. On cPanel each domain has its own pool and log. On LiteSpeed there is no PHP-FPM, so read /usr/local/lsws/logs/error.log and use step 6.
You raised a timeout and nothing changed. Time the failure again. If it still stops at 60 seconds, another layer has the same limit, often a load balancer or cPanel’s nginx in front of Apache. If it stops at 100 seconds, Cloudflare is cutting it off, and no setting on your server changes that.
When this is the wrong approach
- Every page is slow, not one request. If the server load is high and all pages crawl, this is a capacity problem, not a single hung request. Look at CPU, memory and PHP worker counts first.
- The work is long on purpose. A report or import that needs five minutes should not run in a web request. Move it to WP-CLI or a queue. If an admin page truly must wait longer, raise the limit only for
/wp-admin/. The PHP-FPM limit applies to the whole pool, so sites that need both use a second pool for admin traffic. - You do not control the layer that answered. On managed hosting, send the provider the URL, the time and the status code from step 1.
For the exact meaning of each setting, see the nginx FastCGI module reference and the PHP manual’s PHP-FPM configuration page.
Running WordPress that earns real money deserves an operator — Stack Harbor runs that stack under Enterprise WordPress; book an infrastructure consult.