Skip to content

Instantly share code, notes, and snippets.

@MikeDacre
Created September 12, 2017 19: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 MikeDacre/df4fa090cfb06bad79dffd400af4088d to your computer and use it in GitHub Desktop.
Save MikeDacre/df4fa090cfb06bad79dffd400af4088d to your computer and use it in GitHub Desktop.
Sync cloudflare IPs with NGINX
# Based on https://support.cloudflare.com/hc/en-us/articles/200170706-How-do-I-restore-original-visitor-IP-with-Nginx-
# Save this file to /usr/bin/clodflare_refresh_nginx.sh
# Add the line "include /etc/nginx/global/cloudflare-ip.conf" to the server
# block of every nginx site you wish to expose the real IP for
# Add crontab (run a 03:05 every day): 5 3 * * * /usr/bin/clodflare_refresh_nginx.sh >/dev/null 2>&1
NGINX_CONF=/etc/nginx/global/cloudflare-ip.conf
curl https://www.cloudflare.com/ips-v4 | perl -p -e 's/^(.*)$/set_real_ip_from \1;/g' > $NGINX_CONF
curl https://www.cloudflare.com/ips-v6 | perl -p -e 's/^(.*)$/set_real_ip_from \1;/g' >> $NGINX_CONF
# use any of the following two
echo "real_ip_header CF-Connecting-IP;" >> $NGINX_CONF
# echo "real_ip_header X-Forwarded-For;" >> $NGINX_CONF
@dvershinin
Copy link

For CentOS 7, there are packages with those lists, which can be updated via yum update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment