Skip to content

Instantly share code, notes, and snippets.

@AnttiKurittu
Created December 24, 2016 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnttiKurittu/62643e6d7398ed3accfe4c4350c7ea78 to your computer and use it in GitHub Desktop.
Save AnttiKurittu/62643e6d7398ed3accfe4c4350c7ea78 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Get and refresh the Cloudflare IP range and set them in a configuration
# file for Nginx. Rememember to include this in your server directive
# with 'include /path/to/this/script.sh;'
# add this to your crontab with 'crontab -e'
# 30 5 * * * /etc/nginx/sites-available/get-cf-ip-range.sh
set -e
wget -q -P /etc/nginx/sites-available/ https://www.cloudflare.com/ips-v4
wget -q -P /etc/nginx/sites-available/ https://www.cloudflare.com/ips-v6
cat /etc/nginx/sites-available/ips-v6 >>/etc/nginx/sites-available/ips-v4
echo "# Get cloudflare IP range and set real IP for logs." >/etc/nginx/sites-available/cloudflare-set-real-ip
for i in $(cat /etc/nginx/sites-available/ips-v4);
do echo "set_real_ip_from $i;" >>/etc/nginx/sites-available/cloudflare-set-real-ip;
done
echo "real_ip_header CF-Connecting-IP;" >>/etc/nginx/sites-available/cloudflare-set-real-ip
rm /etc/nginx/sites-available/ips-v*
/usr/sbin/nginx -t -q
/usr/sbin/nginx -s reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment