Nginx 配置文件升级和回滚
1. 简介¶
Nginx在升级之前,将站点目录指定到临时升级目录。这样访问站点的时候会页面会显示正在升级,升级完毕后在指定回来。
2. 脚本内容¶
nginx 配置文件升级和回滚
#!/bin/sh # update and rollback the nginx withing no perception. # file : Configuration files refer to nginx vhosts file source_path=/opt/backup update_nginx_conf_file=/usr/local/nginx/conf/backconf/maintain.conf nginx_root_conf=/usr/local/nginx/conf/vhosts/ #--------------------------------------------------------------------- # function: check parameter # #--------------------------------------------------------------------- if [ "$#" -ne 2 ];then echo "the parameter is not equal to 2" echo "USG: sh $0 update|rollback nginx_confile" exit 1 fi #--------------------------------------------------------------------- # function: check nginx confile and reload # #--------------------------------------------------------------------- reload_nginx(){ /usr/local/nginx/sbin/nginx -t if [ $? -eq 0 ];then /usr/local/nginx/sbin/nginx -s reload else echo "nginx 配置文件出错" exit 1 fi } #--------------------------------------------------------------------- # function: update nginx file # #--------------------------------------------------------------------- update(){ file=$1 [ ! -d $source_path ] && mkdir -p $source_path mv $nginx_root_conf/$file.conf $source_path cp $update_nginx_conf_file $nginx_root_conf/$file.conf reload_nginx } #--------------------------------------------------------------------- # function: rollback nginx file # #--------------------------------------------------------------------- rollback(){ [ ! -e $source_path/${file}.conf ] && { echo "can not find $source_path/${file}.conf" exit 1 } mv $source_path/${file}.conf nginx_root_conf reload_nginx } #--------------------------------------------------------------------- # function: show all vhosts machine withing nginx conf name # #--------------------------------------------------------------------- help_nginx_vhosts(){ path=/usr/local/nginx/conf/vhosts for item in $(ls -l $path |grep -v total|awk '{print $NF}'|awk -F. '/conf$/{print $1}') do echo " $item" done } #--------------------------------------------------------------------- # function: check nginx configuration file legitimacy # #--------------------------------------------------------------------- check_nginx_file(){ nginx_conf_file=$1 path=/usr/local/nginx/conf/vhosts all_vhosts=$(ls -l $path |grep -v total|awk '{print $NF}'|awk -F. '/conf$/{print $1}') num=$(echo $all_vhosts|grep -w $nginx_conf_file|wc -l) if [ $num -eq 0 ];then echo "Error: input nginx conf file is not find.The current configuration file is as follows:" help_nginx_vhosts exit 1 fi } #--------------------------------------------------------------------- # function: Program Entry # #--------------------------------------------------------------------- main(){ action=$1 file=$2 if [ $action == "update" ];then check_nginx_file $file echo "now this system nginx withing $file.conf will update" update $file elif [ $action == "rollback" ];then echo "now this system nginx withing $file.conf will rollback" check_nginx_file $file rollback $file else echo "error: This operation is not allowed" echo "USG: sh $0 update|rollback nginx_confile" fi } main $1 $2