Talk to the NOC, IST hours — +91 75994 50220 support@bigdomainhost.com
Guide

Cloudflare error 521, 522 and 523: what they mean and how to fix them

Your site is behind Cloudflare and visitors see "Web server is down", "Connection timed out" or "Origin is unreachable" instead of your pages. This guide explains what each error means, how to test your server without Cloudflare in the way, and the exact Ubuntu and Windows commands to find and fix the cause.

Short answer: Cloudflare errors 521, 522 and 523 mean Cloudflare could not connect to your server: 521 means the server refused the connection, 522 means it timed out, and 523 means there was no route to its IP. Check the web server is running, your firewall allows Cloudflare's IP ranges on ports 80 and 443, and your DNS A record holds the server's current IP.

What Cloudflare errors 521, 522 and 523 mean

When a site is proxied through Cloudflare (the orange cloud in the DNS tab), every visit makes two connections: one from the visitor's browser to Cloudflare, and a second from Cloudflare to your server, which Cloudflare calls the origin. Errors 521, 522 and 523 all mean the second connection failed. The visitor's side and Cloudflare itself are working; Cloudflare simply could not get a usable TCP connection to your server, so it shows its own error page instead of your site.

The three codes tell you how the connection failed, which tells you where to look:

  • Error 521: Web server is down. Cloudflare's documentation says it occurs when the origin web server refuses connections from Cloudflare. Your server's IP answered, but nothing accepted the connection on the web port, or a firewall actively rejected it.
  • Error 522: Connection timed out. Cloudflare timed out contacting the origin. Either the TCP handshake never completed (no SYN+ACK came back within 19 seconds), or the handshake completed but no data above TCP, such as the TLS Server Hello, arrived within 90 seconds.
  • Error 523: Origin is unreachable. Cloudflare could not reach the origin at all, typically because a network device on the way has no route to the origin's IP address. A wrong IP in your DNS records is a common cause.

All three are origin-side problems. Purging the Cloudflare cache, switching browsers or clearing your own DNS cache will not fix them. The fix is on your server, its firewall, or the IP address in your Cloudflare DNS records.

Quick diagnosis table

Cloudflare origin errors: what each one tells you and where to look first
ErrorWhat happenedMost likely causeCheck first
521 Web server is downConnection refused by the originNginx, Apache or IIS stopped; nothing listening on 80 or 443; firewall rejecting Cloudflare IPssystemctl status nginx and ss -ltnp
522 Connection timed outNo handshake reply in 19 s, or no data within 90 s after itFirewall silently dropping Cloudflare IPs; server overloaded; wrong origin IPFirewall rules, then load and memory
523 Origin is unreachableNo route to the origin's IP addressWrong or old IP in the A/AAAA record; routing problem at the networkA and AAAA records in Cloudflare DNS
524 A timeout occurredConnected, but your application answered too slowlySlow PHP, database or long-running requestApplication logs; see our 504 gateway timeout guide
502 / 504 from your serverYour own web server's proxy failed, not CloudflarePHP-FPM, Node or upstream app down or slowSee the 502 bad gateway guide

To tell a Cloudflare error from your own server's error, look at the error page: Cloudflare's version names the error code, shows a diagram of Browser, Cloudflare and Host with the failing hop marked, and prints a Ray ID at the bottom. Keep the Ray ID and the time of the error; Cloudflare asks you to share the error code, the time with time zone and the failing URL when you contact your host.

Step 1: Test the origin directly, bypassing Cloudflare

Before changing anything, find out whether your server works when Cloudflare is not in the path. You need your origin's public IP address (in the examples, 203.0.113.10) and your domain (example.in). Run these from your own computer, not from the server:

# HTTPS straight to the origin, with the right hostname for SNI and the Host header
curl -sv -o /dev/null --resolve example.in:443:203.0.113.10 https://example.in/

# Plain HTTP straight to the origin
curl -sv -o /dev/null --resolve example.in:80:203.0.113.10 http://example.in/

The --resolve option tells curl to use the IP you give for that host and port instead of looking it up in DNS, so the request skips Cloudflare but still sends the correct hostname. Read the result:

  • "Connection refused" comes back at once: nothing is listening, or a firewall rejects the connection. This is the 521 pattern. Go to Step 2.
  • The command hangs and then times out: packets are being dropped, usually by a firewall, or the server is too busy to answer. This is the 522 pattern. Go to Steps 3 and 4.
  • You get a normal response (200, 301 or similar): the server is fine from your IP. The problem is specific to Cloudflare: its IP ranges are blocked or rate limited, the A record points somewhere else, or the SSL/TLS mode asks for a port you do not serve. Go to Steps 3 and 5.

