#!/bin/SH###################################################### name:create_nginx_conf.SH# VERSION:V1.0# Author: OPS Rookie # Description: Creating Nginx-related Profiles # Create Date: .- --Geneva# Email: ######################################################Env. sh File Content format:10.10.2.6=basics-price-Service; #function_name: The method to invoke Function_name=$1#ns_type: Project Environment name Ns_type=$2#env_file:Env. sh file specific path Env_file_path=$3#判断env. SH is presentfunctionCheck_env_file () {if[-F ${env_file_path}]; Then Echo "The ${env_file_path} is exist." Echo-E"\ n" Else Echo "The ${env_file_path} is not exist."Exit1 fi} #生成nginx的location段的配置文件functioncreate_location () { forPoolinch`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配置文件functioncreate_upstream_conf () { forPoolinch`Cat${env_file_path} |Cut-D"="-f2 |Cut-D";"-f1 |Sort|Uniq`; Doip_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} {" forIpinch${ip_list[*]}; Do Echo-E"\tserver ${ip}:8080;" Done Echo "}" Done}if[$#-eq3]; Then Case$1 inchLocation ) 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}"Exit3 EsacElse 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 script to create nginx upstream and location configuration file