Configuring NGINX to Retrieve Real IP from Cloudflare
When using Cloudflare as a reverse proxy, all incoming requests to your server will appear to come from Cloudflare’s IP addresses instead of the actual client IP addresses. To correctly log the real IP address of the client for auditing purposes, you need to configure NGINX to extract and log the client’s real IP address. This guide explains how to configure NGINX to retrieve the real client IP from Cloudflare.Step-by-Step Configuration
-
Update Cloudflare IP Ranges in NGINX Configuration
To retrieve the real IP address of a client, you must inform NGINX about the IP ranges used by Cloudflare. This allows NGINX to trust the
X-Forwarded-FororCF-Connecting-IPheaders provided by Cloudflare. To Get Ip list of cloudflareVisit:https://www.cloudflare.com/en-in/ips/ Add the following lines to your NGINX configuration file (usually found in/etc/nginx/nginx.confor a specific configuration file in/etc/nginx/conf.d/):These lines tell NGINX to trust Cloudflare’s proxy addresses, allowing it to accept the real client IP forwarded by Cloudflare. -
Specify the Header for the Real IP
Next, specify which header NGINX should use to determine the real client IP. Cloudflare uses the
CF-Connecting-IPheader to pass the original client IP. Add the following directive:This tells NGINX to look for the client’s real IP address in theCF-Connecting-IPheader. -
Enable Recursive Real IP Resolution
To ensure NGINX processes the
real_ip_headercorrectly even if multiple proxies are involved, enable recursive resolution:Withreal_ip_recursiveset toon, NGINX will search the entire header chain to find the first non-trusted IP and use that as the client’s IP. -
Log the Real IP
Ensure your access logs are set to log the correct client IP. This is usually the default behavior if
real_ip_headeris set correctly. You can verify your logging format in thehttpblock of your NGINX configuration:The access log will now record the real client IP address. -
Reload NGINX Configuration
After making the changes, save the configuration file and reload NGINX to apply the new settings:
-
Example configuration
nginx.conf
Use Case
By following this configuration, you can ensure that your NGINX server logs and utilizes the real IP address of clients who access your site through Cloudflare. This is particularly useful for:- Security Auditing: Keeping accurate logs of visitor IP addresses for security and auditing purposes.
- Rate Limiting and Access Control: Implementing rate limits and access controls based on the actual IP address of the client, rather than Cloudflare’s proxy IPs.
- Troubleshooting and Analysis: Better understanding of traffic patterns and potential malicious activity by correctly identifying client IP addresses.
This documentation should help you configure your NGINX server to correctly interpret the real client IP addresses behind the Cloudflare proxy. Make sure to regularly check for updates to Cloudflare’s IP ranges and update your configuration accordingly.
