1. 程式人生 > >Shell腳本創建Nginx的upstream及location配置文件

Shell腳本創建Nginx的upstream及location配置文件

path egrep cti 路徑 pass auth pri 文件內容 variables

#!/bin/sh
#####################################################
#    Name:                create_nginx_conf.sh
#    Version:            V1.0
#    Author:            運維菜鳥
#    Description:        創建nginx相關配置文件    
#    Create Date:        2017-07-04
#    Email:            
#####################################################

#
env.sh文件內容格式:10.10.2.6=basics-price-service; #function_name:要調用的方法 function_name=$1 #ns_type:項目環境名稱 ns_type=$2 #env_file:env.sh文件具體路徑 env_file_path=$3 #判斷env.sh是否存在 function check_env_file() { if [ -f ${env_file_path} ];then echo "the ${env_file_path} is exist." echo -e "\n\n" else
echo "the ${env_file_path} is not exist." exit 1 fi } #生成nginx的location段的配置文件 function create_location(){ for pool in `cat ${env_file_path} | cut -d "=" -f2 | cut -d ";" -f1 | sort | uniq`;do echo -e "location /${pool} {\n\tproxy_pass http://${ns_type}-${pool}/${pool};\n\tproxy_set_header Host \$host;\n\tproxy_set_header X-Real-IP \$remote_addr;\n\tproxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;\n\tbreak;\n}
" done } #生成nginx的upstream配置文件 function create_upstream_conf() { for pool in `cat ${env_file_path} | cut -d "=" -f2 | cut -d ";" -f1 | sort | uniq`;do ip_list=`cat ${env_file_path} | egrep "${pool};" | cut -d "=" -f1 | sort |uniq` #pool_port=`cat env.sh | egrep "${pool}=" | cut -d "=" -f3 | cut -d ";" -f1| sort | uniq` echo -e "upstream ${ns_type}-${pool} {" for ip in ${ip_list[*]};do echo -e "\tserver ${ip}:8080;" done echo "}" done } if [ $# -eq 3 ];then case $1 in location) check_env_file; create_location; ;; upstream) check_env_file; create_upstream_conf; ;; *) $"Usage: {sh create_nginx_conf.sh location hh-prod /data/env.sh|sh create_nginx_conf.sh upstream hh-prod /data/env.sh}" exit 3 esac else echo "variables count not eq 5.please check the usage." echo "Usage: {sh create_nginx_conf.sh location hh-prod /data/env.sh|sh create_nginx_conf.sh upstream hh-prod /data/env.sh}." fi

Shell腳本創建Nginx的upstream及location配置文件