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

By RunRacks Team · Published

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:

Terminal
nginx -V 2>&1 | grep -o with-http_realip_module

How 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 send X-Forwarded-For.
  • `real_ip_recursive` matters when there are several proxies in a chain. With on, Nginx walks back through X-Forwarded-For and 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.

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:

Terminal
sudo nano /usr/local/bin/update-cloudflare-realip
Terminal
#!/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
fi

Make it executable and run it:

Terminal
sudo chmod +x /usr/local/bin/update-cloudflare-realip
sudo /usr/local/bin/update-cloudflare-realip

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

Terminal
cat /etc/nginx/conf.d/cloudflare-realip.conf

To keep it current, run the script weekly from root's crontab (sudo crontab -e):

Text
30 4 * * 1 /usr/local/bin/update-cloudflare-realip

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

NGINX
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.
  • allow and deny rules 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:

Terminal
sudo nginx -t && sudo systemctl reload nginx

Check it works

Visit your site from your own browser, then look at the last log line:

Terminal
sudo tail -n 3 /var/log/nginx/access.log

The 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_ip and 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-For with real_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

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.

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