When a site is proxied, every connection your origin sees comes from a Cloudflare edge address, and the visitor’s real IP rides in the CF-Connecting-IP request header. The fix has two halves and both matter: configure Nginx (ngx_http_realip_module) or Apache (mod_remoteip) to substitute that header for the connection address, and restrict that trust to connections arriving from Cloudflare’s published IP ranges — because a header anyone can type is only evidence when it comes from a party you trust. Do the first half without the second and you have handed IP spoofing to anyone who can reach your origin directly.
What’s happening
The proxy terminates the visitor’s connection at the edge and opens its own connection to your origin. At the TCP level, the client is Cloudflare. Everything keyed on connection IP — access logs, fail2ban, allow/deny lists, application rate limits, geo lookups — now sees a few dozen Cloudflare addresses instead of your actual visitors. One brute-forcer looks like all of Cloudflare; ban it and you ban everyone.
Cloudflare adds headers to compensate: CF-Connecting-IP (exactly one value — the client IP the edge accepted the connection from), X-Forwarded-For (a comma-separated chain that Cloudflare appends to, meaning an upstream client or proxy can seed it with junk), and CF-IPCountry if IP geolocation is enabled. On Enterprise there is also a managed transform to add True-Client-IP for appliances that expect that name. For origin configuration, prefer CF-Connecting-IP: single-valued, set by Cloudflare, no chain parsing. The published edge ranges live at https://www.cloudflare.com/ips/ and in the fundamentals docs — your config trusts exactly that list and nothing else.
What to check first
- Are your logs full of a small set of repeating IPs that resolve to Cloudflare? That is the symptom.
- Is the origin reachable directly from the internet? If yes, the trust boundary matters even more — and the firewall step below is not optional.
- Which services consume the IP: web server logs only, or also fail2ban, ModSecurity, and the application?
How to verify
# What does the origin see right now? (before the fix)
tail -5 /var/log/nginx/access.log
# 172.71.x.x / 104.x.x.x repeating = you are logging the edge, not visitors
# Confirm the header arrives with the real address
curl -s "https://example.com/" -H "X-Debug: 1" >/dev/null
# then check what a debug endpoint or the log shows for CF-Connecting-IP
# Spoofing check - can you inject the header when talking to the origin directly?
curl -sI --resolve example.com:443:<ORIGIN_IP> "https://example.com/" \
-H "CF-Connecting-IP: 203.0.113.99"
# If the origin log records 203.0.113.99, your trust boundary is broken
# After the fix: real visitor IPs in the log, cf-ray still joinable
tail -5 /var/log/nginx/access.log
The fix
-
Nginx — real_ip module, scoped to Cloudflare ranges. In
http {}context, list every Cloudflare range as trusted and name the header:# /etc/nginx/conf.d/cloudflare-realip.conf # one set_real_ip_from line per range from https://www.cloudflare.com/ips/ set_real_ip_from 173.245.48.0/20; set_real_ip_from 103.21.244.0/22; # ... remaining IPv4 ranges ... set_real_ip_from 2400:cb00::/32; # ... remaining IPv6 ranges ... real_ip_header CF-Connecting-IP;Then
nginx -t && systemctl reload nginx. Because the substitution only fires for connections fromset_real_ip_fromaddresses, direct visitors (or attackers) sending a fake header are ignored — that is the safety property doing its job. -
Apache — mod_remoteip. Enable the module and point it at the same list:
# /etc/apache2/conf-available/cloudflare-remoteip.conf RemoteIPHeader CF-Connecting-IP RemoteIPTrustedProxy 173.245.48.0/20 103.21.244.0/22 # ... remaining ranges, IPv4 and IPv6 ...Update
LogFormatto use%a(the substituted client address) rather than%hwhere needed, then reload. On cPanel servers there are platform-specific wrinkles — the cPanel/Nginx version of this setup covers those. -
Automate the range refresh. Cloudflare’s ranges change rarely, but “rarely” is not “never.” A monthly cron that pulls
https://www.cloudflare.com/ips-v4andips-v6, regenerates the config file, tests, and reloads keeps you honest. Configuration management (Ansible template + scheduled run) is the tidier version of the same idea. -
Close the side door: firewall the origin to Cloudflare ranges. Restoring the IP fixes attribution; it does not stop direct-to-origin traffic. Allow 80/443 from Cloudflare ranges (plus your management IPs), drop the rest. Now the trusted-header story is airtight: the only party who can reach the web ports is the one whose header you trust.
-
Point downstream consumers at the restored address. fail2ban jails, application rate limiting, and audit logging all work again once the web server substitutes the address — but double-check anything that reads
X-Forwarded-Foron its own. Standardize on the server-substituted remote address everywhere.
When this fix is wrong
If only some hostnames on the server are proxied, this configuration is still safe — substitution triggers only for connections from Cloudflare ranges, and direct traffic keeps its genuine connection IP. Where it is wrong: origins reached through another intermediary (a load balancer in front of the origin means the trusted-proxy list must name the balancer, and the balancer must forward the header faithfully), and DNS-only (grey-cloud) hostnames, where no Cloudflare headers exist at all — the proxied vs DNS-only explainer covers that split.
Common mistakes
- Trusting
X-Forwarded-Forinstead ofCF-Connecting-IP. XFF is a chain the client can seed. If a device insists on XFF, make sure the trust configuration takes the last Cloudflare-appended hop, not the first value. - Trusting the header from any source.
real_ip_header CF-Connecting-IPwithoutset_real_ip_fromscoping (or with0.0.0.0/0) turns your logs into fiction the first time someone probes the origin directly. - Forgetting the IPv6 ranges. Half the substitution silently fails for IPv6-connected edges.
- Leaving fail2ban watching the raw connection IP. It will ban a Cloudflare edge and take out every visitor routed through it.
Prevention
Treat the Cloudflare IP list as config-managed data with a refresh schedule, keep the direct-header spoof test from the verification section in your post-change checklist, and log CF-RAY alongside the restored IP so edge and origin stay joinable. If “is it Cloudflare or is it us” keeps eating your evenings, that triage is literally the job of 24/7 Monitoring & Response — book a monitoring and response review.