Skip to content

Instantly share code, notes, and snippets.

@Saren-Arterius
Created August 7, 2019 08:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Saren-Arterius/30ff3f65b307758830969accac21d924 to your computer and use it in GitHub Desktop.
Save Saren-Arterius/30ff3f65b307758830969accac21d924 to your computer and use it in GitHub Desktop.
CloudFlare IP whitelist & set_real_ip_from
#!/bin/bash
set -x
echo "#Cloudflare" > /etc/nginx/conf.d/00_real_ip_cloudflare_00.conf;
ips=$(curl https://www.cloudflare.com/ips-v4)
iptables -F cloudflare;
iptables -N cloudflare;
iptables -C INPUT -p tcp -m multiport --dports http,https -j cloudflare || iptables -A INPUT -p tcp -m multiport --dports http,https -j cloudflare
iptables -A cloudflare -p tcp -m multiport --dports http,https -s 127.0.0.0/8 -j ACCEPT;
for i in $ips; do
iptables -A cloudflare -p tcp -m multiport --dports http,https -s $i -j ACCEPT;
echo "set_real_ip_from $i;" >> /etc/nginx/conf.d/00_real_ip_cloudflare_00.conf;
done
iptables -A cloudflare -j DROP
echo "real_ip_header CF-Connecting-IP;" >> /etc/nginx/conf.d/00_real_ip_cloudflare_00.conf;
nginx -s reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment