Cloudflare Error 521 "Web server is down": causes and fixes

Cloudflare Error 521 means your origin refused Cloudflare's connection. Usually the app is down or a firewall blocks Cloudflare IPs. How to find and fix it.

Simon, Founder of Shipyard

By · Founder of Shipyard

6 minute read

Error 521 in one paragraph

Cloudflare Error 521 "Web server is down" means Cloudflare found your origin server and tried to open a connection, but the origin actively refused it. Cloudflare's own docs name two common causes: the web server application is offline, or something at the origin is blocking Cloudflare's requests.

The fix is almost always on your server, not in Cloudflare. Check the app is running and listening on the port your SSL/TLS mode uses: 443 for Full or Full (strict), 80 for Flexible. Then allow all Cloudflare IP ranges in the firewall, and make sure the DNS record still points at the machine that runs the app.

521 next to its neighbours

The three origin errors look alike on screen but point to different layers. Reading the number correctly saves an hour of checking the wrong thing.

CodeWhat Cloudflare sawWhere to look first
521 Web server is downThe origin refused the TCP connectionIs the app running, is the port open, does the firewall allow Cloudflare
522 Connection timed outNo answer at all within Cloudflare's timeoutWrong origin IP or CNAME, overloaded server, packets dropped
525 SSL handshake failedTCP connected but the TLS handshake failedCertificate and port 443 at the origin, SSL/TLS mode

Why "refused" narrows the search

A refusal is an answer. Something at that IP address replied and said no, so the address is reachable and the network path works. That rules out most DNS typos that send traffic into the void, which would produce a timeout (522) instead.

What replies with a refusal is usually one of three things. The operating system, because no process is listening on that port. A firewall rule set to reject rather than drop. Or a security tool such as fail2ban that has banned the specific Cloudflare address that tried to connect. Because Cloudflare reaches your server from many addresses, a partial ban can produce a 521 for some visitors and a working site for others.

The SSL/TLS mode matters here too. In Flexible mode Cloudflare connects to your origin on port 80. In Full and Full (strict) it connects on port 443. A server that only listens on 80 will refuse every connection once you switch the mode to Full, and the result is a 521 on every page.

A worked example: tidyroster.com on a small VPS

Take a scheduling app, tidyroster.com, running as a Node process behind nginx on a single virtual server at 203.0.113.10. The DNS record is a proxied A record, and the SSL/TLS mode is Full (strict) with a Cloudflare Origin Certificate installed in nginx.

After a deploy the whole site shows Error 521. The four commands below find the cause in a few minutes. The first bypasses Cloudflare entirely by forcing curl to connect to the origin IP while still sending the right hostname, so you see exactly what Cloudflare sees.

Terminal
# 1. Connect straight to the origin, skipping Cloudflare
curl -sv --resolve tidyroster.com:443:203.0.113.10 https://tidyroster.com/ -o /dev/null
# "Connection refused" here = the same fault Cloudflare reports as 521

# 2. On the server: is anything listening on 80 and 443?
sudo ss -tlnp | grep -E ':(80|443) '

# 3. Is the web server itself running?
sudo systemctl status nginx --no-pager

# 4. Which firewall rules apply?
sudo ufw status numbered

Reading the results

In the tidyroster case, step 2 printed nothing for port 443. The deploy script had replaced the nginx site file with a version that only had a listen 80 block, so nginx was running but no longer served HTTPS. Cloudflare, in Full (strict) mode, knocked on 443 and the kernel refused. Restoring the listen 443 ssl block and reloading nginx cleared the error at once.

Other outcomes point elsewhere. If step 3 shows nginx stopped or crashed, read its error log and the app's own log before restarting, because a crash loop will bring the 521 back. If steps 2 and 3 look healthy but step 1 still refuses, the firewall is the suspect. If step 1 works from your laptop but Cloudflare still reports 521, a tool on the server may be banning Cloudflare addresses specifically.

Checklist, fastest checks first

Firewalls that block Cloudflare by accident

Once a hostname is proxied, every visitor's request reaches your server from a Cloudflare address, not from the visitor's own IP. Security tools that count requests per IP therefore see a handful of very busy addresses and may decide they are attackers. Cloudflare's docs ask you to make sure its IP addresses are not blocked or rate limited, and to allow all of its ranges in the origin firewall.

The ranges are published as plain text, which makes them easy to script. The loop below adds an allow rule for each IPv4 range on a server that uses ufw. Run the same idea for the IPv6 list if your server has an IPv6 address in DNS.

If you use fail2ban, configure your web server to log the real visitor address instead of the Cloudflare address, so bans hit the right client. Otherwise the tool will keep banning Cloudflare and the 521 will return at random.

Terminal
for range in $(curl -s https://www.cloudflare.com/ips-v4); do
  sudo ufw allow proto tcp from "$range" to any port 80,443
done
sudo ufw reload

When the origin is Railway or Vercel

On a platform host you do not manage a firewall or nginx, so the causes shrink. The two that remain are a record that points at the wrong place and an app that is not running. A stale A record left over from an old server is the classic case: the old machine still answers, refuses the connection, and Cloudflare shows 521 even though your new deploy is healthy. Replace it with the CNAME target your host gives you.

If the record is right, open the host's deploy logs. An app that crashes on start, or that listens on a hard-coded port instead of the PORT the platform provides, will fail there first. For the record types each host expects, the walkthrough in Cloudflare DNS for a new app covers them one by one.

How to confirm it is fixed

Repeat the curl --resolve command. You want an HTTP status line such as HTTP/2 200 instead of a refused connection. Then request the public URL with curl -sI https://tidyroster.com/ and check for a normal response with a cf-ray header, which shows the request went through Cloudflare and back successfully.

Cloudflare caches some error responses briefly, so give it a minute before deciding the fix failed. If you see a different error afterwards, such as a redirect loop, that is a separate issue: ERR_TOO_MANY_REDIRECTS usually means Flexible mode meets an origin that forces HTTPS.

Keeping 521 away

Sources

Launch in an hour, not a weekend

Shipyard connects your accounts once, then sets up the domain, hosting, database, email and payments for every new product.

Get started See how it works

Read next