I'm prepping a new webhost to be served behind Cloudflare. Cloudflare advises that you will need to implement the real_ip
module if you use nginx and want to know where your traffic is really coming from. The method to do so is described here:
Below the list of Cloudflare IP addresses is the following caveat:
NB: That list of prefixes needs to be updated regularly
It's unclear what the frequency of "regularly" is, but clearly we need to monitor this list for updates. If we need to monitor this list for updates, then we may as well assign that task to a robot. If the robot is monitoring the list, then it may as well be responsible for updating the nginx configuration too. Here's a script to do just that.
(Alternative, I have also made this available as a facter fact over here).
Hopefully the script is easy to parse, but just in case it isn't, here is what it does:
- Downloads a list of return-delimited IPv4 and IPv6 addresses from Cloudflare
- Cleans
/etc/nginx/real_ip.conf
- Validates each IP address and writes valid ones into
/etc/nginx/real_ip.conf
- Reloads nginx via
/bin/systemctl
Every setup is a little different, so this will not necessarily be a "drop-in" addition. It's simple and short enough to modify to your needs. As a set of general guidelines though:
- Install the script somewhere convenient like
/usr/local/bin/
- Run the script and validate that
/etc/nginx/real_ip.conf
looks sane - Setup a cron to run the script daily
- Add
include /etc/nginx/real_ip.conf
to your nginx config(s)
This script makes certain assumptions which may cause you grief:
- The script cleans
/etc/nginx/real_ip.conf
before validating IP addresses- Ideally the script should abort if no IPv4 or IPv6 addresses are found
- The script assumes a return-delimited list
- The script assumes you are using Cloudflare
- The script assumes you are using systemd
- The script assumes it will be run under a user that has permission to reload nginx
- The script will not attempt to validate your nginx configuration before reloading
- This might get fixed or at least hardened
Use/modify/deploy at your own risk.
Feel free to fork and make pull requests.