By The Practical Stack Research Desk — a condensed companion to our full guide: How to Fix Cloudflare Error 522 (Connection Timed Out) & Error 521 on Your VPS.
What Error 522 actually means
When a visitor sees Cloudflare Error 522 (Connection Timed Out) — or its sibling Error 521 (Web Server Is Down) — DNS resolution worked fine. The failure happens one step later: Cloudflare's edge proxy could not complete a TCP handshake with your origin VPS, or never received an HTTP response within Cloudflare's timeout windows.
The key insight most guides skip: Error 522 fires at two different stages, and each stage points to a different root cause.
Phase 1 vs. Phase 2: read the timeout, find the culprit
Phase 1 — TCP SYN handshake (15 seconds). Cloudflare sent SYN packets to port 80/443 and your origin never replied with SYN+ACK. On a VPS this is almost always a firewall problem: UFW, iptables, or Fail2ban silently dropping Cloudflare's edge IP ranges, or a wrong A-record IP in Cloudflare DNS.
Phase 2 — Origin ACK response (90 seconds). The TCP connection opened, but the origin never acknowledged the HTTP request. Typical causes: 100% CPU saturation, TCP KeepAlive disabled on the origin, or exhausted Nginx/Apache worker connections.
The three fixes, in order
- Whitelist Cloudflare's IP ranges in UFW and Fail2ban. All proxied traffic arrives from Cloudflare edge IPs, so rate-limiting tools routinely mistake one busy edge node for a DDoS attack and drop its packets — an instant Phase 1 522. Allow Cloudflare's official IPv4/IPv6 ranges explicitly.
- Enable TCP KeepAlive in Nginx/Apache. Cloudflare reuses connection pools between its edge and your VPS. If keepalive_timeout is disabled, connections drop mid-request and trigger Phase 2 timeouts. Check /etc/nginx/nginx.conf and make sure keepalive is on with a sane timeout.
- Fix SSL/TLS mode mismatches. With Cloudflare set to Full or Full (Strict), the edge connects to your VPS on port 443. If Nginx only listens on port 80 — or the origin certificate has expired — you get Error 521 or 522. Match the mode to what your origin actually serves. Seeing a 502 instead? 502 and 522 are cousins: one is your proxy failing upstream, the other is Cloudflare failing to reach you. Our 502 Bad Gateway in Nginx, Cloudflare & Docker Compose guide covers that diagnosis, and the free Nginx + Docker Compose 502 Config Generator builds a working proxy config in the browser.
Full step-by-step commands, screenshot cards, and the complete fix sequence: How to Fix Cloudflare Error 522 & Error 521 — The Practical Stack.
