Skip to content

Cloudflare error 521: web server is down — or just refusing Cloudflare

Cloudflare shows error 521 when your server refuses its connection. Find out whether the web server is down or a firewall is blocking Cloudflare, and fix the cause.

Cloudflare error 521 appears when visitors reach Cloudflare but your own server, the origin, refuses Cloudflare’s connection. This article is for anyone whose site sits behind Cloudflare and suddenly shows the “Web server is down” page. A refusal means the server answered and said no, so there are only two likely causes. Either nothing is listening on the address and port Cloudflare uses (the web server stopped, or DNS points at the wrong IP), or a firewall on the server is rejecting Cloudflare’s IP ranges. Check first that the web server is running and listening on ports 80 and 443, because that is the cause most of the time.

What’s happening

When a visitor requests a proxied hostname, the browser talks to the Cloudflare edge, and the edge opens its own TCP connection to the origin IP stored in your DNS record. A 521 is generated when that connection attempt gets an explicit refusal — in TCP terms, Cloudflare sends a SYN and receives a RST back. Per the Cloudflare 5xx documentation, that distinguishes it from its siblings: a 522 means silence (packets dropped, nobody answered), a 523 means no route at all. A 521 means a machine at that IP is alive and responding, and it is telling Cloudflare to go away.

Only two things produce that refusal. Either the port is genuinely closed (the web server process is dead, crashed on a bad config reload, or is bound to a different port or interface than the one Cloudflare is dialing). Or something in the path is rejecting the connection by policy: iptables/nftables rules with a REJECT target, a hosting provider’s edge firewall, or security tooling that has banned Cloudflare’s IP ranges because it never learned that all your traffic now arrives from them. The behaviour is the same on every Cloudflare plan; there is no plan-level setting involved.

What to check first

  • Is the web server process up? systemctl status nginx / apache2 / litespeed. A crashed process is the boring, common answer.
  • Did the config reload cleanly last time? A failed reload can leave the old process serving until a restart kills it for good — the 521 starts hours after the actual mistake.
  • Is the DNS record pointing at the right IP? After a migration, Cloudflare may be dialing the old server, which is up but no longer serving your site’s ports.
  • Does the site load when you bypass Cloudflare? If yes, the origin is fine and the refusal is aimed specifically at Cloudflare’s ranges.
  • Any recent firewall or fail2ban activity? Rejections that started suddenly usually correlate with a ban list update.

How to verify

On the origin itself:

# Is anything listening on 443 and 80?
ss -tlnp | grep -E ':443|:80'

# Web server status and the reason it died, if it died
systemctl status nginx --no-pager
journalctl -u nginx --since "1 hour ago" | tail -30

# Test the vhost locally on the loopback
curl -skI https://127.0.0.1/ -H "Host: example.com"

From an external machine (not the origin, not behind the same firewall), using the origin IP from your Cloudflare DNS → Records screen:

# Does the origin accept a TCP connection from outside?
curl -sv --max-time 15 --resolve example.com:443:203.0.113.10 https://example.com/ -o /dev/null

Read the failure mode carefully: Connection refused is 521 behaviour, a hang ending in a timeout is 522 territory. If the external test connects fine but Cloudflare still reports 521, the refusal is selective — the firewall is rejecting Cloudflare’s ranges but not your IP:

# Any REJECT rules in the input chain?
iptables -L INPUT -n --line-numbers | grep -i reject
nft list ruleset | grep -i reject

# CSF: are Cloudflare ranges in the deny list?
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: what is currently banned?
fail2ban-client banned 2>/dev/null | head

The fix

  1. Web server down — start it and read why it stopped. journalctl -u nginx (or the Apache equivalent) almost always names the culprit: a config syntax error, a missing certificate file after a renewal, or an OOM kill. Validate config before restarting (nginx -t, apachectl configtest) so you fix the cause and not just the symptom.
  2. Firewall rejecting Cloudflare’s ranges — allow the published Cloudflare IP ranges (the current list lives at https://www.cloudflare.com/ips/) through every layer: host firewall, CSF/fail2ban, and the hosting provider’s cloud firewall or security groups. Allowing one or two edge IPs you saw in a log is not enough; Cloudflare connects from many ranges and the 521 will come back intermittently.
  3. Wrong origin IP in DNS — the record in Cloudflare points at a previous server that now refuses your ports. Update the A/AAAA record content to the current origin IP. This is the classic post-migration 521.
  4. Server listening on the wrong port or interface — the web server is bound to 127.0.0.1 only, or to a nonstandard port while Cloudflare dials 443/80. Fix the listen directives so the service is reachable on the public interface and standard ports.
  5. Security tooling banned the edge — fail2ban or CSF banned Cloudflare IPs because origin logs show all traffic coming from them. Unban, allowlist the ranges, and restore real visitor IPs at the web server so bans hit actual offenders — see restoring the real client IP behind Cloudflare.

When this fix is wrong

If the 521 appears only for some visitors or some regions while others load fine, a single dead web server is the wrong theory — a fully down origin fails for everyone. Selective failure points at partial firewall rules (some Cloudflare ranges allowed, others not) or at multiple origins behind the record where only one is refusing. And if the origin refuses on 443 but accepts on 80, look at the TLS listener specifically rather than the whole server.

Common mistakes

  • Testing from the origin itself (curl localhost) and concluding “the server is fine” — the refusal may live in a firewall that loopback traffic never crosses.
  • Allowlisting a single Cloudflare IP seen in a log instead of the full published ranges.
  • Restarting the web server without reading why it stopped, guaranteeing a repeat.
  • Confusing 521 with 522: refused and dropped have different causes, and the fix lists barely overlap. If you are seeing timeouts instead, start from the 522 connection timed out guide instead of this one.

Prevention

  • Monitor the origin directly (by IP, not through Cloudflare) so you learn the web server is down before visitors do.
  • Automate Cloudflare IP-range updates in the firewall — the ranges change rarely, but stale copies cause mystery 521s months later.
  • Alert on failed config reloads (nginx -t in CI or a pre-reload hook).
  • After any migration, verify the Cloudflare DNS record content against the new origin IP as a checklist item.
  • Restore real visitor IPs so fail2ban and friends never ban the edge.

A 521 is one of the friendlier Cloudflare errors: the origin answered, so the fault is close and findable. 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.