Restoring original visitor IP address

When your website traffic is routed through the Cloudflare network, we act as a reverse proxy. This allows Cloudflare to speed up page load time by routing packets more efficiently and caching static resources (images, JavaScript, CSS, etc.). As a result, when responding to requests and logging them, your origin server returns a Cloudflare IP address.

For example, if you install applications that depend on the incoming IP address of the original visitor, a Cloudflare IP address is logged by default. The original visitor IP address appears in an appended HTTP header called CF-Connecting-IP. With the mod_cloudflare tool, you can log the original visitor IP address at your origin server.

In a CloudStick powered server, we can add extra Nginx configurations specific to our web application. In the “/etc/nginx-cs/extra.d” directory, we can find subdirectories in the name of our web application. Inside it, we can find a file named ‘main-post.conf’. Where we can add our extra configuration.

Let’s open the ‘main-post.conf’ file that corresponds to our web application on a file editor, (replace app-name with your web application name)

vim /etc/nginx-cs/extra.d/app-name.d/main-post.conf

Then paste the following content into it.

#Cloudflare ip addresses

# – IPv4
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 131.0.72.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;

# – IPv6
set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2a06:98c0::/29;
set_real_ip_from 2c0f:f248::/32;

real_ip_header CF-Connecting-IP;

Let’s check for any errors in Nginx configuration using the command,

nginx-cs -t

If there are no errors, restart the Nginx service using command or from the services page,

systemctl restart nginx-cs.service