Setting the HTTP_CF_CONNECTING_IP
header in an Apache HTTP server configuration involves using the mod_headers
module to set or modify the header value. The HTTP_CF_CONNECTING_IP
header is typically used with Cloudflare to pass the original client IP address to your server.
Here’s how you can configure it:
- Enable the
mod_headers
Module:
Ensure that themod_headers
module is enabled. You can enable it by running the following command:
sudo a2enmod headers
Then, restart Apache to apply the changes:
sudo systemctl restart apache2
- Configure the Header in Your Apache Configuration File:
You need to add the configuration to your Apache virtual host configuration file, which is usually located in/etc/apache2/sites-available/
for Debian-based distributions or/etc/httpd/conf.d/
for Red Hat-based distributions. Open your virtual host configuration file, for example:
sudo nano /etc/apache2/sites-available/000-default.conf
Add the following lines inside the <VirtualHost>
block:
<VirtualHost *:80>
ServerName yourdomain.com
# Other configurations...
# Set HTTP_CF_CONNECTING_IP header
<IfModule mod_headers.c>
RequestHeader set HTTP_CF_CONNECTING_IP "%{HTTP:X-Forwarded-For}e"
</IfModule>
# Other configurations...
</VirtualHost>
If you are using HTTPS, make sure to update the configuration in the corresponding SSL virtual host file, typically named something like default-ssl.conf
.
- Restart Apache:
After making these changes, restart Apache to apply the new configuration:
sudo systemctl restart apache2
Explanation:
- RequestHeader set: This directive is used to set the value of the
HTTP_CF_CONNECTING_IP
header. - “%{HTTP:X-Forwarded-For}e”: This extracts the
X-Forwarded-For
header, which is set by Cloudflare and contains the original client IP address.
Example with SSL:
If you are using HTTPS, you should update your SSL virtual host configuration file as well, for example:
sudo nano /etc/apache2/sites-available/default-ssl.conf
And add:
<VirtualHost *:443>
ServerName yourdomain.com
# Other configurations...
# Set HTTP_CF_CONNECTING_IP header
<IfModule mod_headers.c>
RequestHeader set HTTP_CF_CONNECTING_IP "%{HTTP:X-Forwarded-For}e"
</IfModule>
# Other configurations...
</VirtualHost>
After making these changes, restart Apache again:
sudo systemctl restart apache2
By following these steps, you can set the HTTP_CF_CONNECTING_IP
header in your Apache HTTP server configuration, allowing you to correctly pass and use the original client IP address provided by Cloudflare.