On Windows, PowerShell has a built-in test that reports TcpTestSucceeded : True or False:

Test-NetConnection -ComputerName 203.0.113.10 -Port 443

Step 2: Fix error 521, make sure the web server is running and listening

Log in to the server over SSH and check the web server service. On Ubuntu 24.04 and 22.04:

sudo systemctl status nginx          # or apache2
sudo ss -ltnp | grep -E ':(80|443)\b'

The ss line lists listening TCP sockets with the program that owns each one. You want to see your web server on 0.0.0.0:80 and 0.0.0.0:443 (Apache shows *:80 and *:443; IPv6 listeners show as [::]:80 and [::]:443). If nothing is listening, start the service and read why it stopped:

sudo nginx -t                        # check the config before starting (apache2ctl configtest for Apache)
sudo systemctl restart nginx
sudo journalctl -u nginx --since "1 hour ago" --no-pager | tail -n 50
sudo tail -n 50 /var/log/nginx/error.log

Common reasons a web server stops: a config change with a syntax error that was applied with a restart, a missing certificate or key file referenced in the config, another program already holding port 80 or 443, or the kernel killing the process when memory ran out. Check for the last one with:

sudo journalctl -k --since "today" | grep -i -E "out of memory|oom"
free -m

Also check that the server listens on the port Cloudflare uses. Cloudflare's 521 documentation is specific here: your web server must listen on port 80 for Flexible mode and on port 443 for Full or Full (strict) mode. A server that only listens on 80 behind a Full-mode zone refuses Cloudflare's HTTPS requests, so HTTPS visitors see 521 even though the site loads fine over plain HTTP.

On a Windows server running IIS, the equivalent checks are:

Get-Service W3SVC
netstat -ano | findstr ":443"
iisreset /start

If IIS is running but nothing listens on 443, open IIS Manager and add an HTTPS binding with a certificate to the site.

Step 3: Fix 521 and 522 caused by a firewall blocking Cloudflare

Cloudflare calls a firewall that blocks or rate limits Cloudflare's IP addresses the most common cause of error 522, and it is also a top cause of 521. The reason is simple: once a site is proxied, all visitor traffic reaches your server from a small set of Cloudflare IP ranges. Cloudflare's documentation warns that this can look like a few sources sending a lot of traffic, which can trip automatic blocking or rate limiting.

The difference between the two errors is how the firewall says no. A rule that rejects a connection gets a 521. A rule that silently drops packets gets a 522, because Cloudflare waits for a reply that never comes.

Check UFW, iptables and nftables

sudo ufw status verbose
sudo iptables -L INPUT -n --line-numbers | head -n 40
sudo nft list ruleset | head -n 60

If UFW is active and there is no rule allowing 80 and 443, add one. To allow only Cloudflare to reach your web ports, pull the current ranges from Cloudflare's published lists and add a rule per range:

