Uptime monitoring from outside your own infrastructure that alerts you the minute your site goes down — upordown.online

When a site behind Cloudflare breaks, visitors don't see your Nginx error page. They see a Cloudflare page with a big number on it and a little diagram: browser (working), Cloudflare (working), host (error). That diagram is the most useful thing on the page, because it tells you the problem is between Cloudflare and your server, not in the visitor's browser.

The numbers themselves are less friendly. 520, 521, 522, 523 and 524 are not standard HTTP status codes. Cloudflare made them up to describe where in the conversation with your origin things went wrong. Once you know which step each code means, the fix is usually quick.

The short version

Code What Cloudflare saw Usual cause
520 The origin answered with something empty or invalid App crashed mid-response, headers too large, origin reset the connection
521 The origin refused the TCP connection Web server not running, or firewall rejecting Cloudflare's IPs
522 TCP connection timed out Firewall silently dropping packets, server overloaded, wrong origin IP
523 The origin isn't reachable at all Wrong DNS record for the origin, routing problem
524 Connected fine, but no response within 100 seconds A slow request: long report, stuck database query

If you remember one thing: 521 and 522 are about getting connected, 520 and 524 are about what happened after you connected.

Step 0: test the origin directly

Every one of these errors is easier to debug if you skip Cloudflare and talk to your server yourself. You need the origin IP (it's the A record in the Cloudflare DNS dashboard, the one with the orange cloud). Then:

curl -sv --resolve www.example.com:443:203.0.113.10 https://www.example.com/ -o /dev/null 

--resolve makes curl connect to your origin IP while still sending the right hostname and SNI, so you get exactly the virtual host Cloudflare would get. Run it from a machine outside your server's network. Running it on the server itself skips the firewall, which is often the very thing that's broken.

What curl prints tells you which of the codes you're dealing with, often before you look at any logs.

Fixing 521: "Web server is down"

Cloudflare tried to open a TCP connection and the origin actively refused it. Your curl will say Connection refused.

Check, in order:

  1. Is the web server running? systemctl status nginx (or apache2, caddy, your Node process). A crashed process or a failed config reload is the most common cause. nginx -t will tell you if the config is the reason it won't start.
  2. Is it listening on the right port? ss -tlnp | grep -E ':80|:443'. If Cloudflare's SSL mode is "Full" or "Full (strict)", Cloudflare connects to port 443. If your server only listens on 80, every request gets refused.
  3. Is something rejecting Cloudflare's IPs? Fail2ban, a host firewall or a hosting provider's "DDoS protection" sometimes decides that a few IPs sending thousands of requests are an attack. Those IPs are Cloudflare. Allowlist the published ranges from cloudflare.com/ips.

Fixing 522: "Connection timed out"

Cloudflare sent a SYN and got nothing back before giving up. Curl will hang and then say Connection timed out. The difference from 521 matters: refused means something answered "no", timeout means nothing answered at all.

Usual causes:

  • A firewall that drops instead of rejects. Cloud security groups and iptables -j DROP rules both behave this way. If you recently tightened a security group to "only my office IP", you locked Cloudflare out.
  • The origin IP in DNS is wrong. After a server migration it's easy to leave the A record pointing at an old box that's been switched off. The old IP might not even exist anymore.
  • The server is so overloaded it can't accept connections. Check load and connection backlog: ss -s, uptime, and whether the accept queue is full. This one comes and goes, which makes it look like a flaky network.

A 52x error is exactly the kind of outage your own server's logs won't show. upordown.online checks the public URL from outside, through Cloudflare, so you get alerted when visitors see the error page, not when someone finally emails you.

Fixing 520: "Web server is returning an unknown error"

520 is the catch-all. The connection worked, but the response was empty, malformed or cut off. Curl against the origin might show Empty reply from server, a reset, or a response that looks fine to curl but isn't fine to Cloudflare.

Things to check:

  • The application crashed mid-request. PHP-FPM workers dying, a Node process hitting an unhandled exception after writing the status line, an out-of-memory kill. The app logs and dmesg | grep -i kill will show it.
  • Headers are too big. Cloudflare has a limit on total response header size (the current number is in its 5xx troubleshooting docs). An app that sets dozens of cookies, or a giant Content-Security-Policy, can exceed it. Count with curl -sI ... | wc -c.
  • Keep-alive mismatch. If the origin closes idle connections faster than Cloudflare expects, Cloudflare may reuse a connection the origin has just closed. Make sure keep-alive is enabled on the origin and that its idle timeout isn't shorter than Cloudflare's (Nginx's default keepalive_timeout is 75 seconds); that usually stops the intermittent 520s.

Fixing 524: "A timeout occurred"

Cloudflare connected, sent the request, and waited 100 seconds (on most plans) without getting a response. Your origin didn't fail. It was just too slow.

The fix is almost never "raise the timeout". It's to stop doing slow work inside a web request:

  • Find the slow URL in your access log. Log $request_time in Nginx if you don't already.
  • Long exports, reports and bulk operations should start a background job and return immediately, with a status page the browser can poll.
  • If you need a long-running endpoint for an internal tool, put it on a hostname that isn't proxied (grey cloud) so the 100-second limit doesn't apply.

523: "Origin is unreachable"

Less common: Cloudflare can't find a route to your origin at all. Check that the DNS record points to a real public IP, not a private 10.x or 192.168.x address copied from a server's network config. If you use Cloudflare Tunnel, check that cloudflared is running and connected.

Why your monitor might have missed it

If your only uptime check hits the origin directly, it will stay green through a 521 caused by a firewall that blocks Cloudflare but not your monitor. If it hits the Cloudflare URL but only checks for "any response", the Cloudflare error page itself can count as a response. Check the public URL, expect a 200, and look for a keyword that only appears on your real page.

Related reading

Which one bites you most often? For me it's been the 522 after someone "tidied up" a security group. Curious whether anyone has a reliable way to catch the intermittent 520s.