When a cPanel server sits behind Cloudflare, Nginx and Apache log a Cloudflare address for every visitor, because each request arrives over a connection that a Cloudflare edge server opened. Restoring the visitor’s real IP takes two changes, one per layer. Nginx needs its realip module (ngx_http_realip_module), with set_real_ip_from limited to Cloudflare’s published IP ranges and real_ip_header CF-Connecting-IP. Apache behind it needs mod_remoteip with RemoteIPHeader CF-Connecting-IP plus RemoteIPInternalProxy 127.0.0.1, because by the time a request reaches Apache, the connecting peer is the local Nginx proxy, not Cloudflare. Fix one layer and the other keeps logging the wrong address. The list of trusted proxies is the security half of the job. Trust the header from addresses you have not restricted, and anyone who can reach the origin directly can claim to be any IP on the internet.
What you’ll accomplish
- Nginx and Apache both logging the visitor’s address instead of
172.68.x.xor127.0.0.1. - A trusted-proxy list built from Cloudflare’s published ranges, IPv6 included, that you can regenerate rather than retype.
- ModSecurity, CSF/LFD, PHP’s
REMOTE_ADDRand WordPress login limiters all keying on the same corrected address, with no per-application “Cloudflare mode” toggles. - A spoofing test that proves the header is only honoured from Cloudflare, run against the origin directly.
- A refresh job so the range list does not quietly go stale.
Prerequisites
Root on a cPanel/WHM server, Cloudflare proxying the domain (orange cloud), and the ability to restart both services. This guide assumes cPanel’s own ea-nginx reverse proxy in front of EasyApache 4; the same directives apply to an Engintron-style install, only the file locations differ, and both are noted below.
Know which layers you actually have before you change anything:
rpm -q ea-nginx; systemctl is-active nginx
apachectl -M 2>/dev/null | grep remoteip
On a cPanel box with the Nginx front end you get a package version and active. On a plain Apache server you get package ea-nginx is not installed and inactive — in that case you only need the Apache half, covered in more depth in restoring real visitor IPs on cPanel Apache with mod_remoteip. An empty second line means mod_remoteip is not loaded yet.
Domain logs live at /etc/apache2/logs/domlogs/ on current cPanel versions, with /usr/local/apache/domlogs kept as a symlink on older ones. Substitute example.com throughout.
How it works
Cloudflare terminates the visitor’s connection at the edge and opens a new one to your origin. Nothing about that second connection carries the visitor’s address at the network layer — it rides inside the request, in headers Cloudflare adds: CF-Connecting-IP, holding exactly one address, and X-Forwarded-For, holding a comma-separated chain that the client may have started populating before Cloudflare ever saw it. Both Nginx and Apache have a module whose only job is to replace their notion of the client address with a value read out of such a header, and to do it early enough that everything downstream inherits the corrected value.
The part that catches people on cPanel is that there are two hops on the box, not one.
Nginx sits on 80/443 and its peer genuinely is a Cloudflare edge, so set_real_ip_from takes Cloudflare’s ranges. Apache sits behind Nginx on the loopback interface, and its peer is 127.0.0.1 on every single request. Loading mod_remoteip with only Cloudflare’s ranges in RemoteIPTrustedProxy therefore does nothing at all on such a server: Apache never sees a connection from those ranges, so it never honours the header. That is the single most common reason a fix that “worked” leaves Apache logs full of loopback.
Why CF-Connecting-IP and not X-Forwarded-For: Cloudflare sets CF-Connecting-IP itself and overwrites whatever the client sent, so at the edge boundary it is authoritative and unambiguous. X-Forwarded-For is a list, it is appended to by every intermediary, and a client can seed it before the request leaves their machine. Both are safe once the trusted-proxy list is right, but only one of them is a single value you never have to parse.
The trusted-proxy list is not a detail. real_ip_header and RemoteIPHeader say “believe this header”, and set_real_ip_from and RemoteIPTrustedProxy/RemoteIPInternalProxy say “but only from these peers”. Drop the second half — or widen it to 0.0.0.0/0 — and any host that can open a socket to your origin can put whatever it likes in CF-Connecting-IP. Your logs become attacker-authored, your ModSecurity bans hit uninvolved third parties, and rate limiting stops existing. This is why the ranges must come from Cloudflare’s published list and stay current, and why locking the origin’s 80/443 to those ranges at the firewall is worth doing alongside.
Everything below the web server reads one field.
Step-by-step
-
Look at what the two layers record today. Compare them, because the answers usually differ.
tail -200 /var/log/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head tail -200 /etc/apache2/logs/domlogs/example.com | awk '{print $1}' | sort | uniq -c | sort -rn | headThe Nginx column is typically a handful of Cloudflare ranges (
172.68.,104.2,162.158.); the Apache column is very often a single line reading127.0.0.1for the whole sample. Two different symptoms, two different fixes. -
Pull Cloudflare’s current ranges instead of pasting a copy. Fetch both families to a working file.
curl -fsS https://www.cloudflare.com/ips-v4/ -o /root/cf-v4.txt curl -fsS https://www.cloudflare.com/ips-v6/ -o /root/cf-v6.txt head -1 /root/cf-v4.txt; wc -l /root/cf-v4.txt /root/cf-v6.txtThe first line should be a CIDR such as
173.245.48.0/20, and the counts land in the mid-teens for IPv4 and the high single digits for IPv6. Anything beginning with<means you saved an HTML error page — check the URL and retry. The authoritative page is cloudflare.com/ips. -
Install
mod_remoteipthrough EasyApache, not by hand. In WHM » Software » EasyApache 4 » Customize » Apache Modules, enablemod_remoteipand provision. The command-line equivalent isdnf install ea-apache24-mod_remoteip(yumon older systems).apachectl -M | grep remoteipExpected output is a single line:
remoteip_module (shared). Nothing means the package did not install or Apache has not restarted yet. -
Generate the Apache include. Build it from the files you just downloaded so the list cannot drift from Cloudflare’s.
{ echo 'RemoteIPHeader CF-Connecting-IP' echo 'RemoteIPInternalProxy 127.0.0.1' echo 'RemoteIPInternalProxy ::1' awk 'NF {print "RemoteIPTrustedProxy " $0}' /root/cf-v4.txt /root/cf-v6.txt } > /root/cf-remoteip.conf head -5 /root/cf-remoteip.confYou should see the header directive, the two loopback entries, then
RemoteIPTrustedProxy 173.245.48.0/20. The loopback lines are what make this work behind Nginx; the Cloudflare lines keep it correct if a request ever reaches Apache without passing through the proxy. -
Place it where an Apache rebuild will not erase it. Paste the file’s contents into WHM » Service Configuration » Apache Configuration » Include Editor » Pre Main Include » All Versions, which writes
/etc/apache2/conf.d/includes/pre_main_global.conf. If you prefer the shell, append rather than overwrite, because that file may already hold other directives.cat /root/cf-remoteip.conf >> /etc/apache2/conf.d/includes/pre_main_global.conf apachectl configtest && /scripts/restartsrv_httpdconfigtestmust printSyntax OKbefore the restart runs. Editinghttpd.confdirectly instead is the classic mistake: the next EasyApache rebuild regenerates it and your fix disappears without a trace. -
Configure the Nginx layer at HTTP level. Put the directives in their own file under
/etc/nginx/conf.d/, which cPanel’sea-nginxincludes globally and does not regenerate — unlike/etc/nginx/conf.d/users/, which it rewrites on every config run.{ awk 'NF {print "set_real_ip_from " $0 ";"}' /root/cf-v4.txt /root/cf-v6.txt echo 'real_ip_header CF-Connecting-IP;' echo 'real_ip_recursive off;' } > /etc/nginx/conf.d/cloudflare-realip.conf nginx -t && systemctl reload nginxExpected:
nginx: configuration file /etc/nginx/nginx.conf test is successful.real_ip_recursive offis the default and is stated explicitly because it is the correct setting for a single-value header; the directive reference is at nginx.org. On an Engintron install the equivalent home is/etc/nginx/custom_rules, which Engintron preserves across updates while it overwrites its ownconf.dfiles — the realip directives are valid in the server context that file is included from. -
Confirm the built configuration actually contains it. A file in the right directory that nothing includes is a silent failure.
/usr/local/cpanel/scripts/ea-nginx config --all nginx -T | grep -c set_real_ip_fromThe count should match the number of prefixes you wrote, or a multiple of it if the directives ended up inside per-server blocks. Zero means the file is not being included.
-
Point the firewall and the panel at the same list. Two things still need attention once the address is correct. LFD now bans real visitor addresses, but packets reach you from Cloudflare, so a local
iptablesDROP on the attacker achieves nothing — allowlist Cloudflare’s ranges in/etc/csf/csf.allowand use CSF’s Cloudflare integration (CF_ENABLE = "1"in/etc/csf/csf.conf, credentials in/etc/csf/csf.cloudflare) so blocks are pushed to the edge instead. ModSecurity and PHP need nothing:mod_remoteiprewrites the address before either runs, so audit-logclientfields and$_SERVER['REMOTE_ADDR']follow automatically, which also removes a large share of ModSecurity false positives behind Cloudflare. WordPress security plugins readREMOTE_ADDRtoo, so their own “I’m behind Cloudflare” options should be left off once the server is correct — two layers rewriting the same value is how you end up trusting the header twice. -
Schedule the refresh. Cloudflare’s ranges change rarely but they do change, and a stale list means real visitors stop being recognised. A monthly root cron entry that re-runs steps 2, 4 and 6 and reloads both services is enough:
17 4 1 * * /usr/local/bin/cf-realip-refresh.sh >/dev/null 2>&1Have the script write to a temporary file, run
apachectl configtestandnginx -t, and only then move the files into place and reload. A refresh that installs a broken config at 04:17 is worse than a stale one.
Validate it works
Drop a one-line probe in the document root:
printf '<?php echo $_SERVER["REMOTE_ADDR"], "\n";' > /home/USER/public_html/whoami.php
Then run three checks. First, the normal path through Cloudflare:
curl -s https://example.com/whoami.php
This should print the public IP of the machine you are testing from — not a Cloudflare address, not 127.0.0.1. Second, the log lines for that exact request:
tail -1 /var/log/nginx/access.log
tail -1 /etc/apache2/logs/domlogs/example.com
Both first fields should now be your address. Third, and most important, the spoofing test — go straight to the origin, bypassing Cloudflare, with a forged header. Replace 203.0.113.10 with your origin’s real IP:
curl -s --resolve example.com:443:203.0.113.10 -k -H 'CF-Connecting-IP: 198.51.100.7' https://example.com/whoami.php
The correct result is your own address, or a connection refused if the firewall already restricts the origin. If it prints 198.51.100.7, your trusted-proxy list is too wide and you have built an IP spoofing hole — go back to steps 4 and 6 before doing anything else. Remove whoami.php when you are finished.
Troubleshooting
Nginx logs are correct but Apache still shows 127.0.0.1. The RemoteIPInternalProxy 127.0.0.1 line is missing or landed in a file Apache does not read. Confirm with apachectl -t -D DUMP_INCLUDES | grep pre_main and re-check that apachectl -M lists remoteip_module.
Everything reverted after a cPanel update. Apache directives were in httpd.conf instead of an Include Editor file, or the Nginx snippet was under /etc/nginx/conf.d/users/, which ea-nginx config regenerates. Move them to pre_main_global.conf and /etc/nginx/conf.d/ respectively and re-verify with nginx -T.
Some visitors still show as Cloudflare. Almost always the IPv6 prefixes were skipped. Check with grep -c ':' /root/cf-v6.txt and confirm the same count appears in both config files.
The spoof test returns the forged address. Either a trusted-proxy entry is far too broad, or real_ip_header was set without any set_real_ip_from line reaching the running config. Fix the scope first, then close the origin: restricting inbound 80/443 to Cloudflare’s ranges in CSF removes the attack path entirely.
AWStats and bandwidth figures look wrong for the changeover day. They are computed from the same domain logs, so the day of the change contains both shapes. The numbers are right from the next full cycle onward; there is nothing to reprocess.
When this is the wrong approach
If the origin is reachable directly and you are not willing to restrict it at the firewall, header trust is a liability rather than a fix — get the network boundary right first. If a load balancer or a second proxy sits in the chain, the trusted list has to describe the hop that actually connects, not the one you wish were connecting, and a blanket Cloudflare list will simply be ignored. If the server runs LiteSpeed instead of Apache there is no mod_remoteip: set Use Client IP in Header to Trusted IP Only in the LiteSpeed WebAdmin console and add Cloudflare’s ranges to the trusted list — never set it to Yes, which trusts the header from anywhere. And none of this touches Exim or cpsrvd: mail and panel traffic never passes through Cloudflare, so their logs were always showing the true peer.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.