Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

Cloudflare answers 524 because the PHP page takes too long

Your error

Error 524: a timeout occurred2 more lines it can show

Fix it fast

Most common cause, 1 of 4

One request does a long job while the visitor waits

Split it in two.

Fix
<?php
// export-start.php: store the job, reply straight away
$id = bin2hex(random_bytes(8));
$db->prepare('INSERT INTO export_jobs (id, status) VALUES (?, ?)')
   ->execute([$id, 'queued']);
http_response_code(202);
header('Content-Type: application/json');
echo json_encode(['id' => $id, 'status_url' => '/export-status.php?id=' . $id]);

// export-worker.php runs with the php CLI, takes queued rows,
// builds the file and sets status to 'done'.
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Error 524 means Cloudflare reached your server but got no HTTP response within its Proxy Read Timeout, 125 seconds by default. The origin is slow, not down. Time the URL against the server directly, then move long jobs into a background worker with status polling, or fix a full PHP-FPM pool or a hung outside call.

  • Applies to Cloudflare proxy (all plans), PHP-FPM 8.3, nginx 1.24, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Time the failing URL against your own server so Cloudflare is out of the way. Run it on the server itself when the firewall only admits Cloudflare. A total above 125 seconds proves the origin is the slow side; a short total points at the causes further down.

Run this first
curl -s -o /dev/null -w '%{http_code} %{time_total}\n' --resolve example.com:443:127.0.0.1 https://example.com/reports/export

Then find the line you see in this list and open its fix:

  1. 1One request does a long job while the visitor waitsAlways the same URL (export, import, bulk email, large report), and the direct timing is longer than 125 seconds
  2. 2The task has to stay one long requestA staff-only tool or a bulk upload that you cannot rebuild as a background job right now
  3. 3All PHP-FPM workers are busy, so requests sit in the queue524 on many URLs during busy hours, including pages that are normally quick
  4. 4A worker hangs on a slow query or an outside API callRequest times jump around for the same URL; the server is not busy, yet a request now and then never finishes

What does the error look like?

  • Error 524: a timeout occurred
  • Error 524 on an export, import or report page, roughly two minutes after the click
  • The same page finishes when it is opened on the server without Cloudflare

Cause 1 of 4

One request does a long job while the visitor waits

You seeAlways the same URL (export, import, bulk email, large report), and the direct timing is longer than 125 seconds

Check

A 524 that always hits the same heavy URL means that one request runs longer than the 125 seconds Cloudflare waits for a response. Cloudflare names a long-running origin process, such as a big data query, as a cause. Run the action once on a small data set and once on a large one; when the time grows with the data, the job is too big for a single page load.

Check
curl -s -o /dev/null -w '%{time_total}\n' --resolve example.com:443:127.0.0.1 "https://example.com/reports/export?month=1"
curl -s -o /dev/null -w '%{time_total}\n' --resolve example.com:443:127.0.0.1 "https://example.com/reports/export?year=2026"

Fix

Split it in two. The first request only records the job and replies at once; a worker started by cron or a systemd timer does the heavy part; the page asks a small status URL every few seconds and offers the file when it is ready. Cloudflare's own advice for large HTTP processes is this kind of status polling.

Fix
<?php
// export-start.php: store the job, reply straight away
$id = bin2hex(random_bytes(8));
$db->prepare('INSERT INTO export_jobs (id, status) VALUES (?, ?)')
   ->execute([$id, 'queued']);
http_response_code(202);
header('Content-Type: application/json');
echo json_encode(['id' => $id, 'status_url' => '/export-status.php?id=' . $id]);

// export-worker.php runs with the php CLI, takes queued rows,
// builds the file and sets status to 'done'.

Verify

The start request returns in a second or two, the status URL moves from queued to done, and the browser never shows 524 on that action again.

Verify
curl -s -o /dev/null -w '%{http_code} %{time_total}\n' -X POST https://example.com/export-start.php

Confirmed by developers.cloudflare.com

Did this fix it?

Cause 2 of 4

The task has to stay one long request

