Script for Nginx/Openresty to restore up-to-date source IP addresses of visitors from Cloudflare
Set params like NGINX_BIN_PATH
and CLOUDFLARE_CONFIG_PATH
echo "NGINX_BIN_PATH=/usr/bin/openresty" > /etc/cloudflare-real-ip-update.env
Create dirs
mkdir -p /etc/nginx/includes.d /opt/scripts
Download files
gist_url=https://gist.githubusercontent.com/WoozyMasta/602035f50156a812c20e259189da1422
script_name=cloudflare-real-ip-update
get_gist() { curl -sSfL "$gist_url/raw/$2" -o "$1/$2"; }
# Download files
get_gist /etc/systemd/system "$script_name.service"
get_gist /etc/systemd/system "$script_name.timer"
get_gist /opt/scripts "$script_name.sh"
chmod +x /opt/scripts/"$script_name.sh"
Edit nginx.conf
, comment real_ip_header
and real_ip_recursive
if exists and add include for cloudflare-ip.conf
{
...
# real_ip_header X-Forwarded-For;
# real_ip_recursive on;
include includes.d/cloudflare-ip.conf;
}
Edit timer shedule if needed
editor "/etc/systemd/system/$script_name.timer"
Try execute script and check output and Nginx config
/opt/scripts/cloudflare-real-ip-update.sh
less /etc/nginx/includes.d/cloudflare-ip.conf
nginx -t
# or
openresty -t
Enable service
systemctl enable "$script_name.timer"