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.
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.
| Code | What Cloudflare saw | Where to look first |
|---|---|---|
| 521 Web server is down | The origin refused the TCP connection | Is the app running, is the port open, does the firewall allow Cloudflare |
| 522 Connection timed out | No answer at all within Cloudflare's timeout | Wrong origin IP or CNAME, overloaded server, packets dropped |
| 525 SSL handshake failed | TCP connected but the TLS handshake failed | Certificate 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.
# 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 numberedReading 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
- Open the DNS record for the failing hostname in the Cloudflare dashboard and confirm the IP or CNAME target belongs to the server that runs the app today, not one you retired.
- Check the SSL/TLS mode under SSL/TLS > Overview and match it to the port your server listens on: 80 for Flexible, 443 for Full or Full (strict).
- Run the
curl --resolvetest above. A refusal confirms the fault is at the origin. - On the server, confirm the web server and the app process are running and bound to the expected ports.
- Review the web server and app error logs for crashes, out-of-memory kills or a failed start after the last deploy.
- Compare your firewall rules against Cloudflare's published ranges and allow all of them.
- Look for automatic banning tools (fail2ban, a host-level DDoS filter, a security plugin) that may have blocked individual Cloudflare addresses.
- If you use a managed host, read its deploy logs and status page, because a stopped service there looks the same from Cloudflare's side.
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.
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 reloadWhen 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
- Run the app under a process manager or platform that restarts it on crash, and alert on restarts so a crash loop does not go unnoticed.
- Keep the web server configuration in version control so a deploy cannot silently drop the HTTPS listener.
- Re-run the Cloudflare IP allow script on a schedule, since the published ranges can change.
- Add an external uptime check against the public URL, so you hear about a 521 before your users do.
- When you move servers, update or delete the old DNS records the same day. Shipyard writes the DNS records for the host it deploys to, which avoids leftovers from a previous setup.
Sources
- Cloudflare docs: Error 521developers.cloudflare.com
- Cloudflare docs: Error 522developers.cloudflare.com
- Cloudflare docs: Error 525developers.cloudflare.com
- Cloudflare docs: Cloudflare 5xx errorsdevelopers.cloudflare.com
- Cloudflare: IP rangescloudflare.com
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.