Short answer: To get the nginx real IP behind Cloudflare, enable the realip module with set_real_ip_from for every Cloudflare IPv4 and IPv6 range and real_ip_header CF-Connecting-IP, then test and reload. Never trust 0.0.0.0/0, because anyone could spoof their address. Keep the range list current with a weekly script from cloudflare.com/ips.
Key takeaways
- Behind Cloudflare, nginx sees edge IPs until you configure the realip module.
- Trust only Cloudflare’s published ranges with set_real_ip_from, and read the CF-Connecting-IP header.
- set_real_ip_from 0.0.0.0/0 lets anyone fake their IP and silently breaks fail2ban and rate limits.
- Test with a spoofed header sent directly to the origin; it must be ignored.
- Automate the range list with a script that validates, tests and rolls back.

The first time you put a site behind Cloudflare, your nginx access log changes overnight: every visitor suddenly comes from addresses like 172.70.x.x or 162.158.x.x. Analytics in log files becomes useless, fail2ban starts banning Cloudflare edges, and rate limits throttle entire data centres. The fix is small, but there is a common shortcut that opens a serious security hole.
I recently audited my own production VPS and found exactly that shortcut in the main nginx.conf, inherited from a control panel template: set_real_ip_from 0.0.0.0/0. This guide shows the safe configuration I replaced it with, how to test it, and how to keep it current automatically.
Why does nginx show Cloudflare IPs instead of real visitors?
With the orange-cloud proxy enabled, visitors connect to Cloudflare, and Cloudflare opens a new connection to your origin. Nginx logs the address of the machine that connected, which is a Cloudflare edge. Cloudflare passes the original visitor address in the CF-Connecting-IP and X-Forwarded-For headers, and nginx must be told to trust them.
Origin server: your own server (for example an Ubuntu VPS running nginx) that Cloudflare fetches content from.
realip module: the nginx module ngx_http_realip_module, which replaces the client address with a value from a request header, but only when the request comes from an address you trust.
Step 1: check the module and your current logs
Most distribution packages of nginx include the realip module. Confirm it, and look at the current access log so you can compare before and after.
# Is the realip module compiled in? (look for --with-http_realip_module)
nginx -V 2>&1 | tr ' ' 'n' | grep realip
# What does your access log show right now?
sudo tail -n 5 /var/log/nginx/access.log
If the grep prints --with-http_realip_module, you are ready. If it prints nothing, you are probably on a custom build and need a package that includes the module.
Step 2: set the nginx real IP by trusting Cloudflare ranges
Create a separate config file with one set_real_ip_from line per Cloudflare range, and set real_ip_header to CF-Connecting-IP. Files in /etc/nginx/conf.d/ are included in the http block on Debian and Ubuntu, so the setting applies to every site on the server.
# /etc/nginx/conf.d/cloudflare-realip.conf
# Trust these proxies to tell us the real client address
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;
set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2a06:98c0::/29;
set_real_ip_from 2c0f:f248::/32;
# Read the client address from Cloudflare's header
real_ip_header CF-Connecting-IP;
The list above was current when I wrote this, but always take the live list from cloudflare.com/ips; step 6 automates it. Then test and reload:
sudo nginx -t && sudo systemctl reload nginx
CF-Connecting-IP or X-Forwarded-For: which header should you use?
Use CF-Connecting-IP when Cloudflare is the only proxy in front of nginx. Cloudflare sets it to exactly one address, the connecting client. X-Forwarded-For may contain several comma-separated addresses, including values sent by the client, so it needs real_ip_recursive and careful trust settings to be safe.
| Header | Contains | Use when |
|---|---|---|
| CF-Connecting-IP | One client IP set by Cloudflare | Cloudflare is the only proxy (most sites) |
| X-Forwarded-For | A chain: client, proxy, proxy | Several proxies, with real_ip_recursive on |
| True-Client-IP | One client IP | Cloudflare Enterprise feature, if enabled |
Why set_real_ip_from 0.0.0.0/0 is dangerous
Trusting 0.0.0.0/0 tells nginx to accept the client-IP header from anyone on the internet. An attacker who finds your origin IP can send requests directly with a fake header and appear to come from any address they choose, which defeats bans, allow-lists, rate limits and audit logs.
# DANGEROUS: trusts every client on the internet
set_real_ip_from 0.0.0.0/0;
real_ip_header X-Forwarded-For;
From my own work: This exact block was in my server’s main nginx.conf, added by a hosting panel template years ago. Everything looked fine because Cloudflare traffic showed the right IPs. I only noticed while hardening the server: any request sent straight to the origin could choose its own IP. I commented it out, kept a dated backup of nginx.conf, and moved to the explicit Cloudflare list.
Search for it on your own server; panels and copied tutorials add it more often than you would think:
sudo grep -Rn "set_real_ip_from|real_ip_header" /etc/nginx/
If you find a broad rule, comment it out, keep a backup with a date in the filename, test with nginx -t, and reload.
Step 3: verify that real IPs appear in the logs
Open your site from your phone on mobile data and from your computer, then check the access log. You should see your real public addresses, not Cloudflare ranges. You can confirm your public address by searching “what is my IP” in a browser.
sudo tail -f /var/log/nginx/access.log
Step 4: test that spoofed headers are ignored
A correct setup restores the IP only for requests that really come from Cloudflare. Send a request directly to the origin with a fake CF-Connecting-IP header from a machine outside Cloudflare. The log must show that machine’s real address, not the fake one.
# From any machine that is NOT Cloudflare, try to spoof an address
curl -s -o /dev/null -w "%{http_code}n" -H "CF-Connecting-IP: 1.2.3.4"
--resolve example.com:443:YOUR_ORIGIN_IP https://example.com/
# Then check the log: the line must show YOUR real IP, not 1.2.3.4
sudo tail -n 1 /var/log/nginx/access.log
If the log shows 1.2.3.4, something still trusts too broadly. Look for another set_real_ip_from line, perhaps in a site file or an included snippet.
Step 5: improve your log format
The realip module also provides $realip_remote_addr, the original connecting address before replacement. Logging it next to $remote_addr shows which Cloudflare edge forwarded each request, and the CF-Ray header lets you match a log line with Cloudflare’s own logs when you debug.
# Optional: log both the restored client IP and the proxy that sent it
log_format cf '$remote_addr via $realip_remote_addr [$time_local] '
'"$request" $status $body_bytes_sent "$http_user_agent" '
'cf_ray=$http_cf_ray country=$http_cf_ipcountry';
access_log /var/log/nginx/access.log cf;
Remember to update your logrotate configuration if you change log file names.
Step 6: keep the Cloudflare list updated automatically
Cloudflare publishes its ranges as plain text at cloudflare.com/ips-v4 and ips-v6. The script below rebuilds the config from those files, refuses to install an empty download, tests nginx, and restores the previous file if the test fails. Run it weekly from cron.
#!/usr/bin/env bash
# /usr/local/sbin/cf-realip-update - rebuild the Cloudflare realip list safely
set -euo pipefail
OUT=/etc/nginx/conf.d/cloudflare-realip.conf
TMP=$(mktemp)
{
echo "# Generated $(date -u +%F) from https://www.cloudflare.com/ips/"
for v in 4 6; do
curl -fsS "https://www.cloudflare.com/ips-v$v" | while read -r net; do
[ -n "$net" ] && echo "set_real_ip_from $net;"
done
done
echo "real_ip_header CF-Connecting-IP;"
} > "$TMP"
# Refuse to install an empty or broken list
grep -q "set_real_ip_from" "$TMP" || { echo "download failed"; exit 1; }
cp "$OUT" "$OUT.bak" 2>/dev/null || true
install -m 644 "$TMP" "$OUT"
if nginx -t 2>/dev/null; then
systemctl reload nginx
else
echo "nginx -t failed, restoring backup"; cp "$OUT.bak" "$OUT"; exit 1
fi
# Run weekly (root crontab: sudo crontab -e)
17 4 * * 1 /usr/local/sbin/cf-realip-update >/var/log/cf-realip-update.log 2>&1
Make it executable with sudo chmod 700 /usr/local/sbin/cf-realip-update, run it once by hand, and check the generated file. Never let an automated job reload nginx without a passing nginx -t first.
How do you check the real IP in PHP and WordPress?
Once nginx restores the address, PHP-FPM receives it in REMOTE_ADDR through the standard fastcgi parameters, so WordPress and plugins see the visitor without any extra code. Avoid application code that reads HTTP_CF_CONNECTING_IP or HTTP_X_FORWARDED_FOR directly, because those headers can be forged on requests that bypass Cloudflare.
- Quick test: create a temporary PHP file that prints
$_SERVER["REMOTE_ADDR"], open it, compare with your public IP, then delete the file. - WordPress: comment IPs, WooCommerce order IPs and login limiter logs should now show real addresses.
- Plugins with their own IP settings: security plugins often ask how to detect the visitor IP; choose REMOTE_ADDR once nginx is configured, rather than trusting a header.
This is the main reason to fix the problem in nginx rather than in each application: one trusted place restores the address, and everything behind it simply uses REMOTE_ADDR.
What about IPv6 visitors and Pseudo IPv4?
Cloudflare serves IPv6 visitors natively, so CF-Connecting-IP may contain an IPv6 address. Make sure log parsers, fail2ban filters and databases accept IPv6. Cloudflare also offers a Pseudo IPv4 setting that adds a header with a mapped IPv4 address for legacy software; most modern stacks do not need it, and you should keep reading the real address from CF-Connecting-IP.
How does this affect fail2ban, rate limiting and allow-lists?
After restoring real IPs, $remote_addr is the visitor, so nginx limit_req zones, allow and deny rules, and fail2ban jails reading nginx logs all act on individual visitors. Before the fix, one ban could block a Cloudflare edge serving thousands of legitimate users.
- Rate limiting: zones keyed on
$binary_remote_addrnow count per visitor; see nginx rate limiting for WordPress. - fail2ban: nginx bans with iptables do not stop traffic arriving through Cloudflare, because packets come from Cloudflare IPs. Use fail2ban with a Cloudflare action, or block at Cloudflare; see fail2ban for WordPress and nginx.
- Allow-lists: restricting wp-admin to your office IP now works as expected, but a Cloudflare WAF rule is often simpler; see Cloudflare WAF rules for WordPress.
- Applications: PHP sees the real address in REMOTE_ADDR, so WordPress comments, WooCommerce fraud checks and login limiters work correctly.
Optional: accept web traffic only from Cloudflare
For stronger protection, allow ports 80 and 443 only from Cloudflare ranges at the firewall, so nobody can reach the origin directly. This also stops attackers bypassing Cloudflare’s WAF. Keep SSH reachable from your own address before you apply it, and test carefully.
# Optional hardening: only accept HTTP(S) from Cloudflare at the firewall
for net in $(curl -fsS https://www.cloudflare.com/ips-v4); do
sudo ufw allow proto tcp from "$net" to any port 443
done
# (repeat for ips-v6, keep SSH open from your own IP, then remove any "allow 443" rule)
My UFW firewall guide explains rule order and how to avoid locking yourself out. An alternative is Authenticated Origin Pulls, where nginx requires Cloudflare’s client certificate.
What changes with Cloudflare Tunnel?
With Cloudflare Tunnel, the cloudflared daemon on your server connects outward to Cloudflare and forwards requests to nginx over localhost. The connecting address is 127.0.0.1, so you trust localhost instead of public ranges, and you can close ports 80 and 443 entirely.
# With Cloudflare Tunnel, cloudflared connects to nginx from localhost
set_real_ip_from 127.0.0.1;
set_real_ip_from ::1;
real_ip_header CF-Connecting-IP;
See Cloudflare Tunnel for self-hosted apps for the full setup.
What about Docker and nginx proxy manager?
If nginx runs in a container, the same directives apply inside the container’s config. In Nginx Proxy Manager, add them in the advanced settings or a custom include. If another reverse proxy sits between Cloudflare and nginx, trust that proxy’s address too and consider X-Forwarded-For with real_ip_recursive on. My guide to nginx as a reverse proxy for Docker apps covers networking details.
Troubleshooting: still seeing Cloudflare IPs?
- Config not loaded: check that /etc/nginx/conf.d/*.conf is included in the http block of nginx.conf.
- Wrong header name: it is CF-Connecting-IP, and nginx is case-insensitive for header names, but typos matter.
- Missing IPv6 ranges: Cloudflare connects over IPv6 if your origin has an AAAA record.
- Another proxy in between: a load balancer or container proxy is the real connecting address; trust it as well.
- Old log lines: only new requests after the reload show the change.
Checklist
- realip module present (
nginx -V). - No
0.0.0.0/0or other broad trust anywhere in /etc/nginx. - All Cloudflare IPv4 and IPv6 ranges trusted.
real_ip_header CF-Connecting-IPset.nginx -tpasses and nginx reloaded.- Logs show real visitor IPs.
- Spoofed header to the origin is ignored.
- Weekly update script with test and rollback.
- fail2ban, rate limits and allow-lists reviewed.
Want this done on your own server or site?
I am Saizul Amin, a web developer and technical SEO consultant. I set up and harden VPS servers, migrate WordPress sites, fix indexing problems and build automations. See my portfolio or get in touch.
FAQ
Why does nginx log Cloudflare IPs instead of visitor IPs?
When a site is proxied through Cloudflare, every request reaches your server from a Cloudflare edge address. Nginx records the address of the machine that connected, so you see Cloudflare IPs until you configure the realip module to trust Cloudflare and read the CF-Connecting-IP header.
Should I use CF-Connecting-IP or X-Forwarded-For?
Use CF-Connecting-IP when Cloudflare is the only proxy in front of nginx. It contains a single client address set by Cloudflare. X-Forwarded-For can contain a chain of addresses and is easier to get wrong; it is useful when there are several proxies and you also enable real_ip_recursive.
Is set_real_ip_from 0.0.0.0/0 safe?
No. It tells nginx to trust the header from any client, so anyone who reaches your origin directly can fake their IP address. That breaks rate limiting, fail2ban, access rules and logs. Trust only Cloudflare’s published ranges, or 127.0.0.1 when using Cloudflare Tunnel.
How often do Cloudflare IP ranges change?
Rarely, but they do change, and Cloudflare publishes the current list at cloudflare.com/ips with plain-text files for IPv4 and IPv6. Rebuilding your nginx list from those files with a weekly cron job keeps it current without manual work.
Does this affect fail2ban and rate limiting?
Yes, positively. After the change, $remote_addr holds the real visitor IP, so fail2ban bans and nginx limit_req zones apply to individual visitors instead of Cloudflare edges. Without it, a ban could block a whole Cloudflare data centre and many legitimate visitors.
Do I need this with Cloudflare Tunnel?
Yes, but the trusted address changes. With Cloudflare Tunnel, cloudflared connects to nginx locally, so trust 127.0.0.1 and ::1 and keep real_ip_header CF-Connecting-IP. Your origin does not need public ports open at all in that setup.
Sources
- nginx: ngx_http_realip_module
- Cloudflare: IP ranges
- Cloudflare: Restoring original visitor IPs
- Cloudflare: HTTP request headers (CF-Connecting-IP)
- nginx: ngx_http_log_module
- Cloudflare: Cloudflare Tunnel

Saizul Amin is a web developer, AI automation engineer and technical SEO consultant from Dhaka, Bangladesh (B.Sc. in CSE, BUBT). Since 2017 he has worked in digital marketing, WordPress, IT infrastructure and server administration, including the bilingual SHR Bangladesh website for DGHS, WordPress sites for UK and US clients, and the ShobFree tools platform. He runs this blog on his own Ubuntu VPS with nginx, Redis and CloudPanel, and tests the guides here on it. Portfolio: saizul.com

