Cloudflare Error 521: What It Means and How to Fix It

Cloudflare error 521 means Cloudflare received the visitor's request but couldn't open a TCP connection to the origin server, usually on port 80 or 443. The visitor's browser isn't the problem, and Cloudflare usually isn't broken. The failure sits between Cloudflare and the server behind it. This guide explains what causes it, how to fix it if you own the site, and what to do if your scraper or integration hits it.

TL;DR

  1. Cloudflare returns 521 when the origin refuses Cloudflare's TCP connection.
  2. Site owners should check the web server process, ports, firewall, SSL/TLS mode, and DNS.
  3. Scrapers and integrations should treat 521 as upstream availability failure, not bot detection.
  4. Retry with backoff and keep 521 separate from blocked-rate metrics.

What is Cloudflare error 521?

Cloudflare error 521 is Cloudflare's "Web server is down" response. It appears when Cloudflare tries to open a TCP connection to the origin server, but the origin refuses the connection or has nothing listening on the required port.

The origin server is the backend server that hosts the website Cloudflare protects. Cloudflare sits in front of that server as a reverse proxy, which means it receives visitor requests first and forwards them to the origin.

The request path looks like this:

Visitor → Cloudflare edge → Origin server

With 521, the break happens between Cloudflare and the origin. The browser reached Cloudflare. Cloudflare reached the origin IP and attempted to open a connection. The origin refused it.

A normal 500 Internal Server Error means the origin received the request and returned a server-side failure.

521 fails earlier. Cloudflare generates the response because it cannot complete the origin connection. That is why NginxApache, or IIS access logs may show nothing for the failed request. The request never reached the web server layer that would log it.

That early failure also explains why 521 gets confused with nearby Cloudflare errors:

  1. 520 means Cloudflare got an empty or malformed response.
  2. 522 means the connection timed out.
  3. 523 means Cloudflare could not reach the origin IP.
  4. 524 means the origin accepted the connection but took too long to respond.

Causes of Cloudflare error 521

521 points to the origin side of the Cloudflare-to-origin connection. Your next step depends on whether you control that origin.

If you own the affected site

If you own the site, start with infrastructure. Most 521 incidents come from a stopped web server, blocked Cloudflare IPs, mismatched SSL/TLS settings, or stale DNS.

Origin web server crashed or stopped

NginxApacheIIS, or the app process may no longer be listening on port 80 or 443.

The machine can still respond to ping, accept SSH, and show as "running" in your cloud dashboard. None of that proves the web server is listening on the right port.

Common triggers include failed deployments, OOM kills after memory leaks, and services that crash without restart policies.

Origin firewall blocks Cloudflare IP ranges

Cloudflare can return 521 when origin-side security tools block legitimate Cloudflare IP addresses. This often happens after a firewall cleanup, a "temporary" office-IP allowlist, or a cloud security group change that leaves out Cloudflare's current IP ranges.

From your server's point of view, proxied traffic comes from Cloudflare IPs, not from each visitor's real IP.

Your hosting provider filters Cloudflare traffic

Some shared hosts and VPS providers filter traffic when they see heavy volume from a small set of source ranges. Because proxied traffic arrives through Cloudflare edge IPs, legitimate visitors can look noisy to automated abuse systems.

SSL/TLS mode doesn't match the origin

If Cloudflare is set to Full or Full (strict) mode, Cloudflare must reach the origin over HTTPS, commonly on port 443. If the origin only serves HTTP, blocks port 443, or has no HTTPS service listening there, the connection can fail as 521.

Don't overread certificate details here. Invalid, expired, revoked, or hostname-mismatched certificates usually produce 526, while SSL handshake failures usually produce 525.

DNS points to the wrong origin

After a migration, the A record in Cloudflare can still point to an old VPS, a decommissioned load balancer, or a staging box that does not serve the hostname. If that old origin is reachable but refuses the expected connection, Cloudflare can return 521.

If Cloudflare cannot reach the IP at all, expect 523. If the TCP handshake times out, expect 522.

If you scrape or integrate with the affected site

If you scrape or integrate with a site you don't control, your job is to separate an origin outage from bot detection.

A real 521 is not a scraper block

User-agent rotation, CAPTCHA handling, and browser fingerprint changes do not fix an origin that refuses Cloudflare's connection. If normal visitors see the same 521 page, your scraper probably did not cause it.

Treat 521 as upstream availability failure

In a data pipeline, 521 belongs with origin outages and Cloudflare-to-origin connectivity errors, not anti-bot events. If you count it as "blocked," your metrics will lie.

You will think your scraper is being detected when the target site is simply unavailable through Cloudflare.

Intermittent 521 usually means the origin is flapping

If most requests succeed but some return 521, the origin may be restarting, overloaded, misconfigured behind 1 load balancer node, or affected by a regional firewall rule. Retry with backoff before you declare the URL dead.

Don't confuse 521 with Cloudflare bot detection

A genuine 521 returns a 521 status code and Cloudflare's "Web server is down" page. A challenge page, a 200 OK response with a JavaScript interstitial, or access-control and rate-limit codes such as 1015 or 1020 point to anti-bot systems, not origin refusal.

Methods to fix Cloudflare error 521

How you fix 521 depends on whether you control the origin. Site owners need to restore the Cloudflare-to-origin connection. Everyone else should verify the error, retry cleanly, and keep the workflow moving.

If you own the affected site

Fix 1: Confirm the origin is actually serving traffic

Test the origin directly from a machine outside your hosting network. For HTTP, send the request to the origin IP with the expected Host header:

curl -v -H "Host: yoursite.com" http://ORIGIN_IP/

For HTTPS, use SNI and pin the hostname to the origin IP:

curl -vk --resolve yoursite.com:443:ORIGIN_IP https://yoursite.com/

If you get a connection refused or a timeout, the origin isn't accepting the connection. If you get 200 OK, the origin works for direct requests. The issue is likely Cloudflare IP filtering, routing, or the proxied path.

Fix 2: Allow Cloudflare's IP ranges on the origin

Pull the current IPv4 and IPv6 ranges from Cloudflare, then allow them in your host firewall, web server firewall module, and cloud security group on the ports your site uses, usually 80 and 443. For ufw, the pattern looks like this:

for ip in $(curl -s https://www.cloudflare.com/ips-v4); do sudo ufw allow from $ip to any port 443; done

Repeat the same logic for port 80 and IPv6 if your origin uses them. Then, retest the affected URL after the rules apply.

Fix 3: Restart and verify the web server process

Check which services are listening before and after the restart:

sudo ss -tlnp | grep -E ":80|:443"

If nothing is listening on the expected port, restart NginxApacheIIS, or your app service. Then watch the service for more than 1 minute.

A service that starts and dies in a crash loop can create the same user-facing error. Tail the service logs before you move on.

Fix 4: Align SSL/TLS mode with the origin

In Cloudflare SSL/TLS settings, match the mode to what your origin supports. For production, install a valid origin certificate and use Full (strict) when possible.

If the origin only has port 80 open, enable HTTPS at the origin before you force encrypted Cloudflare-to-origin traffic.

If the origin accepts HTTPS but the certificate is invalid, you're usually in 526 territory instead of 521.

Fix 5: Check DNS records and proxy status

Confirm the A or AAAA record points to the live origin IP. If you recently migrated hosts, do not trust memory. Inspect the record.

A stale record causes 521 when the old origin is reachable but refuses the connection. Unreachable or unroutable origins usually show up as 523, and connection timeouts usually show up as 522.

You can briefly switch the record from proxied to DNS-only in Cloudflare. If the site loads directly, the origin is alive and the Cloudflare-to-origin path needs attention.

Fix 6: Ask your host about Cloudflare filtering

If your origin process, firewall, SSL/TLS mode, and DNS all check out, open a ticket with your provider. Ask whether Cloudflare edge IP ranges are being blocked, rate limited, or blackholed before traffic reaches your server.

If you scrape or integrate with the affected site

If you don't own the site, you can't fix the origin. Your goal is to keep your workflow stable while the owner fixes it.

Confirm it is a real 521, not bot detection

Inspect the status code and response body. A real 521 returns Cloudflare's "Web server is down" page. If you get a challenge page or access-control and rate-limit codes such as 1015 or 1020, follow a Cloudflare scraper troubleshooting path instead.

Retry with exponential backoff

Treat 521 as transient first. Retry after 30 seconds, then 2 minutes, then 10 minutes, with jitter. Cap the sequence at 3 or 4 tries so a long outage does not burn the whole run.

Verify whether the outage is global

Test the URL from a clean residential IP in another region. A residential proxy pool can help with this sanity check because it lets you compare the same target from different real-world networks and regions.

If one region works and another gets 521, you are probably looking at origin-side filtering or routing instead of a full outage.

Skip the URL and resume the queue

A long scrape should not stall on 1 failing URL. Push the failed item to a retry queue, continue the job, and reprocess it later.

That keeps the pipeline moving while the target recovers.

Don't chase a client-side bypass

Headers, user-agent rotation, proxy swapping, and CAPTCHA handling do not repair an origin that refuses Cloudflare's connection. Use the checklist for your role instead.

Normal visitors can retry later, try another network to rule out local routing, or contact the site owner. If the site still returns 521, the website owner has to fix the origin.

How to prevent Cloudflare error 521

Prevention follows the same role split. Site owners reduce the chance that the origin refuses Cloudflare. Scrapers and integrations keep 521s from corrupting metrics, queues, and datasets.

If you own the affected site

Automate Cloudflare IP allow-listing

Pull Cloudflare's IPv4 and IPv6 lists on a schedule and re-apply firewall rules. Cloudflare IP ranges do not change often, but they can change. Stale allowlists are the quiet drift that creates 521s at the worst time.

Monitor the origin separately from Cloudflare

Use an uptime check that hits the origin IP directly with the right Host header. That catches origin failures before visitors report them through a Cloudflare error page.

Keep the web server under a supervisor

Use systemd with Restart=alwayspm2, Docker restart policies, or your platform's equivalent. A single crash shouldn't become a multi-hour outage because nobody restarted the process.

Use a stable SSL/TLS setup

Cloudflare Origin CA certificates and Full (strict) mode reduce drift between Cloudflare settings and origin behavior.

Certificate renewal problems are more likely to appear as 526, but a broken HTTPS listener can still contribute to a 521 when Cloudflare can't establish the origin connection.

Document firewall changes

A lot of 521 incidents start as "we tightened the firewall last week." Keep a small change log for security group, WAF, host firewall, and load balancer changes.

If you scrape or integrate with the affected site

If you're scraping or integrating against a Cloudflare-fronted site, you can't prevent the target's origin from going down. You can prevent that outage from breaking your pipeline.

Build 521 into your retry policy

Treat Cloudflare-origin 5xx errors as transient by default. A retry-with-backoff schedule such as 30 seconds, 2 minutes, and 10 minutes, with jitter, recovers many short incidents without manual work.

Separate availability failures from blocks

Classify what failed before deciding what to change:

  1. Tag 521522523, and 524 as upstream availability failures.
  2. Tag 10061015, and 1020 as access-control or rate-limit events.

Add an external sanity check

If the same target returns 521 only from your scraping infrastructure, compare it from another geography or network. That doesn't make the origin problem yours, but it tells you whether a route change can keep the job moving.

Cache the last successful response

For price monitoring, availability tracking, and other time-series workflows, a stale value with a clear flag is often better than an empty row. The dataset stays usable, and downstream consumers can decide how to handle the stale record.

Have a fallback source

If a single target is critical and 521-prone, build a secondary source into the pipeline. That could be a partner API, another public page, or a separate domain with the same data.

If you're dealing with a mix of real 521s and Cloudflare anti-bot blocks, Decodo Site Unblocker and Decodo Web Scraping API can handle retries, routing, and clean response delivery without moving that logic into core scraper code. They won't fix someone else's downed origin, but they do simplify the cases around it. For broader resilience patterns, see Decodo's guide to web scraping without getting blocked.

Conclusion

Cloudflare error 521 is an origin-side connectivity problem: Cloudflare tries to connect, and the origin refuses. If you own the site, diagnose in this order: origin process, firewall allowlist, SSL/TLS mode, then DNS. If you're scraping or integrating, classify it as upstream availability, retry with backoff, and keep it separate from anti-bot metrics. That same classification habit helps with other proxy error codes, where the right fix depends on where the request failed.

© 2018-2026 decodo.com (formerly smartproxy.com). All Rights Reserved