Home
How to Fix Cloudflare Error 522 Connection Timed Out on Your Server
Cloudflare Error 522 is one of the most common issues encountered by website administrators using a Content Delivery Network (CDN). When a visitor tries to access your website, they are met with a branded Cloudflare page stating "Error 522: Connection timed out." This error essentially means that while Cloudflare's edge servers attempted to establish a connection with your origin web server, the request was not acknowledged within a specific timeframe.
Unlike some other 5xx errors that might stem from Cloudflare's infrastructure, a 522 error is almost exclusively an issue located at the origin server level or the network path leading to it. Understanding the technical nuances of this timeout is the first step toward restoring your website's availability.
What Does Cloudflare Error 522 Mean?
To understand Error 522, we must look at how a Transmission Control Protocol (TCP) connection is established, often called the "Three-Way Handshake." When Cloudflare acts as a reverse proxy, it sits between the user and your server. The connection process follows these steps:
- SYN: Cloudflare sends a "Synchronize" packet to your origin server to initiate a connection.
- SYN-ACK: Your server should respond with a "Synchronize-Acknowledgment" packet.
- ACK: Cloudflare sends an "Acknowledgment" packet back to complete the handshake.
Error 522 occurs in two specific scenarios:
- Before a connection is established: Your origin server does not return a SYN-ACK packet to Cloudflare within 15 seconds of the initial SYN request.
- After a connection is established: Your origin server accepts the connection but does not acknowledge the actual resource request (the HTTP GET or POST) within 90 seconds.
In layman's terms, Cloudflare knocked on your server's door, but the server either didn't hear it, refused to open the door, or took so long to walk to the door that Cloudflare gave up and left.
The Difference Between Error 521, 522, and 524
When troubleshooting, it is easy to confuse these three errors. However, they indicate different failure points:
- Error 521 (Web Server Is Down): The origin server actively refused the connection. This usually means the web server software (like Nginx or Apache) is not running or a firewall is sending a "Reset" packet.
- Error 522 (Connection Timed Out): The origin server did not respond at all. The packet was likely dropped or ignored.
- Error 524 (A Timeout Occurred): The TCP handshake was successful, and Cloudflare sent the request, but the server took too long (more than 100 seconds) to process the logic and start sending back data. This is common with long-running database queries or heavy PHP scripts.
Common Causes of the 522 Connection Timeout
Identifying the root cause requires checking several layers of your hosting environment. The most frequent culprits include:
- Firewall Blocking Cloudflare IPs: This is the #1 cause. Your server’s firewall (iptables, UFW, or a hardware firewall) sees a massive amount of traffic coming from a few Cloudflare IP addresses and mistakes it for a DDoS attack, subsequently dropping the packets.
- Server Overload: If your server's CPU or RAM is pegged at 100%, it may lack the resources to process new TCP handshakes, leading to dropped requests.
- Incorrect DNS Settings: If you moved your site to a new server but didn't update the IP address in your Cloudflare DNS dashboard, Cloudflare will try to connect to a non-existent or incorrect server.
- Disabled KeepAlive: Cloudflare relies on persistent connections. If your server is configured to close connections too aggressively, it can trigger 522 errors during high-traffic periods.
- Routing Issues: Physical network congestion or outages between Cloudflare’s data centers and your hosting provider’s facility can cause packets to get lost in transit.
Step-by-Step Troubleshooting and Fixes
Follow this systematic approach to diagnose and resolve the issue.
Step 1: Verify Origin Server Health and Status
Before diving into complex configurations, ensure your server is actually functional.
- Check the Web Server Process: Log in to your server via SSH and check if Nginx or Apache is running.
- For Nginx:
sudo systemctl status nginx - For Apache:
sudo systemctl status apache2(orhttpdon CentOS)
- For Nginx:
- Monitor Resource Usage: Use the
toporhtopcommand to see if the server is under extreme load. If the load average is significantly higher than the number of CPU cores, the server is likely dropping connections because it is overwhelmed. - Test Connectivity Locally: Run a curl command from within the server to see if it responds to itself:
curl -I http://localhost. If this fails, the issue is internal to the web server software.
Step 2: Whitelist Cloudflare IP Addresses
Because Cloudflare acts as a proxy, all traffic to your origin server appears to originate from Cloudflare's IP ranges. If your security software sees hundreds of requests per second from the same IP, it may trigger rate-limiting.
You must explicitly allow (whitelist) all Cloudflare IP ranges in:
- Iptables/UFW/Firewalld: The system-level firewall.
- Fail2Ban: A common security tool that bans IPs based on log patterns.
- Cloud Security Groups: If you are on AWS (EC2), Google Cloud, or Azure, ensure the Security Groups or Network ACLs allow traffic on ports 80 and 443 from Cloudflare IPs.
- CMS Plugins: WordPress security plugins like Wordfence or Sucuri sometimes block Cloudflare IPs if not configured correctly.
How to find Cloudflare IPs: Cloudflare publishes an official, up-to-date list of their IPv4 and IPv6 ranges. You should periodically check these to ensure your firewall is current.
Step 3: Check and Correct DNS Settings
A common mistake occurs during site migrations. If Cloudflare is trying to reach 192.0.2.1 but your new server is at 192.0.2.5, a 522 error is inevitable.
- Log in to your Cloudflare Dashboard.
- Navigate to the DNS tab.
- Verify that the A record for your root domain and the CNAME for
wwwpoint to the correct IP address of your origin server. - If you are using an IPv6 environment, check the AAAA records as well.
If you recently changed these settings, allow a few minutes for the changes to propagate globally through the Cloudflare network.
Step 4: Optimize KeepAlive Settings
Cloudflare uses "KeepAlive" headers to reuse existing connections for multiple requests, which significantly improves performance. If your server is configured to kill these connections too quickly, Cloudflare might try to use a connection that the server has already closed, leading to a timeout.
- For Nginx: Ensure
keepalive_timeoutis set to at least 60 seconds.http { keepalive_timeout 75s; keepalive_requests 1000; } - For Apache: Ensure
KeepAliveisOnand theKeepAliveTimeoutis sufficient.KeepAlive On MaxKeepAliveRequests 100 KeepAliveTimeout 60
Step 5: Mitigate Server Resource Exhaustion
If the 522 error is intermittent and happens during peak hours, your server is likely running out of "worker" processes or memory.
- PHP-FPM: If you are running a WordPress site, check your PHP-FPM logs. If you see "server reached max_children," the server is refusing new PHP requests. You may need to increase
pm.max_childrenin your PHP configuration. - Database Bottlenecks: A slow MySQL query can tie up a web server process. Use
SHOW PROCESSLIST;in MySQL to identify hanging queries that might be preventing the server from accepting new connections.
Advanced Diagnostics for Developers
If the basic steps don't solve the issue, you need to look at the network layer.
Using MTR or Traceroute
An MTR (My Traceroute) provides a report on network latency and packet loss between two points. To diagnose a 522 error, you should ideally run an MTR from your origin server towards a Cloudflare IP.
Look for:
- Packet Loss: If you see 10% or 20% loss at a specific hop, there is a network routing issue.
- Latency Spikes: High latency at the final hop indicates the server is struggling to process the packet.
Testing with Curl
You can bypass Cloudflare's cache and proxy by forcing a request directly to your origin IP while spoofing the host header:
curl -v -H "Host: yourdomain.com" http://your_origin_ip/
If this command times out, it confirms the problem is strictly on your server or hosting provider’s network. If it works instantly, the issue might be a very specific Cloudflare configuration or a regional routing problem.
Analyzing Server Logs
Your server logs are the "black box" of your website.
- Nginx:
/var/log/nginx/error.log - Apache:
/var/log/apache2/error.log
Look for "upstream timed out" or "connection refused" messages. If there are no logs corresponding to the time the 522 error occurred, it means the request never reached your web server software at all—it was stopped by the firewall or the network.
Platform-Specific Considerations
WordPress
WordPress is particularly susceptible to 522 errors due to heavy plugins. If you use a plugin that performs "loopback" requests (the server trying to talk to itself), it can exhaust available connections. Disabling heavy security plugins temporarily can help isolate the cause.
Cloudflare Pages and Workers
While rare, 522 errors can appear in Cloudflare Pages or Workers if they are fetching data from an external API. If the external API is slow or has blocked Cloudflare’s IP, the Worker will time out and return a 522. Always use a try-catch block and set a custom timeout in your fetch() calls within Workers to handle these scenarios gracefully.
How to Prevent Error 522 in the Future
Prevention is about resilience and monitoring.
- Implement Monitoring: Use tools like UptimeRobot or Cronitor to monitor your origin server's IP directly. This helps you know if the server is down before Cloudflare even reports it.
- Auto-Whitelisting Scripts: Use scripts that automatically pull Cloudflare's IP list and update your
iptablesorufwrules. Many open-source scripts on GitHub can automate this via a daily cron job. - Use a Load Balancer: If your traffic is growing, a single origin server becomes a single point of failure. Using Cloudflare's Load Balancing feature allows you to distribute traffic across multiple servers. If one times out, Cloudflare automatically routes traffic to a healthy one.
- Optimize Content: Reducing the size of your pages and using Cloudflare's "Tiered Cache" reduces the number of times Cloudflare has to "ask" your origin server for data, decreasing the load and the chance of a timeout.
Conclusion
Cloudflare Error 522 is a clear signal of a communication breakdown between the CDN edge and your hosting environment. While it can be frustrating, the resolution usually lies in ensuring your firewall is Cloudflare-friendly, your DNS is accurate, and your server has the breathing room to handle incoming requests. By following the systematic troubleshooting steps outlined above—starting from server health and moving toward network diagnostics—you can quickly identify the bottleneck and bring your site back online.
FAQ
Is a 522 error Cloudflare's fault? Almost never. A 522 error indicates that Cloudflare is trying to connect but your server is not responding. The issue is typically with your host, firewall, or server configuration.
Can an expired SSL certificate cause Error 522? Usually, an SSL issue results in a 525 (SSL Handshake Failed) or 526 (Invalid SSL Certificate) error. However, if the server's SSL configuration is so broken that it causes the web service to crash or hang during the handshake, it could theoretically manifest as a 522.
Why does my site work when I'm logged into my VPN? If you use a VPN, you might be hitting a different Cloudflare data center. If the 522 error is caused by a regional network routing issue between a specific Cloudflare data center and your host, changing your location via VPN might bypass the congested path.
Will upgrading my Cloudflare plan fix Error 522? No. Upgrading to a Pro or Business plan provides more features, but it does not change the fact that Cloudflare cannot reach your server. You must fix the underlying connectivity issue at the origin.
What is the "Cloudflare 15-second limit"? Cloudflare allows exactly 15 seconds for a TCP connection to be established. If your server doesn't send the SYN-ACK packet within this window, the 522 error is triggered. This limit is hard-coded and cannot be increased for security and performance reasons.
-
Topic: Error 522 · Cloudflare Support docshttps://developers.cloudflare.com/support/troubleshooting/http-status-codes/cloudflare-5xx-errors/error-522/?utm_auid=fa8e6640-36a7-11f0-8711-b7f14c433375
-
Topic: Resolving Cloudflare Error 522: Origin Connection Timed Out | Error Medichttps://errormedic.com/networking/cloudflare/resolving-cloudflare-error-522-origin-connection-timed-out
-
Topic: 522 Connection Timed Out - HTTP status code explainedhttps://http.dev/522?ref=httpgoats.com