You seeA staff-only tool or a bulk upload that you cannot rebuild as a background job right now

Check

A 524 on a tool that must finish inside one long request can only go away if Cloudflare stops proxying that hostname or, on Enterprise, waits longer. First see whether the hostname is proxied: a proxied record is answered with Cloudflare anycast addresses, a DNS-only record with your server's own address.

Check
dig +short admin.example.com

Fix

Cloudflare suggests serving requests that regularly pass 125 seconds from a subdomain set to DNS-only (grey cloud) in its DNS app. Such a record exposes your origin address to anyone who queries it, and Cloudflare can no longer cache or protect that traffic, so lock the tool down with a login and the server firewall. Enterprise zones can instead raise the timeout to as much as 6,000 seconds, with a Cache Rule or the proxy_read_timeout zone setting; set it one second above the value you need, because the 524 can arrive about a second late.

Verify

dig now returns your server's own address for the tool's hostname, and the long task completes there without a Cloudflare error page.

Verify
dig +short admin.example.com

Confirmed by developers.cloudflare.com

Did this fix it?

Cause 3 of 4

All PHP-FPM workers are busy, so requests sit in the queue

You see524 on many URLs during busy hours, including pages that are normally quick

Check

A 524 spread over many ordinary URLs at peak times fits an overloaded origin, which Cloudflare also lists as a cause; with PHP-FPM it means every worker is occupied and new requests wait. Turn on the FPM status page for the pool and route that single path to FPM from localhost only (the PHP manual shows an Apache example). Two counters matter: listen queue is the number of requests waiting for a free process, and max children reached shows whether the pool ever hit its ceiling.

Check
; /etc/php/8.3/fpm/pool.d/www.conf
pm.status_path = /fpm-status

sudo systemctl reload php8.3-fpm
curl -s "http://127.0.0.1/fpm-status?json"

Fix

pm.max_children caps how many requests the pool serves at the same moment. When the queue grows at peak and max children reached is above zero, either the cap is too low for the traffic or the workers are tied up by slow requests. Look for those slow requests first (next cause), then raise the cap only as far as the server's memory allows.

Fix
; /etc/php/8.3/fpm/pool.d/www.conf
pm.max_children = 20

sudo systemctl reload php8.3-fpm

Verify

At the next busy hour, listen queue stays at 0 and the 524 errors on ordinary pages stop.

Verify
curl -s "http://127.0.0.1/fpm-status?json"

Confirmed by php.net

Did this fix it?

Cause 4 of 4

A worker hangs on a slow query or an outside API call

You seeRequest times jump around for the same URL; the server is not busy, yet a request now and then never finishes

Check

A 524 that comes and goes on one URL while the server is quiet points at a worker stuck waiting on a database query or an outside API. Let FPM write a PHP backtrace for any request that runs longer than a threshold; the top frames name the function the worker is stuck in, such as a database execute() call or curl_exec().

Check
; /etc/php/8.3/fpm/pool.d/www.conf
request_slowlog_timeout = 10s
slowlog = /var/log/php8.3-fpm-slow.log

sudo systemctl reload php8.3-fpm
sudo tail -n 40 /var/log/php8.3-fpm-slow.log

Fix

Give every outgoing curl call a connect limit and an overall limit, so a dead API fails fast instead of holding the worker. A query that shows up in the backtrace belongs in the background job from the first cause, or needs to be made faster.

Fix
<?php
$ch = curl_init('https://api.example.com/rates');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$body = curl_exec($ch);

Verify

The slow log stays empty for that URL, and the timing from the first step is well under the limit.

Verify
sudo tail -n 40 /var/log/php8.3-fpm-slow.log

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Send any action that can run past a minute to a queue with a status URL, never into the page load.
  • Keep request_slowlog_timeout set in production pools, so a stuck worker always leaves a backtrace.
  • Set CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT on every curl handle your PHP code opens.
  • Read max listen queue on the FPM status page after each traffic peak, before visitors meet a 524.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, stegnor.com, Spotless Laundry.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.