for ip in $(curl -fsS https://www.cloudflare.com/ips-v4/) $(curl -fsS https://www.cloudflare.com/ips-v6/); do
  sudo ufw allow proto tcp from "$ip" to any port 80,443 comment 'Cloudflare'
done
sudo ufw status numbered

Cloudflare says its ranges change rarely and that new ranges appear on its list before they go into production, so re-run this when the list changes. If you also want to stop people bypassing Cloudflare by hitting your IP directly, Cloudflare recommends blocking web traffic that does not come from its ranges; with UFW that means deleting any general allow 80 or allow 443 rule (by its number from sudo ufw status numbered) once the Cloudflare rules are in place. Leave your SSH rule alone, and do this only after Step 1 confirms the site works.

Check fail2ban and security plugins

Tools that ban IPs after repeated failures can ban a Cloudflare address, because your server sees Cloudflare's IP instead of the visitor's. One noisy visitor behind a Cloudflare data centre can then cut off everyone routed through it, which looks like intermittent 521 or 522 errors in some cities only.

sudo fail2ban-client status
sudo grep " Ban " /var/log/fail2ban.log | tail -n 20

If you see Cloudflare ranges in the ban list, unban them with sudo fail2ban-client set <jail> unbanip <IP> and add the Cloudflare ranges to the ignoreip setting in /etc/fail2ban/jail.local. For a lasting fix, make your web server log the real visitor address, which Cloudflare sends in the CF-Connecting-IP header, so your logs and ban tools see visitors rather than Cloudflare. Keep in mind that a firewall ban on a visitor's own IP does not stop traffic that reaches you through Cloudflare, so block abusive visitors in Cloudflare's dashboard instead. WordPress security plugins and ModSecurity rules can block Cloudflare the same way; Cloudflare's documentation lists both as things to review.

Check the provider's firewall, not just the server's

Many VPS and cloud platforms have a network firewall outside the server as well, set in the control panel. If that firewall allows only SSH, your UFW rules do not matter: Cloudflare's packets are stopped before they reach the server, and you usually see 522.

On Windows Server, open the web ports in Windows Defender Firewall from an elevated PowerShell:

New-NetFirewallRule -DisplayName "Web HTTP/HTTPS" -Direction Inbound -Protocol TCP -LocalPort 80,443 -Action Allow

Step 4: Fix 522 caused by an overloaded or wrong origin

If the firewall is clean and the direct test from Step 1 times out or is very slow, the server may be too busy to accept new connections. Check load, memory and connection counts:

uptime
free -m
sudo ss -s
sudo ss -tn state syn-recv | wc -l
top -o %CPU

A load average far above your vCPU count, swap in heavy use, or thousands of connections stuck in SYN-RECV all point to a server that cannot keep up, whether from real traffic, a bot crawl or an attack. Restarting PHP-FPM or the application frees resources for a moment; the lasting fixes are caching, fixing slow queries, rate limiting abusive clients at Cloudflare, or a larger server.

Cloudflare lists three more causes of 522 worth ruling out:

  • The origin IP in Cloudflare DNS is not your server's current IP. This happens after a migration, a rebuild or a move to a new server. Compare the A record in the Cloudflare DNS tab with ip -4 addr show on the server.
  • Keepalives are disabled at the origin. Cloudflare names this as a cause of 522 without going into detail. Check that nothing on the server turns keepalives off, for example keepalive_timeout 0 in Nginx or KeepAlive Off in Apache.
  • Packets are dropped at the origin, for example by a full connection-tracking table on a busy firewall. Check sudo journalctl -k | grep -i conntrack for "table full" messages.

Step 5: Check the SSL/TLS mode matches your server

The SSL/TLS encryption mode in the Cloudflare dashboard decides how Cloudflare talks to your origin, and a mismatch is a frequent cause of 521:

  • Flexible: Cloudflare connects to your origin over plain HTTP on port 80. No certificate is needed on the server. If your server redirects HTTP to HTTPS, this mode causes a redirect loop instead; see our guide to ERR_TOO_MANY_REDIRECTS.
  • Full: Cloudflare connects over HTTPS when the visitor uses HTTPS, without validating your certificate. Port 443 must be open and present a certificate (self-signed is accepted); without one, Cloudflare's Full mode page says visitors may get error 525.
  • Full (strict): like Full, but your certificate must be valid and unexpired, from a public CA such as Let's Encrypt or from Cloudflare's Origin CA. A bad certificate here gives error 526 rather than 521.

Cloudflare recommends Full or Full (strict) where possible. If you switch from Flexible to Full, install a certificate and confirm that the server listens on 443 first; otherwise the switch itself turns a working site into a 521 (nothing on 443) or a 525 (TLS on 443 not working).

Step 6: Fix error 523, a wrong IP or a routing problem

For 523, start with DNS. In the Cloudflare DNS tab, check every proxied A and AAAA record for the hostname:

  • The A record must hold your server's current public IPv4 address, not an old server's IP or a private address such as 10.x or 192.168.x.
  • An AAAA record is a frequent hidden cause. If your server has no working IPv6, or the AAAA record holds an old address, fix it or delete the AAAA record so that Cloudflare uses the A record only.

If the records are right, the problem is routing between Cloudflare and your server. Cloudflare asks for an MTR or traceroute from your origin to a Cloudflare IP that used to connect to your server, which you can find in your web server access log. On Ubuntu:

sudo apt install mtr-tiny
mtr -rwc 50 <Cloudflare IP from your access log>
ip route

Send that report to your hosting provider; it shows where packets stop.

Common mistakes that cause 521, 522 and 523

  • Allowing ports in UFW but not in the provider's firewall, or the other way round. Both layers must allow 80 and 443.
  • Copying an old list of Cloudflare IP ranges from a blog post instead of fetching the current list from cloudflare.com/ips.
  • Allowing only IPv4 ranges while the hostname also has a proxied AAAA record pointing at the server. If you publish an IPv6 origin address, allow Cloudflare's IPv6 ranges too.
  • Switching Cloudflare to Full mode before the server has a certificate and listens on 443.
  • Leaving an old A record after a migration. After moving a site, update the record in Cloudflare, not just at the domain registrar. Our guide to pointing a domain to a VPS covers the checks.
  • Rate limiting by IP at the origin without trusting CF-Connecting-IP, so the limiter counts all visitors from a Cloudflare data centre as one client.

Is it Cloudflare's fault?

Usually not. Cloudflare's own troubleshooting pages tell site owners to start every 5xx investigation with their hosting provider or site administrator, and note that the cause is not always in the origin's error log: check any load balancers, proxies or firewalls in front of the server too. If Cloudflare's status page shows no incident and your direct test fails, the fix is on your side. If the direct test passes and every firewall layer allows Cloudflare's ranges, collect the Ray ID, an MTR from the origin and your firewall rules, and then open a ticket.

Sources (checked )

Answers

Frequently asked questions

Still unsure? Call +91 75994 50220 or write to support@bigdomainhost.com — a human replies in IST hours.

What does Cloudflare error 521 mean?

Error 521 "Web server is down" means your server refused the connection from Cloudflare. Usually the web server (Nginx, Apache or IIS) has stopped, nothing listens on the port Cloudflare uses (80 for Flexible mode, 443 for Full and Full strict), or a firewall rejects Cloudflare's IP addresses.

What does Cloudflare error 522 mean?

Error 522 "Connection timed out" means Cloudflare got no answer from your server. Either the TCP handshake reply did not arrive within 19 seconds, or no data such as the TLS Server Hello arrived within 90 seconds after connecting. A firewall silently dropping Cloudflare's IP ranges is the most common cause.

What is the difference between error 521 and 522?

Both mean Cloudflare could not connect to your server, but 521 is a refusal and 522 is silence. With 521 the server or a firewall actively rejected the connection. With 522 packets were dropped or the server was too busy to reply, so Cloudflare waited and timed out.

What does Cloudflare error 523 origin is unreachable mean?

Error 523 means Cloudflare could not reach your server's IP address at all, typically because there is no network route to it. Check that the A and AAAA records in Cloudflare DNS hold your server's current public IP. If they do, send an MTR from your server to a Cloudflare IP to your host.

How do I allow Cloudflare IPs in UFW?

Fetch Cloudflare's current lists from cloudflare.com/ips-v4 and cloudflare.com/ips-v6, then add a rule per range, for example sudo ufw allow proto tcp from 173.245.48.0/20 to any port 80,443. A short shell loop over the lists does all ranges at once. Re-run it if Cloudflare adds ranges.

Can visitors fix a Cloudflare 521 or 522 error?

No. These errors are between Cloudflare and the website's server, so clearing your browser cache, changing DNS or switching networks will not help. Only the site owner or their host can fix it. If it is not your site, wait and try later or tell the site owner.

Why do I get 521 after switching Cloudflare SSL to Full?

In Full and Full (strict) modes Cloudflare connects to your server on port 443. If nothing listens on port 443, the connection is refused and Cloudflare shows 521; if something listens but the TLS handshake fails, you get 525 instead. Install a certificate, confirm the server listens on 443, then switch modes.

Does pausing Cloudflare fix error 522?

Setting the record to DNS only (grey cloud) sends visitors straight to your server, which tells you whether the server works without Cloudflare. If the site then loads, your firewall is probably blocking Cloudflare's IPs. It also exposes your server IP, so fix the cause and turn the proxy back on.

Support that picks up the phone.

In IST hours, in Hindi or English, and WhatsApp replies fastest at any time. Sales, migration and emergencies all reach the same engineers. No offshore queue, no 48-hour first reply.

Need cPanel or WHM with the ports already sorted?

Web & cPanel hosting comes with cPanel ready to log in to, reseller hosting adds white-label WHM, and a KVM VPS can have cPanel & WHM pre-installed. Call +91 75994 50220 or email support@bigdomainhost.com.