Restore the real visitor IP behind Cloudflare
Why WordPress logs Cloudflare's IP instead of the visitor's, and how to restore the real address in Nginx, Apache or PHP without trusting spoofed headers.
At a glance
Behind Cloudflare, every request reaches your server from a Cloudflare address, so logs, login limiters and comment tools see the same few IPs for everyone. The fix is to tell your web server which addresses are Cloudflare and take the visitor's IP from the CF-Connecting-IP header only for those addresses. This guide covers Nginx, Apache, PHP and testing.
Updated November 29, 2026
- For site owners and administrators running WordPress behind Cloudflare
- Symptom logs and plugins show Cloudflare IPs, not visitors
- Fix server-level real IP with Cloudflare's published ranges
- Header CF-Connecting-IP, trusted only from Cloudflare addresses
- Also lock the origin to Cloudflare so the header cannot be forged
- Before editing back up, keep a second SSH session open
- Help security service and support
On this page
- What goes wrong without the fix?
- Why not just read the header in PHP?
- How do I fix it in Nginx?
- How do I fix it in Apache?
- What about shared hosting where I cannot edit the server?
- How do I stop direct access to the origin?
- How do I verify it worked?
- How often should I update the Cloudflare IP list?
- Related steps
- FAQ
When a site sits behind Cloudflare, every request reaches your server from a Cloudflare address. Logs, login limiters and comment tools then see the same few IPs for everyone. The fix is to tell your web server which addresses are Cloudflare and to take the visitor's IP from the `CF-Connecting-IP` header only for those addresses.
Take a backup and keep a second SSH session open while you edit server configuration, so a typo cannot lock you out.
What goes wrong without the fix?
| Area | What goes wrong |
|---|---|
| Access logs | Show Cloudflare addresses; investigating abuse is almost impossible |
| Login-limit plugins | May ban a Cloudflare address, blocking every visitor through it |
| Rate limits and geolocation | Use the wrong address |
| WooCommerce fraud checks and gateways | Record a useless IP |
You can confirm the problem by temporarily printing $_SERVER['REMOTE_ADDR'] in a test script, or by checking your security plugin's activity log. If every entry is a Cloudflare address, you need this.
Why not just read the header in PHP?
Many snippets do $_SERVER['HTTP_CF_CONNECTING_IP']. That works only when you can be sure the request came through Cloudflare. If your origin server answers direct requests as well, an attacker sends their own CF-Connecting-IP: 1.2.3.4 and your code believes it. That defeats IP bans and any allowlist.
The safer pattern is to let the web server do the replacement, because it knows the real network peer. It rewrites the client address only when the request comes from a trusted proxy range.
How do I fix it in Nginx?
Nginx's realip module is included in most distributions. Cloudflare publishes its current ranges at https://www.cloudflare.com/ips-v4 and https://www.cloudflare.com/ips-v6. Do not paste a list from an old tutorial, since ranges change. Generate the file instead:
for ip in $(curl -fsS https://www.cloudflare.com/ips-v4; echo; curl -fsS https://www.cloudflare.com/ips-v6); do
echo "set_real_ip_from $ip;"
done | sudo tee /etc/nginx/cloudflare-realip.conf
echo "real_ip_header CF-Connecting-IP;" | sudo tee -a /etc/nginx/cloudflare-realip.confThen include the file in the http block, or inside each server block that needs it:
include /etc/nginx/cloudflare-realip.conf;Test and reload:
sudo nginx -t && sudo systemctl reload nginxUse CF-Connecting-IP rather than X-Forwarded-For. The latter can contain a list and can be appended to by anything in the chain. If your hosting panel (RunCloud, GridPane, Plesk and others) manages Nginx, add the include through its custom configuration screen so a panel update does not overwrite it.
How do I fix it in Apache?
Enable mod_remoteip and list the trusted proxies:
sudo a2enmod remoteipIn a config file such as /etc/apache2/conf-available/cloudflare-remoteip.conf:
RemoteIPHeader CF-Connecting-IP
RemoteIPTrustedProxy 173.245.48.0/20Add one RemoteIPTrustedProxy line for each range from Cloudflare's lists (the example above is only one entry from the published list), then enable the file with a2enconf and reload Apache. Apache also offers RemoteIPTrustedProxyList to read the ranges from a file, which is easier to regenerate with the same loop as above. Also change the log format from %h to %a if you want logs to show the replaced address.
What about shared hosting where I cannot edit the server?
Ask your host first. Many already apply this for Cloudflare customers. If you must handle it in WordPress, do it with a check on the connecting address, in a small custom plugin or in wp-config.php. The code below only trusts the header when the peer is inside a list of Cloudflare ranges you maintain:
function wp24_real_ip( array $cloudflare_ranges ): string {
$peer = $_SERVER['REMOTE_ADDR'] ?? '';
$header = $_SERVER['HTTP_CF_CONNECTING_IP'] ?? '';
if ( filter_var( $header, FILTER_VALIDATE_IP ) && wp24_ip_in_ranges( $peer, $cloudflare_ranges ) ) {
return $header;
}
return $peer;
}You would also need a helper that checks an IP against CIDR ranges. That is real code to maintain and test, so use the server-level fix wherever it is available. Some security plugins ship a setting such as "trusted proxy" or "use Cloudflare header" that does the safe check for you.
How do I stop direct access to the origin?
The restored IP is only as trustworthy as the path to your server. Close the gap:
| Measure | How |
|---|---|
| Firewall | Allow ports 80 and 443 only from Cloudflare's ranges, if your host allows firewall rules |
| Authenticated Origin Pulls | The server rejects connections without Cloudflare's client certificate |
| DNS hygiene | Never expose the origin IP in records for other hostnames, such as mail or ftp, which are not proxied |
How do I verify it worked?
- Load
a page, then check the newest line in your access log. It should show your own public IP, which you can find by searching "what is my IP".
- Check
your security plugin's login log. A failed login from your own browser should now list your IP.
- Test
from mobile data to confirm that a different IP appears.
If something stays wrong after a reload, check that the include is loaded in the right block (use nginx -T | grep real_ip) and that another proxy, such as a load balancer in front of Nginx, is not adding hops that also need to be trusted.
How often should I update the Cloudflare IP list?
Cloudflare changes its ranges rarely, but it does change them, so schedule a monthly job that regenerates the file, runs nginx -t and reloads only if the test passes. Subscribe to Cloudflare's announcements for changes. If your host manages this for you, ask how often they update it, because a stale list means new Cloudflare addresses are not trusted and visitors show up with Cloudflare IPs again.
Related steps
Once visitor IPs are right, rules based on them work as intended. See five Cloudflare rules to secure WordPress and blocking PetalBot with Cloudflare. If server configuration is not something you want to touch, our WordPress support team or security service can do it and test it for you.
Five rules that secure a site.
Read more02Security serviceWe can do this and test it.
Read more03WordPress supportHelp with server changes.
Read moreA person will make the change and test it for you.
Which header holds the real visitor IP in Cloudflare?
CF-Connecting-IP. Cloudflare sets it on every request it proxies. You should trust it only when the request truly came from a Cloudflare address.
Link to this answerWhy is it unsafe to read the header directly in PHP?
Anyone who reaches your server without going through Cloudflare can send a fake CF-Connecting-IP or X-Forwarded-For header and impersonate any address, bypassing IP bans and rate limits.
Link to this answerDo I need this if I use a security plugin?
Yes. Login limiting, firewall and logging plugins act on the IP the server reports. Behind Cloudflare without this fix they may ban Cloudflare itself or treat all visitors as one.
Link to this answerWhich header holds the real visitor IP in Cloudflare?
CF-Connecting-IP. Cloudflare sets it on every request it proxies, and you should trust it only when the request truly came from a Cloudflare address.
Link to this answerWhy is reading the header directly in PHP unsafe?
If your origin answers direct requests too, an attacker can send their own CF-Connecting-IP or X-Forwarded-For header and impersonate any address, defeating IP bans, rate limits and allowlists.
Link to this answerWhy use CF-Connecting-IP instead of X-Forwarded-For?
X-Forwarded-For can contain a list and can be appended to by anything in the chain. CF-Connecting-IP is set by Cloudflare as a single value.
Link to this answerWhat happens if I do not fix this?
Logs show Cloudflare addresses, security plugins may ban a Cloudflare address and block every visitor passing through it, rate limits and geolocation use the wrong address and fraud checks record a useless IP.
Link to this answerHow do I get Cloudflare's current IP ranges?
Cloudflare publishes them at its ips-v4 and ips-v6 pages. Generate the configuration from those lists instead of pasting old ranges from tutorials, since ranges change.
Link to this answerHow often should I update the IP list?
Cloudflare changes ranges rarely but does change them, so schedule a monthly job that regenerates the file, runs nginx -t and reloads only if the test passes. A stale list means new Cloudflare addresses are not trusted.
Link to this answerHow do I stop direct access to the origin?
Allow ports 80 and 443 only from Cloudflare's ranges in the firewall, or enable Authenticated Origin Pulls so the server rejects connections without Cloudflare's client certificate. Never expose the origin IP in unproxied DNS records.
Link to this answerHow do I verify it worked?
Load a page and check the newest access log line shows your own public IP, check your security plugin's login log lists your IP after a failed login and test from mobile data to see a different IP.
Link to this answerIt still shows Cloudflare IPs after a reload. What should I check?
Check the include is loaded in the right block with nginx -T and grep real_ip, and check whether another proxy, such as a load balancer in front of Nginx, adds hops that also need to be trusted.
Link to this answerKeep reading
Want an engineer on it?
Tell us what is wrong in plain words. A person reads it, today.