linux脚本修改nginx配置,Shell脚本修改Nginx upstream配置文件

#!/bin/bash

#####################################################

# Name: change_nginx_upstream_conf.sh

# Version: V1.

# Author: 运维菜鸟

# Description: 更改nginx upstream配置文件

# Create Date: --

# Email:

#####################################################

#function name

function_name=$

#pool name

pool_name=$

#pool corresponding ip list

pool_ip_lists=$

#pool corresponding tomcat port

pool_tomcat_port=$

#upstream file location

ngx_upstream_file=$

#检测pool在nginx upstream配置文件中是否存在

function check_pool_in_ngx_upstream() {

grep -E "${pool_name}[^-]" ${ngx_upstream_file} >> /dev/null

if [ $? -eq ];then

echo -e "\033[36m the ${pool_name} in ${ngx_upstream_file}. \033[0m"

else

echo -e "\033[31m the ${pool_name} not in ${ngx_upstream_file}. \033[0m"

exit

fi

}

#显示pool在nginx upstream配置文件中对应内容

function show_pool_in_ngx_upstream() {

pool_name_first_line=`egrep -n "${pool_name}[^-]" ${ngx_upstream_file} | cut -d ":" -f1`

line_list=`grep -n "^}" ${ngx_upstream_file} | cut -d ":" -f1`

pool_name_end_line=${pool_name_first_line}

for line in ${line_list[*]};do

if [ $line -gt ${pool_name_first_line} ];then

pool_name_end_line=${line}

break;

fi

done

sed -n "${pool_name_first_line},${pool_name_end_line}p" ${ngx_upstream_file}

}

#增加pool进nginx upstream配置文件

function add_pool_to_upstream() {

#pool对应ip地址列表,多个ip以逗号改开

pool_ip=`awk 'BEGIN{list="'${pool_ip_lists}'";split(list,ip_list,",");for(ip in ip_list){print ip_list[ip];}}'`

for ip in ${pool_ip[*]};do

echo "add ${pool_name} ${ip} in ${ngx_upstream_file}"

sed -i '/upstream '${pool_name}'[^-]*{/a\\tserver '${ip}':'${pool_tomcat_port}';' ${ngx_upstream_file}

done

echo -e "\033[31m ====添加完成如下:==== \033[0m"

}

#在nginx upstream配置文件删除pool对应的ip地址

function delete_ip_from_upstream() {

pool_name_first_line=`egrep -n "${pool_name}[^-]" ${ngx_upstream_file} | cut -d ":" -f1`

line_list=`grep -n "^}" ${ngx_upstream_file} | cut -d ":" -f1`

pool_name_end_line=${pool_name_first_line}

for line in ${line_list[*]};do

if [ $line -gt ${pool_name_first_line} ];then

pool_name_end_line=${line}

break;

fi

done

#获取pool对应配置行数

line_count=`sed -n "${pool_name_first_line},${pool_name_end_line}p" ${ngx_upstream_file} | wc -l`

#如果某个pool的配置行数等于3,则不能进行删除操作

if [ ${line_count} -eq ];then

echo -e "\033[31m this is lowest configure. \033[0m"

fi

#删除pool_ip_lists中包含的ip地址

for ((i=${pool_name_first_line};i<=${pool_name_end_line};i++));do

pool_ip=`awk 'BEGIN{list="'${pool_ip_lists}'";split(list,ip_list,",");for(ip in ip_list){print ip_list[ip];}}'`

line_context=`sed -n ''${i}'p' ${ngx_upstream_file}`

for ip in ${pool_ip[*]};do

echo "this line ${line_context} has ${ip}" | egrep "${ip}:${pool_tomcat_port}"

if [ $? -eq ];then

#将包含删除ip的行,替换为空行

sed -i ''${i}'s/.*'${ip}':'${pool_tomcat_port}'.*//ig' ${ngx_upstream_file}

#sed -i ''${i}'d' ${ngx_upstream_file}

echo -e "\033[36m delete ${pool_name} from ${ngx_upstream_file} where ip = ${ip}. \033[0m"

fi

done

done

#删除文件中的空行

sed -i '/^$/d' ${ngx_upstream_file}

echo -e "\033[31m ====删除完成如下:==== \033[0m"

}

#调用方法

if [ $# -eq ];then

case $ in

add)

check_pool_in_ngx_upstream;

show_pool_in_ngx_upstream;

add_pool_to_upstream;

show_pool_in_ngx_upstream;

;;

delete)

check_pool_in_ngx_upstream;

show_pool_in_ngx_upstream;

delete_ip_from_upstream;

show_pool_in_ngx_upstream;

;;

*)

$"Usage: {sh change_nginx_upstream_conf.sh add chat-frontier-web 10.10.13.194 8080 /etc/nginx/conf.d/upstream.conf|sh change_nginx_upstream_conf.sh add chat-frontier-web 10.10.13.194 8080 /etc/nginx/conf.d/upstream.conf}"

exit

esac

else

echo "variables count not eq 5.please check the usage."

fi

Linux下添加shell脚本使得nginx日志每天定时切割压缩

Linux下添加shell脚本使得nginx日志每天定时切割压缩一 简介 对于nginx的日志文件,特别是access日志,如果我们不做任何处理的话,最后这个文件将会变得非常庞大 这时,无论是出现异常 ...

shell脚本分析nginx日志

shell脚本分析nginx日志: name=`awk -F ',' '{print $13":"$32}' $file | awk -F ':' '{print $4}'`ech ...

Busybox shell脚本修改密码

/****************************************************************************** * Busybox shell脚本修改密 ...

Shell脚本创建Nginx的upstream及location配置文件

#!/bin/sh ##################################################### # Name: create_nginx_conf.sh # Versi ...

shell脚本编写nginx部署脚本

下面为shell脚本编写的nginx的安装及修改nginx.conf的脚本,脚本比较简单: #!/bin/bash function yum_install(){ yum install epel-r ...

shell脚本之nginx的安装

为了编写nginx自动部署的脚本而刚学习的shell脚本语言.写文章只是为了记录,有错误勿喷. 一.创建shell脚本程序        操作系统是Linux的 CentOS 7 版本. ...

利用shell脚本实现nginx 的logs日志分割

Nginx 是一个非常轻量的 Web 服务器,体积小.性能高.速度快等诸多优点.但不足的是也存在缺点,比如其产生的访问日志文件一直就是一个,不会自动地进行切割,如果访问量很大的话,将 导致日志文件容量 ...

shell脚本分析 nginx日志访问次数最多及最耗时的页面

当服务器压力比较大,跑起来很费力时候.我们经常做站点页面优化,会去查找那些页面访问次数比较多,而且比较费时. 找到那些访问次数高,并且比较耗时的地址,就行相关优化,会取得立竿见影的效果的. 下面是我在 ...

Mac下通过shell脚本修改properties文件

通过shell脚本替换属性文件中的某行记录 假设有如下属性文件 demo.properties user.name=test user.password=123456 ................ ...

随机推荐

JSON&period;stringify&lpar;&rpar;和JOSN&period;parse&lpar;&rpar;

JSON.stringify()跟JSON.parse() 终于把这两个方法搞清楚了!!! JSON.tringify():把一个json数据转化成JSON string JSON.stringify ...

一个Form中2个按钮,PHP后台如何判断提交的是哪一个按钮

方法一:

我理解的javascript单线程机制

废话不多说,我们先来看几个例子: 1. setTimeout( console.log(2); result:  2 1   2.   console.log(100 setTimeout( cons ...

Android中的webview的进度条

POJ 2208 Pyramids 欧拉四面体

给出边长,直接就可以求出体积咯 关于欧拉四面体公式的推导及证明过程 2010-08-16 14:18 1,建议x,y,z直角坐标系.设A.B.C少拿点的坐标分别为(a1,b1,c1),(a2,b2,c ...

Element类型和HTML元素获取

Mysql增量写入Hdfs(二) --Storm&plus;hdfs的流式处理

一. 概述 上一篇我们介绍了如何将数据从mysql抛到kafka,这次我们就专注于利用storm将数据写入到hdfs的过程,由于storm写入hdfs的可定制东西有些多,我们先不从kafka读取,而先 ...

NPOI设置单元格背景色

NPOI设置单元格背景色在网上有好多例子都是设置为NPOI内置的颜色值 但是想用rgb值来设置背景色,即:通过HSSFPalette类获取颜色值时会抛出异常:Could not Find free c ...

Python:Day13

id() 查看内存地址 和while循环一样,在for循环中也可以使用break和continue,两者效果一样. repr

&lbrack;转&rsqb;Python依赖打包发布详细

Python依赖打包发布详细   http://www.cnblogs.com/mywolrd/p/4756005.html 将Python脚本打包成可执行文件   Python是一个脚本语言,被解释 ...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值