Skip to main content

How to Troubleshoot and Fix Cloudflare Error 521 on Ubuntu

7 min read
Fix my Cloudflare 521

We trace why your origin server stops answering Cloudflare and get it responding again.

A practical Ubuntu-focused guide to diagnosing and resolving Cloudflare Error 521, covering origin server checks, web service restarts, firewall rules, resource monitoring, and prevention strategies.

Fixing Cloudflare Error 521: Web Server is Down

Cloudflare Error 521 means Cloudflare cannot reach the origin server. In this Ubuntu-focused guide, we explain why it happens, how Cloudflare's network interacts with the server, and provide step-by-step fixes to restore access quickly.

Understanding Error 521

Cloudflare proxies traffic to the origin server for speed and security. A 521 indicates Cloudflare's edge servers failed to get any response. Possible culprits include the server being offline, services (web, database, cache) being down, firewall rules blocking ports 80/443, or network/DNS issues.

Common Causes

  • Server Offline: Hardware failure, provider outage, or maintenance.
  • Web Service Down: Apache/Nginx crashed or misconfigured.
  • Database Down: MySQL/PostgreSQL service unavailable, causing application errors.
  • Port Blocked: Firewall (UFW, iptables) rejecting Cloudflare IPs.
  • DNS Misconfigured: A/AAAA records pointing to the wrong IP.
  • SSL/TLS Mismatch: Cloudflare expecting HTTPS but the origin is HTTP-only.

Quick Fixes and Basic Checks

4.1 Check Server Status

sudo systemctl status apache2
# or
sudo systemctl status nginx

4.2 Check Firewall Rules

sudo ufw status

Ensure Cloudflare IP ranges are allowed on ports 80 and 443. Cloudflare publishes their IP ranges at cloudflare.com/ips -- we must whitelist all of them.

4.3 Restart Web Services

sudo systemctl restart apache2
# or
sudo systemctl restart nginx

Advanced Troubleshooting

5.1 Monitor Resources

top
htop

High load can starve services -- consider scaling or tuning if resource utilization is consistently high.

5.2 Tune MySQL and Web Server

  • max_allowed_packet: Adjust in /etc/mysql/my.cnf
  • innodb_buffer_pool_size: Increase to cache more data

These settings require Linux/MySQL expertise -- incorrect values can make things worse.

5.3 Validate DNS and SSL

Ensure A/AAAA records point correctly and that the SSL certificate is valid. Use Cloudflare's diagnostic tools for guidance.

5.4 Contact the Hosting Provider

If all else fails, the hosting provider can diagnose network or virtualization layer issues that are not visible from within the instance.

Preventing Future Issues

  • Automated Backups: Regular snapshots of files and the database.
  • Firewall Hardening: Keep UFW rules lean and up to date.
  • Professional Maintenance: Ongoing care from experienced server administrators.
  • Performance Monitoring: Use Nagios, Zabbix, or similar tools for real-time alerts.
  • Timely Updates: Patch Ubuntu, Apache/Nginx, MySQL, and other services promptly.

Wrap-Up

Error 521 almost always traces back to something on the origin server side -- a stopped service, a firewall rule, or resource exhaustion. By systematically checking service status, firewall configuration, resource usage, and DNS settings, we can identify and resolve the issue quickly. Proactive monitoring and regular maintenance are the best defenses against unexpected downtime.

Frequently asked questions

What does Cloudflare Error 521 mean?

Cloudflare Error 521 means Cloudflare's edge servers could not get any response from the origin server. Cloudflare proxies visitor traffic to the origin, so a 521 appears when the origin is offline, its web, database or cache services are down, a firewall blocks ports 80 and 443, or network or DNS problems stand in the way.

How do I check and restart Apache or Nginx when Cloudflare shows Error 521 on Ubuntu?

Run sudo systemctl status apache2 or sudo systemctl status nginx to see whether the web server is running. A crashed or misconfigured Apache or Nginx is one of the common causes of a 521. If the service is down, bring it back with sudo systemctl restart apache2 or sudo systemctl restart nginx and load the site through Cloudflare again.

Can a UFW firewall cause Cloudflare Error 521?

Yes. When UFW or iptables rejects connections from Cloudflare's IP addresses, the edge cannot reach the origin and visitors see a 521. Check the rules with sudo ufw status and make sure the IP ranges Cloudflare publishes are allowed on ports 80 and 443. Every one of the published ranges needs to be on the allow list.

Can wrong DNS records or an SSL mismatch cause Cloudflare Error 521?

Both can. A or AAAA records pointing to the wrong IP are one cause, and an SSL/TLS mismatch, where Cloudflare expects HTTPS but the origin is HTTP-only, is another. Confirm the records point to the correct origin, check that the SSL certificate is valid, and use Cloudflare's diagnostic tools for further guidance.

Can high server load trigger Cloudflare Error 521?

High load can starve the origin's services until Cloudflare gets no response. Watch resource usage with top or htop, and if utilization stays high, scale the server or tune it. A MySQL or PostgreSQL service that has gone down is a related cause, since the application behind the web server then starts throwing errors.

Which MySQL settings matter when an overloaded database is behind Cloudflare Error 521?

The two settings to look at are max_allowed_packet and innodb_buffer_pool_size, both set in /etc/mysql/my.cnf. Increasing innodb_buffer_pool_size lets MySQL cache more data. Incorrect values can make things worse, so changes to either setting need Linux and MySQL expertise.

When should I contact my hosting provider about Cloudflare Error 521?

Contact the hosting provider when service status, firewall rules, resource usage and DNS all check out and the 521 remains. The provider can diagnose network and virtualization layer issues that are not visible from inside the instance. The server may also be offline because of a hardware failure, a provider outage or maintenance.

How can I prevent Cloudflare Error 521 from coming back?

Set up performance monitoring with Nagios, Zabbix or a similar tool so a stopped service raises a real-time alert. Patch Ubuntu, Apache or Nginx, MySQL and the other services promptly, keep UFW rules lean and up to date, and take regular snapshots of the files and the database.

Read how we handle it in Cloud & Server Management.