Decoding Cloudflare Error 520: A Comprehensive Guide to Origin Server Issues
In the modern web landscape, proxy layers are integral to efficient web infrastructure. These intermediary servers act as crucial traffic handlers between clients and origin servers. Cloudflare, a prominent name in this field, functions as a reverse proxy, processing millions of connections daily. It applies robust security filtering and forwards sanitized requests to origin servers. This sophisticated architecture introduces a unique communication challenge: the need for seamless alignment between two distinct HTTP conversations.
When a visitor accesses your Cloudflare-proxied website, a series of network operations unfold:
- Client ↔ Cloudflare: The visitor’s browser establishes a secure TLS connection with Cloudflare’s edge server, ensuring encrypted data transmission.
- Cloudflare ↔ Origin: Cloudflare initiates a separate connection to your origin server, retrieving the requested content.
- Response Relay: The origin server’s response traverses back through Cloudflare to the visitor, completing the data exchange.
Error 520 arises when step 2 or 3 encounters failures that don’t align with standard HTTP status codes. This can occur when the origin server crashes unexpectedly, sends malformed headers, or violates HTTP protocol expectations. In such scenarios, Cloudflare, encountering an unexpected response, lacks a specific error to return, resulting in the generic 520 error.

Understanding Error 520 Through the OSI Model
To fully grasp the nature of Error 520, it’s essential to examine the different layers of the Open Systems Interconnection (OSI) model:
| Layer | Function | 520 Failure Modes |
|---|---|---|
| Layer 4 (Transport) | Manages TCP connection establishment and maintenance. | Connection resets, timeout mismatches, SYN floods impacting connection reliability. |
| Layer 5 (Session) | Controls connection persistence and session management. | KeepAlive failures, premature connection closures disrupting ongoing sessions. |
| Layer 6 (Presentation) | Handles TLS/SSL encryption and decryption for secure communication. | Handshake failures, certificate errors, cipher mismatches compromising secure connections. |
| Layer 7 (Application) | Deals with HTTP protocol and application-level communication. | Malformed headers, empty responses, invalid status codes violating HTTP standards. |
While most 520 errors originate at Layer 7 due to application-level HTTP violations, underlying issues in lower layers can also manifest in similar ways.
The TCP Handshake: A Deep Dive
Before HTTP communication can commence, a reliable TCP connection must be established through a process known as the three-way handshake (SYN, SYN-ACK, ACK). This handshake forms the bedrock of communication. Cloudflare’s edge servers initiate this handshake with your origin server for every request.
TCP Timeout Configurations
Cloudflare employs aggressive timeouts to optimize performance. If your origin server doesn’t respond to a SYN request within a specific timeframe, or if the connection remains idle for too long, Cloudflare interprets this as a failure. Key timeout thresholds include:
- Connection Timeout: Typically ranges from 15 to 30 seconds for the initial connection establishment.
- Idle Timeout: A maximum of 300 seconds (5 minutes) is allowed for idle connections.
- Total Request Timeout: This varies based on the Cloudflare plan and configuration.
Origin servers with shorter timeout configurations, often found in default Apache/Nginx setups, can create race conditions. In these situations, the server might consider a connection active, while Cloudflare has already timed out and terminated the connection.
The Technical Fix:
# nginx.conf - Ensure timeouts exceed Cloudflare's expectations
keepalive_timeout 300s; # Match Cloudflare's maximum
proxy_connect_timeout 60s; # Time to establish connection
proxy_send_timeout 300s; # Time to send request
proxy_read_timeout 300s; # Time to wait for response
HTTP Protocol Violations: The Header Problem
The HTTP/1.1 and HTTP/2 specifications enforce strict rules regarding header formatting. Cloudflare’s parser rigorously enforces these rules, rejecting responses that browsers might otherwise tolerate.
Header Size Constraints
Cloudflare imposes specific limits on header sizes to prevent denial-of-service (DoS) attacks:
- Total Header Size: The maximum allowable size for all headers combined is 32 KB.
- Individual Header: Each individual header is limited to a maximum size of 16 KB.
While these limits protect against header bloat attacks, they can also impact legitimate applications that use verbose cookies or include extensive debug information in headers.
Real-World Scenario: Imagine a WordPress site with 20 plugins, each setting 2 KB of cookies, combined with analytics tracking and authentication tokens. This could easily exceed the 16 KB limit. Although the origin server accepts and returns these headers, Cloudflare rejects the response with a 520 error.
Header Validation Failures
Beyond size constraints, Cloudflare validates header syntax according to strict rules:
- Character Encoding: Non-ASCII characters are prohibited in header names (RFC 7230).
- Line Endings: CRLF (carriage return line feed) is required; LF-only line endings are rejected.
- Duplicate Headers: Some headers must be single-valued; duplicates are not allowed.
- Empty Header Names: Empty header names are strictly prohibited.
Legacy applications or custom middleware often generate technically invalid headers that function correctly in direct connections but fail when processed by Cloudflare’s strict parser.
The Empty Response Dilemma
One of the most common triggers for Error 520 is when origin servers accept the TCP connection and receive the HTTP request but return absolutely nothing in response—no status line, no headers, no body.
Root Causes of Empty Responses
- Application Crashes: PHP fatal errors, Python exceptions, or Node.js unhandled rejections that terminate the process after connection acceptance but before response generation.
- Resource Exhaustion: Memory limits hit mid-request, triggering the operating system’s out-of-memory (OOM) killer or graceful degradation to an empty response.
- Middleware Failures: Reverse proxies (Varnish, HAProxy) with backend health check failures returning empty 502/503 errors that get lost in translation.
- Intentional Security: Some Web Application Firewall (WAF) configurations return empty responses to suspicious requests, inadvertently triggering 520 errors for legitimate Cloudflare traffic.
Diagnostic Approach:
# Test origin response directly, bypassing all intermediaries
curl -v -H "Host: yourdomain.com" http://origin-ip/path \
--connect-timeout 30 \
--max-time 60 \
-w "\nHTTP Code: %{http_code}\nSize: %{size_download}\n"
# Look for empty downloads (0 bytes) or connection closed
SSL/TLS: The Encryption Handshake Complexity
When Cloudflare connects to your origin server via HTTPS (highly recommended), an additional handshake occurs—the TLS negotiation. This negotiation must succeed before HTTP communication can begin.
Certificate Validation Modes
| Cloudflare Mode | Origin Requirement | 520 Risk |
|---|---|---|
| Off | No encryption | Low (but highly insecure) |
| Flexible | HTTP only | Medium (encryption downgrade, not recommended) |
| Full | HTTPS, any certificate | Low |
| Full (Strict) | HTTPS, valid certificate signed by a trusted CA | Low (if certificate is valid) |
Common 520 scenarios related to SSL/TLS:
- Expired Certificates: The origin server presents a certificate that is past its validity date.
- Self-Signed Certificates in Strict Mode: Full (Strict) mode rejects self-signed certificates (not signed by a trusted Certificate Authority).
- SNI Mismatch: The certificate doesn’t cover the requested hostname.
- Protocol Version: The origin server requires TLS 1.0, while Cloudflare’s minimum is 1.2.
Debug SSL Handshake:
# Detailed TLS debugging
openssl s_client -connect origin-ip:443 -servername yourdomain.com \
-tls1_2 -showcerts -status
# Check certificate dates
openssl x509 -in certificate.crt -noout -dates
HTTP/2 and Protocol Negotiation
Modern Cloudflare deployments utilize HTTP/2 for connections to origin servers whenever possible. If your origin server advertises HTTP/2 support via ALPN (Application-Layer Protocol Negotiation) but then fails to handle HTTP/2 frames correctly, Cloudflare returns Error 520.
The ALPN Problem:
- Cloudflare connects and the origin server responds with
h2in the ALPN extension, indicating HTTP/2 support. - Cloudflare sends HTTP/2 frames (a binary protocol).
- The origin server expects HTTP/1.1 (a text-based protocol) and misinterprets the HTTP/2 frames.
- The connection fails, and Cloudflare returns Error 520.
Resolution: Properly configure HTTP/2 on the origin server, or disable HTTP/2 to Origin in the Cloudflare dashboard: Speed → Optimization → HTTP/2 to Origin: Off.
The KeepAlive Connection Pool
Cloudflare maintains persistent connections (KeepAlive) to origin servers for performance reasons, reusing TCP connections for multiple requests. Origin servers must properly handle these persistent connections, respecting Connection: keep-alive headers and avoiding premature socket closures.
Misconfigured origin servers that close connections after single requests, or that have mismatched KeepAlive timeouts, can cause intermittent Error 520 errors that are difficult to reproduce in testing.
Advanced Diagnostics: Packet Capture
When standard diagnostic methods fail, packet capture can reveal the actual network behavior.
# Capture traffic on origin server (requires root)
sudo tcpdump -i eth0 -w /tmp/cloudflare-traffic.pcap \
host cloudflare-ip-range and port 443
# Analyze with Wireshark
# Look for: TCP RST packets, TLS alerts, HTTP malformed messages
Key indicators to look for in packet captures:
- RST Packets: Abrupt connection termination (firewall or crash).
- TLS Alert 40: Handshake failure (certificate/negotiation issue).
- HTTP 0.9 Responses: Rejection of the ancient HTTP 0.9 protocol version.
- Truncated Responses: Indicates a server crash mid-transmission.
The Proxy Chain Complexity
Many origin servers reside behind multiple proxy layers: Cloudflare → Load Balancer → Cache → Application Server → Database. Each hop introduces potential Error 520 triggers:
- Load balancers marking healthy nodes as failed due to misconfigured health checks.
- Cache layers returning empty responses for cache misses.
- Application server queue overflows during traffic spikes.
Isolating which layer is failing requires systematic bypass testing—direct access to the origin, then through each intermediary, identifying where the chain breaks.
Technical Rigor in 520 Resolution
The generic nature of Error 520 demands a systematic, layer-by-layer diagnostic approach. The error is not random—it signals a specific issue: your origin server violated HTTP protocol expectations, crashed during request handling, or has misconfigured network parameters. Understanding the mechanics of TCP, TLS, and HTTP enables precise identification and permanent resolution of the issue.
For infrastructure teams managing multiple origin servers, automated monitoring from diverse network perspectives—validating not just uptime but protocol compliance—prevents Error 520 errors before they impact users.

Diagnosing complex 520 errors requires thorough testing from multiple network vantage points to isolate layer-specific problems. When verifying origin behavior from diverse geographic locations or testing protocol compliance across varying network paths, a robust infrastructure is essential. Understanding how Cloudflare’s distributed edge interacts with your origins is paramount for long-term reliability. Ensure your infrastructure adheres to protocol standards, validate your network configuration, and monitor for potential issues preemptively.