Question
How to fix Cloudflare 524 timeout occurred
524 means Cloudflare connected to origin but the response took too long.
What to do first
- Find slow URLs with timing checks.
- Inspect DB and upstream dependencies for bottlenecks.
- Move long jobs to async processing.
If you are not sure whether your failure is timeout or malformed response, check Cloudflare 520 vs 524 quick split.
First-response command block
# Compare latency: proxied path vs direct origin
time curl -s -o /dev/null -w '%{http_code}\n' https://yourdomain.com/your-heavy-endpoint
time curl -s -o /dev/null -w '%{http_code}\n' --resolve yourdomain.com:443:ORIGIN_IP https://yourdomain.com/your-heavy-endpoint
# Host pressure check
top -bn1 | head -n 20
vmstat 1 5
iostat -xz 1 3
# Service and app logs
sudo journalctl -u caddy --since '30 minutes ago' --no-pager | tail -n 120
sudo journalctl -u nginx --since '30 minutes ago' --no-pager | tail -n 120
How to read the result
- Direct origin is also slow: application/database bottleneck.
- Direct origin is fast, proxied path is slow: edge-to-origin network or TLS path issue.
- Only one endpoint times out: heavy sync task, query explosion, or upstream dependency.
Low-risk fix order
- Reduce endpoint work inside request cycle (query optimization, pagination, cache).
- Move long tasks to queue/background workers.
- Add cache headers for repeat-read pages where safe.
- Re-test for 30-60 minutes on the same endpoint before wider changes.
Recovery signal
A stable recovery pattern is: target endpoint returns mostly 200/301/304, and 524 stops repeating on your top public pages.
If the issue is connection refusal (not timeout), continue with Cloudflare 521 web server down.