Cloudflare Error 520: Web server is returning an unknown error

Cloudflare reached your origin and something came back — but the reply was empty, truncated, or so broken the edge could not treat it as HTTP. Cousin of a generic 502, with Cloudflare’s branding.

What visitors see

Error 520
Web server is returning an unknown error
The web server reported a gateway-level error or sent a response Cloudflare could not understand.
Cloudflare Ray ID: …

Only orange-cloud traffic sees this page. Grey-cloud or a direct hit to the origin IP skips Cloudflare’s branded error — which is exactly how you diagnose whether the origin itself is sending garbage.

520 vs 502 vs 521 vs 522 vs 524 vs 525/526

CodeWhat Cloudflare managedTypical cause
520Connected; reply empty / invalid / unexpectedCrash mid-response, empty body, protocol mismatch, oversized headers
502Proxy got a bad upstream HTTP replySame family as 520; nginx/ALB often say 502 for the same root causes
521TCP to origin refusedNothing listening, firewall REJECT
522TCP to origin timed outFirewall DROP, wrong IP, host unreachable
524Connected; HTTP response too slowApp hung / slow DB — cousin of 504
525 / 526TLS handshake or cert validation failedOrigin not speaking TLS, cipher mismatch, expired/self-signed cert under Full (strict)
503Origin (or CF) said unavailableOverload, maintenance, drained pool

Rule of thumb: 520 ≈ “I got something, but it was not usable HTTP.” Fix the origin’s response shape — do not open more firewall ports (that is 521/522 territory) and do not raise read timeouts (that is 524).

Frequent real causes

Diagnose from outside and on the origin

# Bypass Cloudflare: grey-cloud the record, or hit the origin IP with the real Host:
curl -svo /tmp/origin.out --connect-timeout 5 \
  https://ORIGIN_IP/ -H "Host: example.com" -k
# Inspect what actually came back:
head -c 400 /tmp/origin.out; echo
curl -sI https://ORIGIN_IP/ -H "Host: example.com" -k

# Compare orange-cloud vs direct:
curl -sI https://example.com/          # through Cloudflare
curl -sI https://ORIGIN_IP/ -H "Host: example.com" -k

# On the origin around the Ray ID timestamp:
sudo journalctl -u nginx -u your-app -n 120 --no-pager
sudo tail -n 80 /var/log/nginx/error.log /var/log/nginx/access.log
# Look for upstream prematurely closed, segfaults, OOM, header-too-large

If direct-to-origin already returns an empty body, a truncated chunked stream, or non-HTTP bytes, Cloudflare 520 is expected — fix the origin before touching Cloudflare settings. Empty or garbage JSON bodies often show up client-side as Unexpected end of JSON input.

Fix checklist

  1. Confirm 520 vs neighbours — refused → 521; connect timeout → 522; slow but valid → 524; TLS fail → 525/526.
  2. Reproduce without Cloudflare — grey-cloud or curl origin IP + Host; capture the raw reply.
  3. Read origin error logs — crash, OOM, upstream reset, “header too large”, protocol errors at the Ray ID time.
  4. Stabilize the app — stop crash loops; add request timeouts; ensure every path returns a complete status + headers + body (or a deliberate close).
  5. Shrink headers — fewer/smaller cookies; drop debug headers; raise origin limits only after you know the size is legitimate.
  6. Align HTTP version / proxying — if you forced HTTP/2 to origin, try HTTP/1.1; fix ALPN; ensure the process on 443 really speaks TLS+HTTP.
  7. After replies look sane — purge Cloudflare cache if a bad edge page stuck; watch for residual 502 from other proxies in the chain.

Related

All Cloudflare error codes · Cloudflare Error 523 · 502 Bad Gateway · Cloudflare Error 521 · Cloudflare Error 522 · Cloudflare Error 524 · Cloudflare Error 525 · Cloudflare Error 526 · HTTP 503 · HTTP 504 · Unexpected end of JSON input · ECONNRESET · Anvil HTTP status · Wirebook: when sites get tired

Questions

What does Cloudflare Error 520 mean?

Cloudflare connected to your origin but received an empty, invalid, or unexpected response it could not treat as normal HTTP — often a crash mid-response or a protocol mismatch.

How is 520 different from 502?

Same family. 502 is the generic gateway status; 520 is Cloudflare’s branded page when the origin reply is empty or unusable.

How is 520 different from 521 or 522?

521 never got a TCP accept (refused). 522 never finished connecting (timeout). 520 connected and got a bad reply.

How is 520 different from 524?

524 means the origin was too slow. 520 means the reply arrived (or partially arrived) but was empty or invalid — not merely late.

Why does it work on the origin IP but 520 through Cloudflare?

Cloudflare is stricter about complete, well-formed responses and header size. Grey-cloud can hide intermittent truncations that the edge rejects.

Can oversized headers cause 520?

Yes. Giant Cookie / Set-Cookie / custom header sets can make Cloudflare reject the origin reply. Trim headers and compare with curl -sI.

What should I check first on the server?

Reproduce with curl to the origin IP + Host header, then read nginx/app error logs around the Ray ID for crashes, upstream resets, and header-too-large messages.

Published .

Try it

Related guides

Newest guides