Skip to content

Allowlisting Cloudflare IPs at the origin firewall: ufw, nftables, and cloud security groups

Once traffic rides the Cloudflare proxy, the origin should refuse everyone else on 80/443. How to build the allowlist in ufw, nftables, and cloud security groups — and keep it current when the published ranges change.

Putting Cloudflare in front of a site and leaving ports 80 and 443 open to the world buys you very little: anyone who learns the origin IP — old DNS history, a certificate transparency log, an email header — walks straight past the WAF, the rate limits, and the DDoS layer. The fix is mechanical: allow Cloudflare’s published IPv4 and IPv6 ranges on the web ports, deny everything else, and keep the list current, because the ranges are published, versioned, and occasionally change.

What’s happening

When a record is proxied, legitimate visitor traffic reaches your origin only from Cloudflare’s egress ranges. Cloudflare publishes those ranges at two plain-text URLs and through the API, and commits to announcing changes in advance. That makes a default-deny posture on the web ports practical: roughly fifteen IPv4 CIDRs and seven IPv6 CIDRs, small enough for any firewall.

Two boundaries to be clear about. First, the allowlist only covers the web ports — SSH, mail, and monitoring ports are separate decisions; scoping the deny to 80/443 keeps you from locking yourself out. Second, allowlisting proves the packet came from Cloudflare’s network, not that it came through your zone. Any Cloudflare customer’s traffic egresses from the same ranges, so a determined attacker can still reach your origin by putting your IP behind their own zone. Treat the allowlist as the coarse filter and add Authenticated Origin Pulls (mTLS between Cloudflare and your origin) when that residual path matters — see origin certificates end to end.

What to check first

  • Everything that must keep reaching the origin directly. Monitoring probes, deploy webhooks from CI, cron jobs on other hosts, backup pulls. Inventory them before the deny rule, not after the pager goes off.
  • Any grey-clouded hostname pointing at this server. A DNS-only record for mail. or direct. still needs its ports reachable; only the proxied web traffic funnels through Cloudflare.
  • Where the firewall actually lives. On a cloud VM behind a security group, ufw rules and security group rules both apply; decide which layer owns this policy so you are not debugging two.

How to verify

Pull the current ranges and confirm what your firewall holds today:

# The published ranges
curl -s https://www.cloudflare.com/ips-v4
curl -s https://www.cloudflare.com/ips-v6

# Same data via the API
curl -s https://api.cloudflare.com/client/v4/ips

# What does the host firewall currently allow on 443?
sudo ufw status numbered | grep 443
sudo nft list ruleset | grep -A3 'cloudflare'

After applying rules, prove the deny works from any non-Cloudflare host:

# Should connect fine through the proxy
curl -sI https://www.example.com/ | head -3

# Should time out: direct-to-origin from an arbitrary network
curl -m 5 -skI https://<origin-ip>/ -H 'Host: www.example.com'

The second command timing out while the first returns 200 is the whole point of the exercise.

The fix

  1. ufw: loop the ranges in, then deny the ports. ufw evaluates rules in order, so insert the allows before a final deny:

    for ip in $(curl -s https://www.cloudflare.com/ips-v4; curl -s https://www.cloudflare.com/ips-v6); do
      sudo ufw allow proto tcp from "$ip" to any port 80,443 comment 'cloudflare'
    done
    sudo ufw deny 80/tcp
    sudo ufw deny 443/tcp
    sudo ufw status numbered
  2. nftables: use a set, not twenty rules. Sets keep the policy readable and make refreshes atomic:

    sudo nft add table inet cf
    sudo nft add set inet cf cf4 '{ type ipv4_addr; flags interval; }'
    sudo nft add set inet cf cf6 '{ type ipv6_addr; flags interval; }'
    sudo nft add element inet cf cf4 "{ $(curl -s https://www.cloudflare.com/ips-v4 | paste -sd, -) }"
    sudo nft add element inet cf cf6 "{ $(curl -s https://www.cloudflare.com/ips-v6 | paste -sd, -) }"
    sudo nft add chain inet cf input '{ type filter hook input priority 0; }'
    sudo nft add rule inet cf input tcp dport { 80, 443 } ip saddr @cf4 accept
    sudo nft add rule inet cf input tcp dport { 80, 443 } ip6 saddr @cf6 accept
    sudo nft add rule inet cf input tcp dport { 80, 443 } drop

    Persist the result in /etc/nftables.conf so a reboot does not silently reopen the ports.

  3. Cloud security groups: allowlist at the edge of the VPC. In AWS, put the CIDRs in a customer-managed prefix list and reference it from the security group rule for 80/443 — one list, many groups, and updates land everywhere at once. The 22 entries fit comfortably inside default rule quotas. Same pattern for DigitalOcean cloud firewalls, GCP firewall rules, and Azure NSGs: allow the ranges inbound on the web ports, remove the 0.0.0.0/0 rule for those ports.

  4. Keep the list current. The ranges change rarely, but “rarely” is not “never”. A weekly cron or systemd timer that fetches both URLs, diffs against the stored copy, and alerts on change is enough. Alert-then-apply beats blind auto-apply: a transient fetch failure that empties the list must never become an empty allowlist.

    curl -s https://www.cloudflare.com/ips-v4 -o /tmp/cf-ips-v4.new \
      && [ -s /tmp/cf-ips-v4.new ] \
      && diff -u /etc/cloudflare/ips-v4 /tmp/cf-ips-v4.new
  5. Re-run the direct-to-origin test. From a network that is not Cloudflare and not your office VPN, confirm the timeout. Put that test in monitoring so a regression pages someone instead of waiting for an incident to reveal it.

When this approach is wrong

On shared hosting you do not control the firewall — skip this and rely on Authenticated Origin Pulls. If the origin should not be reachable from the internet at all, a Cloudflare Tunnel is the stronger answer: outbound-only connections, no inbound ports, no allowlist to maintain. And if multiple non-Cloudflare services share the host — mail, SSH, an API consumed directly — scope the rules per port rather than adopting a host-wide default deny you will fight forever.

Common mistakes

  • Forgetting IPv6. If the host has an AAAA record or the LB fronts IPv6, allowlisting only ips-v4 leaves half the door open — or blocks half the legitimate traffic.
  • Hardcoding the ranges once, in 2022. Stale lists fail in the worst direction: new Cloudflare ranges get dropped and a slice of real users see timeouts that are miserable to diagnose.
  • Denying all inbound instead of 80/443. That locks out your own SSH session mid-change. Scope the deny.
  • Treating the allowlist as authentication. Other tenants share those egress ranges; pair with Authenticated Origin Pulls where it matters.
  • Applying at the host but not the cloud firewall (or vice versa) and later “fixing” a mystery block by opening the wrong layer to the world.

Prevention / maintenance checklist

  • Range-refresh job running weekly, alerting on diff, with the fetch validated non-empty before use.
  • Direct-to-origin reachability test in external monitoring, expected result: blocked.
  • Firewall rules commented (cloudflare) so the next admin knows why 22 CIDRs are allowed.
  • Authenticated Origin Pulls enabled on zones where origin exposure is a real threat.
  • Quarterly: compare the live ruleset against www.cloudflare.com/ips/ as part of the zone review.

A Cloudflare migration with a written rollback plan, or a zone that gets reviewed quarterly instead of drifting — that is the operating discipline Stack Harbor sells under Managed Cloud Ops; book an infrastructure review.