Bobay Stack

Security boundaries / 2026-09-19

Real IP Behind Cloudflare: All Three Configs Are Required

With the CDN on, logs show only Cloudflare IPs, so rate limits and bans stop working. Restoring real IPs takes three changes.

4 MIN READ BY BOBAY.TOP Updated 2026-09-25 ↖ All notes
Real IP Behind Cloudflare: All Three Configs Are Required

Once a site is behind Cloudflare, "rate limit by IP" and "ban by IP" both stop working — because the server only ever sees the IPs of CF edge nodes. This isn't a misconfiguration; the restoration step was simply never done.

Step one: Nginx trusts Cloudflare's ranges

set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 104.16.0.0/13;
# … remaining CF ranges
real_ip_header CF-Connecting-IP;
real_ip_recursive on;

If you write real_ip_header without set_real_ip_from, Nginx trusts no source at all — the same as not configuring it.

Step two: the log format has to carry the real IP

log_format main '$realip_remote_addr "$request" $status';

With $remote_addr the logs still show Cloudflare's address. $realip_remote_addr is the restored value.

Step three: the application layer has to agree

PHP reads REMOTE_ADDR by default, and that's Cloudflare's IP. It needs to read HTTP_CF_CONNECTING_IP, and trust it only when the request actually comes from Cloudflare — otherwise anyone can forge that header.

What happens if you get the order wrong

If you change the application before you change Nginx, the application receives a fake IP filled in by the client itself. This combination is the most dangerous: it looks like "the real IP is working", but the value is whatever the attacker wrote, and your ban list is untrustworthy from then on.

The correct order is: Nginx trusts Cloudflare's ranges first → the header is restored → the application reads REMOTE_ADDR (already rewritten by realip).

Verify

curl -H "CF-Connecting-IP: 1.2.3.4" https://your.site/ip

If hitting the origin directly returns 1.2.3.4, you are trusting a forged header — at that point you must confirm the origin only accepts connections from Cloudflare, or simply restrict the origin ports to Cloudflare's ranges.

What happens if you don't change the logging

Every request's remote_addr becomes a Cloudflare egress IP (usually in ranges like 162.158.x.x and 172.68.x.x). Three things follow:

  1. The access log is all one IP, so you can't tell who is scanning the site
  2. fail2ban bans an entire Cloudflare range, and the whole site immediately returns 522
  3. When rate limiting counts per IP, every visitor shares one bucket, and a single popular page is enough to 429 the whole site

Item 2 is the one most likely to blow up: fail2ban decides "the same IP is hammering 404s", bans one Cloudflare node, and every visitor routed through that node gets an error from then on.

The correct order

Declare the trusted sources in nginx first, then change the log format, and only then touch fail2ban.

# only trust Cloudflare's official ranges
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
# …(full list at cloudflare.com/ips)
real_ip_header CF-Connecting-IP;
real_ip_recursive on;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" cf=$http_cf_ray';
access_log /var/log/nginx/access.log main;

The cf=$http_cf_ray column is worth a lot: when something goes wrong you can take the Ray ID to the Cloudflare dashboard and match the exact same request, which is far faster than working from timestamps alone.

How to verify

You have to verify after the change, otherwise you only think you changed it:

curl -s -o /dev/null -H "CF-Connecting-IP: 1.2.3.4" http://127.0.0.1/
tail -1 /var/log/nginx/access.log   # remote_addr should be 1.2.3.4

If the log still shows 127.0.0.1, it's almost certainly a typo in real_ip_header, or that IP isn't in the set_real_ip_from list.

One counterintuitive point

CF-Connecting-IP can be forged: anyone who connects straight to your origin without going through Cloudflare can send that header themselves. So the origin must only allow access from Cloudflare's ranges (firewall allowlist), otherwise this whole setup just makes the logs look more convincing.

A note: IPv6 also needs its own set_real_ip_from entries. Cloudflare's IPv6 and IPv4 ranges are two separate sets, and if you only configure IPv4 you'll find that v6 visitors still show up as node addresses.

CloudflareNginxReal IPWAF

References

Cloudflare Real IP ↗ Nginx realip module ↗