Show real visitor IPs in Nginx behind Cloudflare or a proxy
Behind Cloudflare or a load balancer, Nginx sees the proxy's IP instead of your visitors'. Use the real_ip module to restore the real address for logs, rate limits and apps.
- Intermediate
- Linux VPS
- 5 min read
When your site sits behind Cloudflare, a load balancer or another reverse proxy, every request reaches Nginx from the proxy's address. Your logs fill with Cloudflare IPs, rate limits treat thousands of visitors as one, and IP blocks stop working. Nginx's real_ip module fixes this by reading the visitor's address from a header the proxy adds, but only when the request really came from a trusted proxy.
Before you start
- A RunRacks Linux VPS with Nginx serving your site.
- The site proxied through Cloudflare (orange cloud) or another proxy you control.
- SSH access with sudo.
Confirm the module is built in. It is included in the standard packages on Ubuntu, Debian, AlmaLinux and Rocky Linux:
nginx -V 2>&1 | grep -o with-http_realip_moduleHow it works
Three directives do the work:
- `set_real_ip_from` lists the proxy addresses you trust. Repeat it once per range.
- `real_ip_header` names the header holding the visitor's IP. Cloudflare sends
CF-Connecting-IP; most other proxies sendX-Forwarded-For. - `real_ip_recursive` matters when there are several proxies in a chain. With
on, Nginx walks back throughX-Forwarded-Forand skips every trusted address.
When a request arrives from a trusted range, Nginx replaces $remote_addr with the header value. Everything that uses $remote_addr then sees the real visitor: access logs, allow and deny, limit_req, and the REMOTE_ADDR passed to PHP.
Only trust ranges that belong to your proxy. If you trust everything, any visitor can send a fake CF-Connecting-IP header and appear to be any IP they like, including one on your allow list.
Build the Cloudflare include file
Cloudflare publishes its ranges at https://www.cloudflare.com/ips-v4 and https://www.cloudflare.com/ips-v6. They change rarely, but they do change. Rather than pasting a list that may go stale, generate the file from those URLs with a short script:
sudo nano /usr/local/bin/update-cloudflare-realip#!/bin/sh
set -e
OUT=/etc/nginx/conf.d/cloudflare-realip.conf
TMP=$(mktemp)
{
echo "real_ip_header CF-Connecting-IP;"
for ip in $(curl -fsS https://www.cloudflare.com/ips-v4) $(curl -fsS https://www.cloudflare.com/ips-v6); do
echo "set_real_ip_from $ip;"
done
} > "$TMP"
if [ "$(grep -c set_real_ip_from "$TMP")" -gt 5 ]; then
mv "$TMP" "$OUT"
chmod 644 "$OUT"
nginx -t && systemctl reload nginx
else
rm -f "$TMP"
echo "Download looked wrong, keeping the old file" >&2
exit 1
fiMake it executable and run it:
sudo chmod +x /usr/local/bin/update-cloudflare-realip
sudo /usr/local/bin/update-cloudflare-realipThe result is a file in conf.d, loaded in the http context, with one set_real_ip_from line per Cloudflare range. Look at it:
cat /etc/nginx/conf.d/cloudflare-realip.confTo keep it current, run the script weekly from root's crontab (sudo crontab -e):
30 4 * * 1 /usr/local/bin/update-cloudflare-realipOther proxies and load balancers
For a load balancer or a second Nginx in front, trust its address and read X-Forwarded-For. Create /etc/nginx/conf.d/realip.conf:
set_real_ip_from 10.0.0.5;
real_ip_header X-Forwarded-For;
real_ip_recursive on;Use the proxy's private or public IP, never 0.0.0.0/0. If the proxy supports the PROXY protocol instead of headers, use listen 443 ssl proxy_protocol; together with real_ip_header proxy_protocol;.
Keep logs and rate limits accurate
Once $remote_addr holds the real IP:
- The default access log format already logs the visitor, not Cloudflare.
limit_req_zone $binary_remote_addr ...now limits each visitor separately. See Rate limit requests in Nginx.allowanddenyrules apply to visitors. See Allow or block IP addresses in Nginx.- Fail2ban reads the real IP from the logs. Because traffic still arrives from Cloudflare, a firewall ban on the server will not stop it; ban at Cloudflare or with an Nginx deny list instead.
If you want both addresses in the log for debugging, add $realip_remote_addr (the original connecting IP) to a custom log_format.
Apply the change
If you created the files by hand rather than with the script:
sudo nginx -t && sudo systemctl reload nginxCheck it works
Visit your site from your own browser, then look at the last log line:
sudo tail -n 3 /var/log/nginx/access.logThe first field should be your own public IP, not an address from Cloudflare's ranges. You can check your public IP by searching "what is my IP" in a browser.
On PHP sites, a test file that prints $_SERVER['REMOTE_ADDR'] should show the same. Delete the test file afterwards.
Troubleshooting
- Logs still show Cloudflare IPs: the include file is not loaded or the site is not proxied. Run
sudo nginx -T | grep real_ipand check the cloud icon in Cloudflare's DNS settings. - `nginx -t` says "real_ip_header" directive is duplicate: it is set in two places, often in a control panel include. Keep one.
- All visitors appear as one internal IP: a second proxy sits between Cloudflare and Nginx. Trust both and use
X-Forwarded-Forwithreal_ip_recursive on. - The script fails with a curl error: the server cannot reach Cloudflare's site. The old file stays in place; check DNS and outbound HTTPS.
- Visitors bypass Cloudflare by using your server IP: lock the origin down. See Allow only Cloudflare to reach your origin.
Next steps
- Use Cloudflare well with your server: Use Cloudflare CDN with your VPS.
- Encrypt the Cloudflare-to-origin hop: Cloudflare origin certificate on Nginx.
- Run your sites on a Linux VPS.
Was this guide helpful?
Still stuck?
Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.
Related guides
Websites & apps
Install PHP 8.x on AlmaLinux and Rocky Linux with Remi
Enable EPEL and the Remi repository on AlmaLinux or Rocky Linux 8 and 9, choose a PHP module stream, install PHP-FPM with common extensions, and run extra PHP versions side by side.
- Intermediate
- Linux VPS
- 4 min read
Websites & apps
Use Apache as a reverse proxy with mod_proxy
Put Apache in front of a Node.js, Python or Docker app with mod_proxy. Set ProxyPass and ProxyPassReverse, keep the original host, proxy WebSockets and serve the app over HTTPS.
- Intermediate
- Linux VPS
- 4 min read
Websites & apps
Enable gzip compression in Apache with mod_deflate
Compress HTML, CSS, JavaScript and JSON in Apache with mod_deflate, add Brotli with mod_brotli for modern browsers and check the Content-Encoding header to prove it works.
- Beginner
- Linux VPS
- 4 min read