# Restore the real visitor IP behind Cloudflare

URL: https://wp24x7.com/how-to-restore-the-real-ip-address-of-the-visitor-if-your-web-application-is-behind-cloudflare/
Updated: 2026-11-29
Type: blog post

> 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.

## Key facts

- 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

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.

> **Quick answer**
Trust `CF-Connecting-IP` only when the connecting peer is a Cloudflare range, set at the web server, then lock the origin to Cloudflare.

## 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 |

_The visitor, Cloudflare, then your server: only the middle hop is visible by default_

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.conf
```

Then 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 nginx
```

Use `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 remoteip
```

In a config file such as `/etc/apache2/conf-available/cloudflare-remoteip.conf`:

```
RemoteIPHeader CF-Connecting-IP
RemoteIPTrustedProxy 173.245.48.0/20
```

Add 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:

```php
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?

1. **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".
2. **Check** your security plugin's login log. A failed login from your own browser should now list your IP.
3. **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](/5-simple-rules-in-cloudflare-to-secure-your-wordpress-website) and [blocking PetalBot with Cloudflare](/how-to-block-petalbot-using-cloudflare). If server configuration is not something you want to touch, our [WordPress support team](/wordpress-support) or [security service](/wordpress-security-service) can do it and test it for you.

- **Cloudflare rules for WordPress** Five rules that secure a site. [Read more](/5-simple-rules-in-cloudflare-to-secure-your-wordpress-website)
- **Security service** We can do this and test it. [Read more](/wordpress-security-service)
- **WordPress support** Help with server changes. [Read more](/wordpress-support)

**Prefer someone else touches the server config?**
A person will make the change and test it for you.
[Post a project](/post-project)
[Call the help desk](tel:+18043754450)

## Frequently asked questions

### 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.

### Why 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.

### Do 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.

### Which 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.

### Why 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.

### Why 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.

### What 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.

### How 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.

### How 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.

### How 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.

### I am on shared hosting and cannot edit the server. What now?

Ask your host first, since many already apply this for Cloudflare customers. Otherwise use a security plugin's trusted proxy setting, or a small plugin that trusts the header only when the peer is in Cloudflare's ranges.

### How 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.

### It 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.
