What the message looks like
Error 522
Connection timed out
The initial connection between Cloudflare's network and the origin web server timed out.
Error 521
Web server is down
The origin web server refused the connection.Common causes
- A firewall, fail2ban or rate limit on the origin blocks the CDN's IP addresses (very common with 522).
- The web server on the origin is stopped, crashed or listens on another port (521).
- The DNS record points to an old or wrong origin IP, for example after a migration.
- The origin is overloaded and does not accept new connections in time.
- The CDN connects on a port or protocol the origin does not offer (HTTPS to a server that only listens on 80).
Check
- Is the web server running and listening?
systemctl is-active nginx; ss -tlnp | grep -E ':(80|443)\b' - Does the origin answer directly? (use the real origin IP)
curl -sI --resolve example.com:443:203.0.113.10 https://example.com/ | head -n 5 - Do requests from the CDN reach the origin at all?
sudo tail -n 20 /var/log/nginx/access.log - Is the CDN's address range blocked by a ban?
sudo fail2ban-client status
Fix
- Start or repair the origin web server and make sure it listens on the ports the CDN uses.
- Allow the CDN's published IP ranges in the origin firewall and exclude them from fail2ban and rate limits.
- Restore the real visitor IP so bans hit visitors, not the CDN: use
set_real_ip_fromfor the CDN ranges andreal_ip_header CF-Connecting-IP;(Cloudflare). - Fix the origin IP in the CDN's DNS record and align the CDN SSL mode with what the origin serves.
- If the origin is overloaded, look at PHP-FPM and database load before blaming the CDN.
Typical scenario
You enabled fail2ban with a strict nginx rule against request floods on the origin server. Since that day Cloudflare shows error 522 now and then, sometimes on every third request, then not at all. Because all visitors reach the origin with Cloudflare addresses, fail2ban banned individual Cloudflare servers and thereby locked out thousands of visitors. With fail2ban-client status nginx-limit-req check whether the banned addresses come from the Cloudflare ranges.
FAQ
What is the difference between 521 and 522?
521: the origin actively refused the connection, so nothing listens or a firewall rejects it. 522: no answer in time, which fits a firewall that drops packets or an overloaded server.
How is this different from 502 and 504?
502 and 504 come from your own server or proxy reaching its backend. 521 and 522 mean the CDN could not even reach your server.
Why does 522 occur only intermittently?
Cloudflare contacts the origin from many changing addresses. If a firewall or fail2ban blocks only some of them, exactly the requests that pass through those nodes fail while others get through. That is why the error looks random and depends on the visitor's location.
Where do I find Cloudflare's IP ranges?
Cloudflare publishes them on its "IP Ranges" page, as separate lists for IPv4 and IPv6. You enter them as allowed in the firewall and use them in nginx for set_real_ip_from. The lists rarely change, but they do change, so an occasional comparison is worthwhile.
What do 523 and 524 mean?
523 means the origin is unreachable, for example because of a wrong DNS entry or routing. 524 means the connection was established but the origin did not answer in time; with Cloudflare the limit is about 100 seconds by default. Both differ from 521 and 522.