加一个定时任务,每天定时执行下面的sh脚本

SH脚本如下:

#!/usr/bin/env bash
# 功能: 生成 cloudflare 代理IP列表,用户配置nginx获取客户端真实IP地址
cf_ipv4="https://www.cloudflare.com/ips-v4"
cf_ipv6="https://www.cloudflare.com/ips-v6"
mod_cffile="/www/path/cloudflare.conf"
get_cfipinfo() {
    # 生成nginx配置记录格式: set_real_ip_from 103.21.244.0/22;
    curl $cf_ipv4 2>/dev/null | grep -v '#' | grep -v '^$' | sed 's/^/set_real_ip_from /g;s/$/;/g'
    curl $cf_ipv6 2>/dev/null | grep -v '#' | grep -v '^$' | sed 's/^/set_real_ip_from /g;s/$/;/g'
    echo
    # echo "real_ip_header CF-Connecting-IP;"
    echo "real_ip_header X-Forwarded-For;" >> $mod_cffile
    echo
}
et_cfipinfo  | tee $mod_cffile
nginx -t && nginx -s reload

在nginx的conf配置文件中加入

include /www/path/cloudflare.conf;

参考了:https://blog.csdn.net/dragonballs/article/details/126345175

扩展一点关于ip伪造的资料:

关于:x-forwarded-for的ip伪造及防范