Created
November 21, 2020 17:25
-
-
Save skarlcf/261c7c06ac90f3c78f7b1d6bd9619c45 to your computer and use it in GitHub Desktop.
Ruby script to get Amazon AWS Cloudfront IPs addresses and prepare nginx configuration (ngx_http_realip_module)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| require 'bundler/inline' | |
| require 'net/http' | |
| gemfile do | |
| source 'https://rubygems.org' | |
| gem 'json' | |
| gem 'pry' | |
| end | |
| AMAZON_IP_RANGES = 'https://ip-ranges.amazonaws.com/ip-ranges.json' | |
| puts "# Amazon AWS Cloudfront IPs from #{AMAZON_IP_RANGES}" | |
| uri = URI(AMAZON_IP_RANGES) | |
| res = Net::HTTP.get_response(uri) | |
| raise(StandardError, "Can't get remote file") unless res.is_a?(Net::HTTPSuccess) | |
| x = JSON.parse(res.body) | |
| x['prefixes'].select { |e| e['service'] == 'CLOUDFRONT' }.map { |e| puts "set_real_ip_from #{e['ip_prefix']};" } | |
| puts %{ | |
| # real_ip_header X-Forwarded-For; | |
| # real_ip_recursive on; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example result: