skip to content

Behind Cloudflare's proxy, why should an origin firewall accept web traffic only from Cloudflare's IP ranges, and how is that list kept current?

level: juniorimportance: must knowfreq 65%

answer

  1. hiding is not blocking
  2. published ranges, everything else dropped
  3. www.cloudflare.com/ips and the API
  4. few sources, high volume, CF-Connecting-IP

basics

~20 s

Proxying hides the origin's address but cannot stop anyone who already knows it. Accepting web traffic only from Cloudflare's published ranges makes the proxy the only way in; sync the list from www.cloudflare.com/ips or the API's /client/v4/ips endpoint.

solid answer

~50 s

A proxied (orange-cloud) record answers DNS with Cloudflare's anycast addresses, but that only hides the origin. Anyone who learns the real IP from old DNS, a mail record or a log can still connect to it directly and skip the WAF, rate limiting rules and DDoS protection. So the origin's firewall should accept HTTP and HTTPS only from Cloudflare's published ranges, plus the partner or monitoring addresses you deliberately trust, and drop everything else. Take the ranges from `www.cloudflare.com/ips` or `GET https://api.cloudflare.com/client/v4/ips` and refresh them on a schedule: they change rarely, and a new range is added to the list before it carries traffic. Plan for two side effects: the origin now sees a few source addresses at high volume, so its own per-IP throttling must not block Cloudflare, and the visitor's address arrives in `CF-Connecting-IP`.

code

bash · 2 lines
bash
# Cloudflare's current IPv4 and IPv6 ranges, the source for the origin allowlist
curl -s https://api.cloudflare.com/client/v4/ips

go deeper

for a junior

Recall that the orange cloud hides the origin's IP but does not block anyone who already has it, and that the fix is a firewall accepting only Cloudflare's published ranges.

for a middle

Explain where the list comes from, why it must be synced automatically, and how CF-Connecting-IP replaces the socket address once every visitor arrives from Cloudflare.

for a senior

Show the failure modes you would watch for: a stale list causing partial 522s, origin throttling banning Cloudflare, and a spoofed CF-Connecting-IP accepted from a direct connection.

for a principal

Treat the allowlist as the floor, not the proof: frame which origins also need Authenticated Origin Pulls or a Tunnel, and who owns the trusted-partner exceptions.

## Why proxying alone is not enough When a DNS record is **proxied** (the orange cloud), a lookup returns Cloudflare's **anycast** addresses instead of the origin's real one. Visitors connect to Cloudflare, Cloudflare applies its cache, WAF, rate limiting rules and DDoS protection, and only then opens its own connection to the origin. That protects only the traffic that takes this path. The origin still has a public IP address and still listens on its web ports. Anyone who learns the address, from DNS history, a mail server on the same host, a forgotten DNS-only record or a log, can send requests straight to it, and **none** of the edge controls ever see them. Cloudflare's own guidance is to block all traffic that does not come from its IP addresses or from partners, vendors and applications you trust. ## What the allowlist looks like At the origin's host or network firewall, the rule set has two halves: - **Accept** TCP on the web ports (80 and 443, or whichever proxied ports you use) from each Cloudflare IPv4 and IPv6 range. - **Accept** the few other sources you deliberately trust: a partner's fixed range, your uptime monitor, your own deploy or admin network. - **Drop** every other packet aimed at those ports, so a direct connection never completes. - Leave management access (SSH, a database port) to its own, narrower rules; the Cloudflare ranges are for proxied web traffic only. The ranges are shared by **all** proxied hostnames on Cloudflare, so this rule proves a request came through Cloudflare's network, not through your zone in particular. Authenticated Origin Pulls with your own certificate, or a Cloudflare Tunnel, close that remaining gap; the allowlist is the floor they build on. ## Keeping the list current Cloudflare publishes the ranges that connect to origins at `www.cloudflare.com/ips` and through the API. It uses other addresses for other products, but those do not open connections to your origin, so the published list is the whole set you need (unless the account uses dedicated egress addresses, below). 1. Fetch the list from `https://api.cloudflare.com/client/v4/ips`; the JSON carries the IPv4 ranges in `result.ipv4_cidrs` and the IPv6 ranges beside them. 2. Diff it against the firewall's current set on a schedule, in the same automation that manages the firewall, rather than pasting it once by hand. 3. Apply additions promptly. Changes are rare, and a new range is listed **before** it goes into production, so a regular sync picks it up ahead of traffic. 4. Alert on a failed or empty fetch instead of applying it; an empty list applied blindly would drop every visitor. If the list falls behind, the failure is partial and confusing: visitors routed through data centres that use the missing range get connection errors such as **522 Connection timed out**, while everyone else is fine. ## Side effects at the origin | Before the proxy | Behind the proxy | |---|---| | Each visitor is a separate source address | All visitors arrive from a small set of Cloudflare addresses | | The socket's peer address is the visitor | The visitor's address is in the `CF-Connecting-IP` header | | Per-IP throttling at the origin is meaningful | Per-IP throttling at the origin can block Cloudflare itself | Three consequences follow: - **Origin-side rate limits and intrusion tools** must not throttle or ban Cloudflare's addresses; to the origin they look like a few sources sending a lot of traffic. Security plugins on the origin need the same allowance. - **Logging and per-user limits** should read the visitor's address from `CF-Connecting-IP`, which Cloudflare sends only on its own edge-to-origin traffic. - Trust that header **only** on connections whose peer is in Cloudflare's ranges. A client connecting directly can send any value in it, which is one more reason the allowlist has to be in place. ## Beyond the shared ranges Enterprise zones have options that shrink or replace the shared pool, such as **Dedicated CDN Egress IPs**, which connect to the origin from addresses reserved for the account. For everyone else, the published ranges plus a stronger proof of origin (Authenticated Origin Pulls with your own certificate, or a Tunnel) are the standard pattern. ## Checking that it works - From a machine outside Cloudflare, send a request straight to the origin's address; the connection should time out or be refused rather than return a page. - Read the origin's access log: every web request should now come from a Cloudflare range, and a source outside them means a rule or a second network interface was missed. - Cover IPv6 as well as IPv4. A server with a public IPv6 address and no matching rule is still wide open, however tight the IPv4 rules are. - Apply the same rules to staging and other hostnames on the same server, not only to the production name.

  • Why must the origin not trust CF-Connecting-IP on every request?
    Cloudflare sets `CF-Connecting-IP` only on traffic from its edge to your origin, but a client that connects directly can send the same header with any value. Restore the visitor's address from it only when the connection's peer is in Cloudflare's ranges. With the allowlist in place that holds for all web traffic, so the header becomes trustworthy.
  • Which sources besides Cloudflare's ranges might stay on the allowlist?
    Only those you deliberately trust: a partner or vendor with a fixed range that calls the origin directly, your own uptime monitor, and your deploy or admin network. Each one is a path around the edge controls, so keep the list short, documented and reviewed, and prefer sending even those callers through Cloudflare when they can be.

  • A proxied record makes the origin unreachable, so the firewall can stay open.
  • Cloudflare's IP ranges never change, so pasting them once is enough.
  • Allow every address Cloudflare announces, including its other products' ranges.
  • Keep the origin's per-IP rate limiting unchanged after moving behind the proxy.
  • Trust CF-Connecting-IP from any client to log the visitor's address.