Fix Cloudflare Error 521 on Your Server
In this article
Cloudflare error 521 means your origin server refused the connection when Cloudflare tried to reach it. This guide walks you through the most common causes and fixes, from checking if your server is online to adjusting firewall rules and TLS settings. You'll need command-line access to your server and, if you use one, access to your hosting control panel.
What You'll Need
- SSH or console access to your origin server (or a hosting control panel like cPanel, Plesk, or a cloud provider dashboard)
- Access to your Cloudflare dashboard for the affected domain
- Basic familiarity with the command line (or the ability to follow commands exactly)
Step-by-Step
- Check if your server is actually online. From your local machine, run
ping yourdomain.com(replace with your real domain) to see if the server responds. If it doesn't, your server may be down, rebooting, or the network is unreachable. Log into your hosting provider's dashboard and confirm the server is running. - Verify the web server is running. SSH into your server and run
sudo systemctl status nginx(orapache2,httpd, or whatever you use). If it shows inactive or failed, start it withsudo systemctl start nginxand enable it to start on boot withsudo systemctl enable nginx. If you use a different process manager (likeserviceorsupervisord), use the equivalent command. - Test locally on the server. From the server itself, run
curl -I http://localhost(orcurl -I https://localhostif you have SSL configured). If you get an HTTP response (like 200 or 301), the web server is working locally. If you get a connection refused error, the web server isn't listening on the expected port. - Check the port your web server listens on. Cloudflare connects to your origin on port 80 (HTTP) or 443 (HTTPS) by default. Run
sudo netstat -tulpn | grep :80andsudo netstat -tulpn | grep :443to see what's listening. If nothing is listening on those ports, your web server may be configured for a different port (like 8080). If so, either change your web server config to use 80/443, or set a specific origin port in Cloudflare (see step 9). - Check firewall rules. If your server has a firewall (like UFW, firewalld, or iptables), make sure it allows incoming connections on ports 80 and 443 from Cloudflare's IP ranges. Cloudflare publishes its IP ranges at
https://www.cloudflare.com/ips/. For UFW, you can allow all traffic from those ranges with commands likesudo ufw allow from 173.245.48.0/20 to any port 80 proto tcp(repeat for 443 and for each Cloudflare IP range, or use a script to add them all). - Check for IP-blocking or rate limiting. Some security modules (like mod_security, fail2ban, or custom iptables rules) may be blocking Cloudflare's IPs. Temporarily disable fail2ban or mod_security to test. If that fixes it, add Cloudflare's IPs to your whitelist.
- Check TLS/SSL settings. If your origin server requires HTTPS (port 443) and has a self-signed or invalid SSL certificate, Cloudflare may fail to connect. In your Cloudflare dashboard, go to SSL/TLS > Overview. If your origin uses HTTPS, set the SSL mode to “Full (strict)” and ensure your origin has a valid certificate (even a free Let's Encrypt one). If your origin only supports HTTP, set the mode to “Flexible” or “Full” (not strict) and make sure your server is listening on port 80.
- Verify DNS records in Cloudflare. In your Cloudflare dashboard, go to DNS > Records. Make sure the A record for your domain points to the correct origin IP address. If you recently changed hosting, the old IP may still be there. Also ensure the record is proxied (orange cloud) – if it's grey, Cloudflare isn't involved, and the error wouldn't come from Cloudflare.
- Set a specific origin port (if needed). If your web server runs on a non-standard port (like 8080 or 8443), you can tell Cloudflare to use that port. In the Cloudflare dashboard, go to DNS > Records, click the record, and under “Origin Port” enter the correct port. Note: Cloudflare only supports certain ports for HTTP/HTTPS (like 80, 443, 8080, 8443, 2052, 2082, 2083, 2086, 2087, 2095, 2096).
- Check for IP whitelisting on the server. Some servers have security plugins or hosting-level firewalls that only allow traffic from certain IPs. Ensure Cloudflare's IPs are not blocked. You can test by temporarily adding your own IP to the whitelist and then curling your domain from outside.
- Restart your web server and test again. After making changes, restart the web server (
sudo systemctl restart nginx) and then runcurl -I https://yourdomain.comfrom your local machine (not the server). If you get a response, the error should be gone. - Check server logs for clues. If the error persists, look at your web server's error log (e.g.,
/var/log/nginx/error.logor/var/log/apache2/error.log) and Cloudflare's error log (in the dashboard under Security > Events). Look for connection refused, timeout, or SSL handshake failures.
Common Mistakes
- Forgetting to open the firewall for Cloudflare's IPs. Many people only allow their own IP, not Cloudflare's ranges.
- Using “Full (strict)” when your origin has a self-signed certificate. This will cause a 521 because Cloudflare can't verify the certificate.
- Pointing DNS to an old IP. If you migrated servers, the old IP may be down, causing a 521.
- Changing the origin port but not updating Cloudflare. If your server listens on 8080 but Cloudflare tries 80, it will fail.
- Assuming the server is up because the website was working before. Server crashes happen; always verify with ping and curl.
Tips
- Use Cloudflare's IP ranges. Regularly update your firewall rules to include the latest IP ranges from
https://www.cloudflare.com/ips/. - Set up monitoring. Use a simple uptime monitor (like UptimeRobot) to alert you if your origin goes down.
- Keep SSL certificates valid. Use Let's Encrypt with auto-renewal to avoid certificate expiration issues.
- Test from an external network. Curl from your local machine, not from the server itself, to simulate Cloudflare's perspective.
- Use Cloudflare's “Diagnose” tool. In the Cloudflare dashboard, go to Help > Diagnose, which can show if Cloudflare can reach your origin.
Troubleshooting
If you've followed the steps and still get error 521, try these additional checks:
- Check if your hosting provider blocks Cloudflare. Some cheap hosts block Cloudflare's IPs by default. Contact support and ask if they allow Cloudflare traffic.
- Try disabling Cloudflare temporarily. In DNS, set the record to “DNS only” (grey cloud). If the site loads, the issue is with Cloudflare's connection to your origin.
- Check for a server-side firewall at the hosting level. Some cloud providers have security groups or network firewalls separate from the OS firewall. Ensure ports 80/443 are open there too.
- Test with a different web server. If you're using a complex setup (like a reverse proxy), try a simple static page served directly by nginx/apache to isolate the issue.
- Look at the Cloudflare error details. The error page may say “Web server is down” or “Connection timed out”. A timeout suggests a firewall or network issue; a connection refused suggests the web server isn't listening.
Frequently Asked Questions
What exactly causes error 521?
Error 521 means Cloudflare's servers couldn't establish a TCP connection to your origin server. This is almost always because the server is offline, the web server isn't running, a firewall is blocking Cloudflare's IPs, or the origin port is wrong.
Is error 521 the same as error 522?
No. 521 means the connection was refused (server not listening). 522 means the connection timed out (server is unreachable, often due to network issues or a firewall silently dropping packets).
Do I need to open all ports for Cloudflare?
No, only the ports your web server uses (usually 80 and 443). You should restrict access to those ports to only Cloudflare's IP ranges for security.
Can I fix error 521 without SSH access?
Sometimes. If the issue is a wrong DNS IP, you can fix it in the Cloudflare dashboard. If the server is down, you may need to restart it from your hosting provider's control panel. But for firewall or web server configuration, you'll need some form of server access.
Why does error 521 happen after I change my SSL settings?
If you switch to “Full (strict)” but your origin has an invalid or self-signed certificate, Cloudflare can't complete the TLS handshake, resulting in a 521. Revert to “Full” or install a valid certificate.
How long does it take for Cloudflare to detect my server is back online?
Usually within a minute or two. Cloudflare retries connections frequently. If it doesn't resolve quickly, purge your cache or wait a few minutes.