您的位置:首页 > 运维架构 > Nginx

脚本添加删除nginx配置中的内容

2015-12-29 14:29 741 查看
[root@nodejs script]# more editnginx.sh
#!/bin/bash
#

function back_check(){

# 备份配置和覆盖配置文件
cp -rf /etc/nginx/nginx.conf /etc/nginx/nginx.conf-$(date +%Y%m%d-%H%M%S) && \
echo "[+] /etc/nginx/nginx.conf-$(date +%Y%m%d-%H%M%S) backup OK!"

# 对比修
diff nginx.conf /etc/nginx/nginx.conf

# 覆盖线上配置
read -p "Write nginx config File(y/n)?" inString
if [ $inString == "y" ];then
cp -rf nginx.conf /etc/nginx/nginx.conf
elif [ $inString == "n"];then
echo "Quit!!!"
exit 1
else
echo "args error!!"
exit 2
fi

nginx -t -c /etc/nginx/nginx.conf
}

# 添加节点函数
function insert_row(){
# 编辑配置文件
nginx -t -c /etc/nginx/nginx.conf
RETURN=$?

if [ $RETURN == 0 ];then
cp -rf /etc/nginx/nginx.conf nginx.conf && echo "[+] Copy nginx.conf to current directory OK!!"

sed -i -n -e :a -e '1,2!{P;N;D;};N;ba' nginx.conf

echo "        location ~/api/v$1 {
proxy_pass http://127.0.0.1:60$1; }
}
}" >>nginx.conf

fi

back_check
}

# 删除节点函数
function delete_row(){
cp -rf /etc/nginx/nginx.conf nginx.conf && echo "[+] Copy nginx.conf to current directory OK!!"
sed -i "/location ~\/api\/v$1/,/}/d" nginx.conf
back_check
#sed -n "$1p" nginx.conf
}

# 帮助选项
function help(){
echo -e "Usage: $(basename $0) {-h|-i|-d}\n"
}

if [ -z $1 ];then
help
fi

while getopts "i:d:h" OPTIONS; do
case ${OPTIONS} in
i ) insert_row $OPTARG;;
d ) delete_row $OPTARG;;
h ) help; exit;;
esac
done


# 添加





# 删除



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: