Cloudflare Error 522: Connection Timed Out

The TCP connection to the origin server timed out before receiving a response.

The Cloudflare 522 "Connection Timed Out" error occurs when Cloudflare successfully establishes a TCP connection with your origin server, but this connection times out before the server responds. Unlike error 521 where the connection is immediately refused, 522 indicates your server is reachable but too slow to respond.

This error is particularly frustrating because it can be intermittent and difficult to reproduce. It typically occurs during peak load, when the server is overloaded, or in case of network issues between Cloudflare and your infrastructure. Cloudflare's default timeout is 100 seconds for establishing a connection.

In this in-depth guide, we analyze the technical causes of 522, advanced diagnostic methods to identify the source of the problem, and optimizations to apply to ensure acceptable response times. Continuous monitoring is essential to detect these slowdowns before they become errors.

Main causes of error 522

Connection timeout can have several origins, often related to performance or network:

How to diagnose error 522

A systematic diagnosis identifies whether the problem comes from the server, network, or configuration:

Solutions to fix error 522

Solutions depend on the cause identified during diagnosis:

Optimized Nginx configuration for Cloudflare

This Nginx configuration optimizes critical settings to avoid 522 timeouts:

# /etc/nginx/nginx.conf - Optimizations for Cloudflare 522

worker_processes auto;
worker_rlimit_nofile 65535;

events {
    worker_connections 4096;
    multi_accept on;
    use epoll;
}

http {
    # Keepalive for persistent connections
    keepalive_timeout 75;
    keepalive_requests 1000;

    # Generous timeouts
    client_body_timeout 60;
    client_header_timeout 60;
    send_timeout 60;

    # Optimized buffers
    client_body_buffer_size 128k;
    client_header_buffer_size 4k;

    # Upstream connections (PHP-FPM)
    upstream php {
        server unix:/run/php/php-fpm.sock;
        keepalive 32;
    }

    # FastCGI timeouts
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
}

This configuration increases simultaneous connections, enables persistent connections with timeouts adapted to Cloudflare, and optimizes communications with PHP-FPM. Adjust values according to your server resources.

Preventing 522 errors

Prevention involves continuous monitoring and properly sized infrastructure:

522 verification checklist

  • CPU and RAM load under 80% in normal conditions
  • keepalive_timeout configured between 60 and 75 seconds
  • worker_connections sufficient for traffic
  • Firewall in REJECT mode, not DROP
  • Local response time under 2 seconds
  • Active response time monitoring

Frequently asked questions about error 522

What is the difference between Cloudflare errors 521 and 522?

Error 521 indicates the connection is immediately refused (server down or port closed). Error 522 means the TCP connection is established but times out before getting a response (server too slow). 521 is an availability problem, 522 is a performance problem.

What is Cloudflare's default timeout?

Cloudflare waits 100 seconds to establish a TCP connection and receive an HTTP response. This delay cannot be modified on free plans. Enterprise plans allow adjusting these timeouts.

522 only appears under load. What to do?

This is typical of an undersized server. Optimize your configuration (PHP-FPM, Nginx workers), add caching, or increase server resources. Consider a load balancer to distribute the load.

How to know if the problem is network-related?

Test with MTR from your server to Cloudflare IPs (173.245.48.0/20). Packet loss or high latency indicates a network issue. Contact your host if the problem persists.

Can 522 errors be caused by a misconfigured CDN?

Yes, if you have another CDN or proxy in front of your server in addition to Cloudflare, timeouts can compound. Ensure each layer has compatible timeouts and connections are properly routed.

How to monitor effectively to prevent 522s?

Use MoniTao to monitor your site through Cloudflare with frequent checks. Configure alerts on response time (e.g., alert if > 5 seconds) to detect slowdowns before they cause timeouts.

Conclusion

Cloudflare error 522 is a signal that your origin server is struggling to respond in time. Whether it's overload, suboptimal configuration, or network issues, solutions exist and generally involve optimizing your infrastructure and server configuration.

The key to avoiding 522 errors is proactive monitoring combined with properly sized infrastructure. MoniTao allows you to continuously track response times and receive alerts at the first signs of slowdown, well before your users see timeout errors.

Ready to Sleep Soundly?

Start free, no credit card required.