这个答案是一个替代方案,它允许从 nginx 解析域,但目标完全相同,能够解析 nginx 配置中包含的 ips。
1) 创建一个文件allowed-domain.list,其中包含您要授予访问权限的域:
jean-paul.mydomain.com
rufus.mydomain.com
robert.mydomain.com
2) 创建一个 bash 脚本 domain-resolver.sh 为您进行查找:
#!/usr/bin/env bash
filename="$1"
while read -r line
do
ddns_record="$line"
if [[ ! -z $ddns_record ]]; then
resolved_ip=`getent ahosts $line | awk '{ print $1 ; exit }'`
if [[ ! -z $resolved_ip ]]; then
echo "allow $resolved_ip;# from $ddns_record"
fi
fi
done < "$filename"
3) 授予此脚本正确的权限chmod +x domain-resolver.sh
4) 添加一个生成有效 nginx 配置并重新启动 nginx 的 cron 作业:
#!/usr/bin/env bash
/pathtoscript/domain-resolver.sh /pathtodomainlist/allowed-domain.list > /pathtooutputdir/allowed-ips-from-domains.conf
service nginx reload > /dev/null 2>&1
这可以是@daily 作业,也可以让它每小时、每分钟、每秒钟运行一次...
5) 更新您的 nginx 配置以考虑此输出:
include /pathtooutputdir/allowed-ips-from-domains.conf;
deny all;
您可以改进这一点,添加一个 ip 格式检查,如果您不想要它,阻止 ipv6,将所有内容分组到一个文件中...