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
| Error | What happened | Most likely cause | Check first |
|---|---|---|---|
| 521 Web server is down | Connection refused by the origin | Nginx, Apache or IIS stopped; nothing listening on 80 or 443; firewall rejecting Cloudflare IPs | systemctl status nginx and ss -ltnp |
| 522 Connection timed out | No handshake reply in 19 s, or no data within 90 s after it | Firewall silently dropping Cloudflare IPs; server overloaded; wrong origin IP | Firewall rules, then load and memory |
| 523 Origin is unreachable | No route to the origin's IP address | Wrong or old IP in the A/AAAA record; routing problem at the network | A and AAAA records in Cloudflare DNS |
| 524 A timeout occurred | Connected, but your application answered too slowly | Slow PHP, database or long-running request | Application logs; see our 504 gateway timeout guide |
| 502 / 504 from your server | Your own web server's proxy failed, not Cloudflare | PHP-FPM, Node or upstream app down or slow | See 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 showon 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 0in Nginx orKeepAlive Offin 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 conntrackfor "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.xor192.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 )
- Cloudflare Docs: Error 521: web server is down
- Cloudflare Docs: Error 522: connection timed out
- Cloudflare Docs: Error 523: origin is unreachable
- Cloudflare Docs: Cloudflare 5xx errors: required error details for hosting provider
- Cloudflare Docs: Cloudflare IP addresses: allow Cloudflare IPs at your origin
- Cloudflare: IP ranges
- Cloudflare Docs: SSL/TLS encryption modes
- Ubuntu Manpages: ufw(8): program for managing a netfilter firewall (Ubuntu 24.04)
- curl project: curl man page: --resolve