Cloudflare Error 520 (Web server returns an unknown error): tracking it down on an AWS origin
Diagnosing Cloudflare error 520 with an AWS origin: application crashes and OOM kills, response headers and cookies over the limit, HTTP/2 to origin, Authenticated Origin Pulls, and comparing ALB logs with what Cloudflare saw.
If Cloudflare cannot connect at all, that is error 522 (silence) or 521 (refused).
Symptoms
The site works most of the time and then shows Error 520: Web server returns an unknown error. It can be repeatable for one URL or random across requests, and loading the origin directly in a browser may well work:
curl -sI https://app.example.com/problem-url | head -4
# HTTP/2 520
# server: cloudflare
# cf-ray: 8a1b2c3d4e5f6a7b-AMS
# The same request straight to the origin
curl -sv --resolve app.example.com:443:$(dig +short my-alb-123456.eu-west-1.elb.amazonaws.com | head -1) \
https://app.example.com/problem-url -o /dev/null 2>&1 | grep -E "^< HTTP|^< |curl: \("
# Look for: an empty reply, a dropped connection, or enormous headers
Cause
Cloudflare’s documentation lists several typical causes. On AWS these come up most:
- The application process dies mid-request. A PHP-FPM segfault, a process killed by the kernel’s OOM killer, workers restarting during a deploy. The connection is closed without a reply. Cloudflare’s own documentation singles out PHP applications for this.
- Response headers over 128 KB, most often from a flood of cookies (sessions, tracking, plugins setting a dozen
Set-Cookieheaders each). - An empty or invalid response: no status line, no body where one is expected, or non-standard headers.
- A faulty HTTP/2 setup on the origin. Cloudflare connects over HTTP/2 when the origin advertises it. If the server accepts the connection and then cannot handle the protocol, you get a 520.
- Authenticated Origin Pulls enabled in Cloudflare while the origin is not set up for it.
- A firewall or security plugin that cuts connections from Cloudflare addresses.
Fix
Step 1: Compare what the origin saw with what Cloudflare saw
This is the fastest way to separate an application crash from a malformed response. If ALB access logs go to S3, find the request from the time of the error and compare two fields:
elb_status_code |
target_status_code |
What it means |
|---|---|---|
| 200 | 200 | The origin answered correctly, so suspect the response format (headers, cookies) |
| 502 | - |
The target closed the connection without replying (a process crash). Cloudflare normally passes the 502 on, but check |
| no entry | no entry | The request never reached the ALB; see the error 522 runbook |
If you use Cloudflare Logpush, an OriginResponseStatus of 0 has two meanings. When CacheStatus is hit or revalidated, Cloudflare never contacted the origin and all is well. When it is miss or expired, the origin connection failed and that is your 520. Never read the zero on its own; check the cache status first.
Step 2: Look for crashes and OOM kills on the instance
# Processes killed by the OOM killer
sudo dmesg -T | grep -i -E "out of memory|killed process" | tail -5
# PHP-FPM: children exiting on a signal
sudo journalctl -u php-fpm --since "1 hour ago" | grep -i -E "signal|exited|segfault" | tail -10
# Nginx: upstream connections cut short
sudo grep -E "prematurely closed|upstream timed out|connection reset" /var/log/nginx/error.log | tail -10
# Memory right now (CloudWatch agent gives you the history)
free -m
If the OOM killer is taking out workers, fix the cause (a per-process memory limit, a leak, an undersized instance) rather than the symptom.
Step 3: Measure the response headers and the cookie count
curl -sD - -o /dev/null --resolve app.example.com:443:ORIGIN_IP \
https://app.example.com/problem-url | tee /tmp/headers.txt | wc -c
grep -ci "^set-cookie" /tmp/headers.txt
# Headers over 128 KB cause a 520. In practice trouble starts much earlier:
# a dozen Set-Cookie lines or tens of kilobytes of headers is already a warning sign.
Cut the number and size of cookies in the application, disable plugins that set dozens, and keep session data server-side rather than in a cookie.
Step 4: Rule out HTTP/2 trouble
As a test, turn off HTTP/2 to Origin in the Cloudflare dashboard (Speed, Settings, Protocol Optimization). If the 520s vanish, the origin advertises HTTP/2 but does not handle it properly. Review the server configuration (for nginx, the http2 directive on the 443 listener) and update the software.
Step 5: Check Authenticated Origin Pulls
With AOP on in Cloudflare, the origin must accept Cloudflare’s client certificate. On an ALB that means mTLS on the listener, on nginx it means ssl_client_certificate and ssl_verify_client. A mismatch gives a 520. While diagnosing, you can switch AOP off for a test hostname.
Step 6: A temporary workaround, and what support needs
To take pressure off users while you investigate, set the DNS record to DNS only (grey cloud). That exposes the origin address and removes Cloudflare’s protection, so treat it as a fix for hours, not days. If the problem persists, Cloudflare support will want the full URL, the cf-ray from the error, the output of https://your-domain/cdn-cgi/trace, and two HAR files (one with Cloudflare on, one with it temporarily paused).
Validation
# 1. No 520 on the problem URL across several requests
for i in 1 2 3 4 5; do
curl -s -o /dev/null -w "%{http_code} " https://app.example.com/problem-url
done; echo
# Expected: 200 200 200 200 200
# 2. Response headers are a sensible size
curl -sD - -o /dev/null https://app.example.com/problem-url | wc -c
# 3. No processes killed after a load test
sudo dmesg -T | grep -i "killed process" | tail -3
Turn on Cloudflare’s origin error notifications and add a CloudWatch alarm on instance memory. A 520 is easy to shrug off because it affects “only some requests”, yet it is often the first sign that an application is near the edge of what it can take.
Frequently asked questions about Cloudflare error 520
What does Cloudflare error 520 mean?
Is a 520 always my server's fault?
Why does my ALB never return a 520?
Related
- Cloudflare error 522 on AWS: no response, security groups, NACLs and DNS
- Cloudflare error 521 on AWS: connection refused, SSL mode and the 443 listener
- AWS ALB 502: target health check failures
- Cloudflare documentation: error 520
An unstable app behind Cloudflare on AWS?
Book a free 30-minute call. We trace intermittent 5xx errors to their source, from memory limits to ALB and Cloudflare configuration, and set up monitoring so you see the problem before your customers do.