Skip to content

Instantly share code, notes, and snippets.

@LouisKottmann
Last active December 19, 2015 12:38
Show Gist options
  • Save LouisKottmann/5956159 to your computer and use it in GitHub Desktop.
Save LouisKottmann/5956159 to your computer and use it in GitHub Desktop.
Fetches CloudFlare's official IPv4 and IPv6 addresses and formats them for use from NGinx's HttpRealipModule
# File name: cf_nginx_realip.rb
if ARGV.first == "--help"
puts "Fetches CloudFlare's official IPv4 and IPv6 addresses and formats them for use from NGinx's HttpRealipModule"
puts "Usage:\n\truby cf_nginx_realip.rb > cf_realip.conf\n\n"
puts 'Then add "include cf_realip.conf" in a http|server|location block of your nginx config'
exit
end
ips = []
ips << `curl -Ls https://www.cloudflare.com/ips-v4`
ips << `curl -Ls https://www.cloudflare.com/ips-v6`
puts ips.join("\n")
.split("\n")
.map { |ip_addr| "set_real_ip_from #{ip_addr};" }
.push("real_ip_header CF-Connecting-IP;")
.join("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment