Intermittent Cloudflare 5xx errors — a 520 here, a 522 twenty minutes later, then an hour of silence — are harder than constant failures because every check you run after the fact comes back clean. The causes are nearly always one of a short list: security tooling banning a subset of Cloudflare’s IP ranges, origin capacity that only runs out at spikes, a keepalive mismatch between edge and origin, or trouble at one specific Cloudflare data centre while others work. The method that catches them is evidence collection first — Ray IDs, timestamps, and a continuous probe on both paths — configuration guessing never.
What’s happening
Each visitor request goes browser → a nearby Cloudflare data centre (the “colo”) → your origin. Intermittency creeps in because that path is not one path: different visitors hit different colos, edge connections to your origin come from different IP ranges and get reused at different rhythms, and your origin’s capacity varies minute to minute. A partial failure anywhere shows up as errors that “come and go”: if fail2ban banned three of Cloudflare’s ranges but not the rest, only requests routed through those ranges fail. If PHP-FPM runs out of workers for ninety seconds during a cron burst, only requests in that window fail — as 520s, 524s, or 522s depending on where exactly they queued, which is why the error code itself looks inconsistent. The per-code mechanics are in Cloudflare’s status code documentation; this article is about the correlation work the individual code pages cannot do for you.
Every Cloudflare error page carries a Ray ID, and its trailing letters name the colo that served it (-YYZ, -YUL, and so on). That single detail turns “random errors” into a pattern: all failures from one colo is a very different diagnosis than failures spread across all of them.
What to check first
- Collect three or more Ray IDs with exact timestamps from affected users or your logs. Without them you are debugging folklore.
- Do the failures cluster in time? On the hour or at :15 marks suggests cron, backups, cache warmers, or log rotation on the origin.
- Do they cluster by colo? Same trailing letters in every Ray ID means a path problem between one Cloudflare site and you.
- cloudflarestatus.com — check the colos your traffic uses before blaming the origin.
- Origin logs at the failure timestamps. A request that appears there rules the network path out; one that never arrived rules it in.
How to verify
Identify which colo serves you, then run a paired probe until the error reproduces:
# Which Cloudflare data centre are you hitting? (colo= in the output)
curl -s https://example.com/cdn-cgi/trace
# Poll edge and origin side by side; run until the next incident
while true; do
d=$(date -u +%FT%TZ)
e=$(curl -s -o /dev/null -w '%{http_code} %{time_total}' https://example.com/)
o=$(curl -s -o /dev/null -w '%{http_code} %{time_total}' --resolve example.com:443:203.0.113.10 https://example.com/)
echo "$d edge=[$e] origin=[$o]"
sleep 30
done | tee -a cf-5xx-watch.log
The matrix this produces is the whole diagnosis: edge fails while origin succeeds means the fault sits on the Cloudflare-to-origin path (bans, routing, keepalive); both fail together means the origin itself is folding. Then check the usual intermittent culprits on the origin:
# Are any Cloudflare ranges banned right now? (full list: https://www.cloudflare.com/ips/)
grep -E '173\.245|141\.101|108\.162|162\.158|104\.1[6-9]|172\.6[4-9]|198\.41' \
/etc/csf/csf.deny 2>/dev/null
fail2ban-client banned 2>/dev/null | head
# Capacity spikes: PHP-FPM hitting its ceiling, in bursts
grep "max_children" /var/log/php*-fpm.log | tail -20
# Conntrack exhaustion drops connections only at peaks
sysctl net.netfilter.nf_conntrack_count net.netfilter.nf_conntrack_max
dmesg -T | grep -i 'conntrack\|out of memory' | tail
# What runs at the failure timestamps?
grep -r . /etc/cron.d/ 2>/dev/null | grep -v '^#' | head -20
crontab -l
The fix
- Partial bans on Cloudflare ranges — the classic intermittent producer: fail2ban or CSF banned a few edge IPs because origin logs show “attacks” from them (which are just proxied visitors). Unban, allowlist all published Cloudflare ranges, and fix the root cause by restoring real client IPs at the web server — the full setup is in restoring the real client IP behind Cloudflare. Until real IPs are restored, every ban decision your tooling makes is wrong.
- Capacity that only fails at spikes —
max_childrenwarnings, load spikes, or conntrack pressure lining up with the error timestamps. Move the cron/backup work off-peak, raise the specific ceiling that is being hit, and fix the slow queries that make workers pile up in the first place. - Keepalive mismatch — the origin closes idle connections faster than the edge expects to reuse them, so a fraction of requests land on a connection dying at that exact moment and surface as sporadic 520s/52xs. Raise the origin’s keepalive window above the edge’s reuse horizon —
keepalive_timeout 120s;in Nginx (orKeepAliveTimeoutin Apache, which also needsKeepAlive On) — so the origin is never the side hanging up first. - One misbehaving colo — every bad Ray ID ends in the same letters while other regions are clean. Check the status page for that facility; if it is green, the path between that colo and your host is the suspect, and the evidence bundle (Ray IDs, timestamps,
mtrfrom your side) is what both Cloudflare support and your provider will ask for. Nothing in your dashboard fixes a path problem. - The origin’s front door, not the origin — a provider load balancer, DDoS appliance, or NAT layer that drops or resets a small percentage of connections. If the paired probe shows direct-to-origin failures too, but server logs never see the requests, the box in front of the server is eating them; that is a hosting ticket with your probe log attached.
When this fix is wrong
If errors stopped on their own before you changed anything, resist closing the incident as “transient” — an unexplained intermittent 5xx has simply not recurred yet, and the probe loop should keep running for days, not minutes. Equally, if the interval smells like a pattern you cannot see locally (weekday mornings, one country’s peak hours), the trigger may be a traffic mix change rather than anything on your server. For a constant 522, this whole article is the wrong tool — use the 522 origin-path walkthrough for the cPanel stack or the generic 522 guide instead.
Common mistakes
- Debugging with a single after-the-fact test that comes back 200 and concluding the user imagined it.
- Restarting services as the response to every recurrence, which destroys the evidence (worker states, connection tables) you needed.
- Collecting error screenshots without Ray IDs or timestamps — the two fields that make correlation possible.
- Allowlisting the one Cloudflare IP seen in today’s logs instead of the published ranges, guaranteeing next month’s repeat.
Prevention
- Restore real client IPs everywhere Cloudflare fronts a server, on day one — it prevents the entire ban-driven class of intermittency.
- Keep an external probe on both the proxied hostname and the origin IP permanently, so the edge-vs-origin matrix already exists when an incident starts.
- Schedule heavy jobs (backups, exports, cache warms) off-peak, and log their start/stop times somewhere your future self can correlate against.
- Alert on PHP-FPM
max_childrenwarnings and conntrack table usage above ~80%. - Automate the Cloudflare IP-range allowlist update in every firewall layer.
Intermittent 5xxs give up their cause the moment you have paired probes and three Ray IDs — everything before that is guessing. If the error keeps coming back, the problem usually sits between Cloudflare and the origin — Stack Harbor operates that whole path under Managed Cloud Ops, and you can book an infrastructure review.