├── .gitignore ├── LICENSE ├── README.md ├── docs ├── README.md └── _config.yml └── nginx-cloudflare-real-ip.sh /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | \.history/ 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 VirtuBox 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nginx-cloudflare-real-ip 2 | 3 | Configure Nginx to restore Visitors real IP under Cloudflare CDN 4 | 5 | ## Features 6 | 7 | * Get Cloudflare IPv4 + IPv6 list and create nginx configuration to restore visitors real IP in `/etc/nginx/conf.d/cloudflare.conf` 8 | * Whitelist Cloudflare IPs on port 80 & 443 with UFW (optional) 9 | 10 | ## Requirements 11 | 12 | * Nginx built with http_realip_module 13 | 14 | You can check if http_realip_module available with : 15 | 16 | ```bash 17 | nginx -V 2>&1 | grep with-http_realip_module 18 | ``` 19 | 20 | If the previous command return nothing, http_realip_module isn't available 21 | 22 | --- 23 | 24 | ## Usage 25 | 26 | Nginx configuration only 27 | 28 | ```bash 29 | bash <(wget -O - vtb.cx/nginx-cloudflare || curl -sL vtb.cx/nginx-cloudflare) 30 | ``` 31 | 32 | Nginx configuration + UFW configuration 33 | 34 | ```bash 35 | bash <(wget -O - vtb.cx/nginx-cloudflare || curl -sL vtb.cx/nginx-cloudflare) --ufw 36 | ``` 37 | 38 | Published & maintained by [VirtuBox](https://virtubox.net) -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # nginx-cloudflare-real-ip 2 | 3 | Configure Nginx to restore Visitors real IP under Cloudflare CDN 4 | 5 | ## Features 6 | 7 | * Get Cloudflare IPv4 + IPv6 list and create nginx configuration to restore visitors real IP in `/etc/nginx/conf.d/cloudflare.conf` 8 | * Whitelist Cloudflare IPs on port 80 & 443 with UFW (optional) 9 | 10 | ## Requirements 11 | 12 | * Nginx built with http_realip_module 13 | 14 | You can check if http_realip_module available with : 15 | 16 | ```bash 17 | nginx -V 2>&1 | grep with-http_realip_module 18 | ``` 19 | 20 | If the previous command return nothing, http_realip_module isn't available 21 | 22 | --- 23 | 24 | ## Usage 25 | 26 | Nginx configuration only 27 | 28 | ```bash 29 | bash <(wget -O - vtb.cx/nginx-cloudflare || curl -sL vtb.cx/nginx-cloudflare) 30 | ``` 31 | 32 | Nginx configuration + UFW configuration 33 | 34 | ```bash 35 | bash <(wget -O - vtb.cx/nginx-cloudflare || curl -sL vtb.cx/nginx-cloudflare) --ufw 36 | ``` 37 | 38 | Published & maintained by [VirtuBox](https://virtubox.net) -------------------------------------------------------------------------------- /docs/_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /nginx-cloudflare-real-ip.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Simple bash script to restore visitor real IP under Cloudflare with Nginx 3 | # Script also whitelist cloudflare IP with UFW (if installed) 4 | 5 | if [ "$1" = "--ufw" ]; then 6 | CF_UFW_SETUP="y" 7 | fi 8 | 9 | if [ -z "$(command -v curl)" ]; then 10 | echo "####################################" 11 | echo "Installing CURL" 12 | echo "####################################" 13 | apt-get update 14 | apt-get install curl -y 15 | fi 16 | 17 | CURL_BIN=$(command -v curl) 18 | CF_IPV4=$($CURL_BIN -sL https://www.cloudflare.com/ips-v4) 19 | CF_IPV6=$($CURL_BIN -sL https://www.cloudflare.com/ips-v6) 20 | 21 | [ ! -d /etc/nginx/conf.d ] && { 22 | mkdir -p /etc/nginx/conf.d 23 | } 24 | 25 | echo '' > /etc/nginx/conf.d/cloudflare.conf 26 | echo "####################################" 27 | echo "Adding Cloudflare IPv4" 28 | echo "####################################" 29 | for cf_ip4 in $CF_IPV4; do 30 | echo "set_real_ip_from $cf_ip4;" >> /etc/nginx/conf.d/cloudflare.conf 31 | if [ "$CF_UFW_SETUP" = "y" ]; then 32 | ufw allow from $cf_ip4 to any port 80 33 | ufw allow from $cf_ip4 to any port 443 34 | fi 35 | done 36 | echo "####################################" 37 | echo "Adding Cloudflare IPv6" 38 | echo "####################################" 39 | for cf_ip6 in $CF_IPV6; do 40 | echo "set_real_ip_from $cf_ip6;" >> /etc/nginx/conf.d/cloudflare.conf 41 | if [ "$CF_UFW_SETUP" = "y" ]; then 42 | ufw allow from $cf_ip6 to any port 80 43 | ufw allow from $cf_ip6 to any port 443 44 | fi 45 | done 46 | echo 'real_ip_header CF-Connecting-IP;' >> /etc/nginx/conf.d/cloudflare.conf 47 | 48 | if [ "$CF_UFW_SETUP" = "y" ]; then 49 | echo "####################################" 50 | echo "Reloading UFW" 51 | echo "####################################" 52 | ufw reload 53 | fi 54 | --------------------------------------------------------------------------------