ubuntu@front-1-plg:~$ cat /data/shell/nginx_ip_allow.sh
#!/bin/bash
# Author: royoy
# Date : 2019/06
# Desc : ./nginx_ip_allow.sh "注释" "IP列表"
# Eg : ./nginx_ip_allow.sh "hrp白名单" "192.168.0.1 192.168.0.2"
nginx_allow_conf=/etc/nginx/conf.d/allow.conf
nginx_bin=/usr/sbin/nginx
comment="${1} -Added by ${3} on `date +"%F %T"`"
ips="$2"
if [ -z "$comment" ];then echo "注释不能为空";exit 1;fi
if [ -z "$ips" ];then echo "IP不能为空";exit 1;fi
# Check if ip is valid
ipcheck "$ips" || exit
# Add comment
sed -i "/deny all/i ##$comment" $nginx_allow_conf
# Add ips
for ip in $ips
do
sed -i "/deny all/i allow $ip;" $nginx_allow_conf
done
# Check if nginx conf is correct and reload nginx
$nginx_bin -q -t && {
$nginx_bin -s reload && echo "`hostname`: OK" || {
echo "`hostname`: nginx reload failed";exit 2
}
} || {
echo "`hostname`: nginx配置文件有错误"
exit 1
}