nginxconf 自动生成 nginx tcp 转发配置文件 conf

2024-03-01 21:54:21 浏览数 (1)

代码语言:shell复制
#!/bin/bash
# nginx 代理自动生成,开发:eisc.cn

configDIR(){

    if [ `id -un` != "root" ]
    then
        sudoc="sudo" 
    else
        sudoc=""
    fi

    wwwconf=/datadisk/eisc/wwwconf/
    $sudoc mkdir -p $wwwconf ; $sudoc rm -rf $wwwconf/*


    xiangmuName=(
        www   work   sou   enchantment  guzheng  zhoubao  gcc  xibin
    )

    peizhi="
eisc.cn

10.1.1.3

62000
"


}

create_www_conf(){

  echo "案例:   eisc.cn 0.0.0.0 62000"
#  read -p " 请输入三个参数:" peizhi

    buff=( $peizhi )

    yuming=${buff[0]}
    # 前端域名,增加到nginx 消息头部
    ip=${buff[1]}
    # 后端服务器ip
    portmin=${buff[2]}
    portmin=$[portmin-1]
    # 设置最小端口,后面网站按照顺序自动加1

    declare -A port
      port["www"]="62010"
      port["work"]="62011"
      port["sou"]="62012"
      port["enchantment"]="62013"
      port["guzheng"]="62014"
      port["zhoubao"]="62015"
      port["gcc"]="62016"
      port["xibin"]="62100"

    cd $wwwconf; sudo chmod 777 -R $wwwconf $wwwconf/*

    if [ ! -e eiscwwwconf.tar.gz ]
    then
      $sudoc tar -czvf eiscwwwconf.tar.gz *
      $sudoc rm -rf `ls | grep -v eiscwwwconf.tar.gz`
    fi

    $sudoc rm -rf `ls | grep -v eiscwwwconf.tar.gz`

   # file=(`ls $www | grep -vE "ssl|wwwconf|wwwroot|*.sh"`)
   file=${xiangmuName[*]}

    for i in ${file[*]}
        do
            ((a  ))
            b=$[portmin a] ; portlist[$c]=$b ; ((c  ))
            # 打印一次变量,接收一个元素存入数组: portlist
            if [ ! -e $www/$i/cgi-bin ]
            then
            sudo mkdir $www/$i/cgi-bin
            fi

            case $i in
              "www")          b=${port[www]}           ; echo "#------- www 被手动指定固定端口  $b -------#" ;;
              "work")         b=${port[work]}          ; echo "#------- work 被手动指定固定端口  $b -------#" ;;
              "sou")          b=${port[sou]}           ; echo "#------- sou 被手动指定固定端口  $b -------#";;
              "enchantment")  b=${port[enchantment]}   ; echo "#------- enchantment 被手动指定固定端口  $b -------#";;
              "guzheng")      b=${port[guzheng]}       ; echo "#------- guzheng 被手动指定固定端口  $b -------#";;
              "zhoubao")      b=${port[zhoubao]}       ; echo "#------- zhoubao 被手动指定固定端口  $b -------#";;
              "gcc")          b=${port[gcc]}           ; echo "#------- zhoubao 被手动指定固定端口  $b -------#";;
              "xibin")        b=${port[xibin]}         ; echo "#------- xibin 被手动指定固定端口  $b -------#";;
            esac

            echo "  $i 创建web网站 的端口:   $b 前端域名    $i.$yuming    后端ip port   $ip:$b     c   cgi 解析目录: $i/cgi-bin"
            echo "  $i 创建web网站 的端口:   $b 前端域名    $i.$yuming    后端ip port   $ip:$b     c   cgi 解析目录: $i/cgi-bin" >> log_www_conf.txt

#---------  生成前端转发 子站点文件  -----------#
            $sudoc mkdir -p $wwwconf/nginxzhuanfa ; $sudoc chmod 777 $wwwconf/nginxzhuanfa
            echo "
            server{
            listen 80;
            server_name $i.$yuming;
            location / {
                proxy_pass http://$ip:$b;
                #---- 记录 IP 地址 ----#
                #proxy_redirect off;
                #proxy_set_header Host $http_host;
                #proxy_set_header X-Real-IP $remote_addr;     
                #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              }
            }
            " > $wwwconf/nginxzhuanfa/$i-$b.conf

            if [ "$i" = "www" ]
            then
                echo "
                  server{
                  listen 80;
                  server_name $i.$yuming $yuming;
                  location / {
                      proxy_pass http://$ip:$b;
                      #---- 记录 IP 地址 ----#
                      proxy_redirect off;
                      proxy_set_header Host $http_host;
                      proxy_set_header X-Real-IP $remote_addr;	    
                      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    }
                  }
                " > $wwwconf/nginxzhuanfa/$i-$b.conf
            fi


            if [ "$i" = "gcc" ]
            then
                echo "
                  server{
                  listen 80;
                  server_name www.okgcc.cn okgcc.cn;
                  location / {
                      proxy_pass http://$ip:$b;
                      #---- 记录 IP 地址 ----#
                      proxy_redirect off;
                      proxy_set_header Host $http_host;
                      proxy_set_header X-Real-IP $remote_addr;     
                      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    }
                  }
                " > $wwwconf/nginxzhuanfa/$i-$b.conf
            fi



#---------- 端口对端口 ----------#

            $sudoc mkdir -p $wwwconf/portconf ; $sudoc chmod 777 $wwwconf/portconf
            echo "
            server{
            listen $b;
            location / {
                proxy_pass http://$ip:$b;
                #---- 记录 IP 地址 ----#
                proxy_redirect off;
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;     
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              }
            }
            " > $wwwconf/portconf/$i-$b.conf

            if [ "$i" = "www" ]
            then
                echo "
                  server{
                  listen $b;
                  location / {
                      proxy_pass http://$ip:$b;

                    }
                  }
                " > $wwwconf/portconf/$i-$b.conf
            fi


            if [ "$i" = "gcc" ]
            then
                echo "
                  server{
                  listen $b;
                  location / {
                      proxy_pass http://$ip:$b;
                    }
                  }
                " > $wwwconf/portconf/$i-$b.conf
            fi



#---------- 后端转发子站点 ----------#

            echo "
      server {
        listen       $b;
        root   $www/$i;
        index index.php index.html index.htm index;

        add_header 'yuming is ' '$i.$yuming';
        add_header 'ipport is ' '$ip:$b';

        location ~ .php {

#                fastcgi_pass   unix:/var/run/php-fpm811.sock;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }

        location /cgi-bin {
            fastcgi_pass  unix:/var/run/fcgiwrap.socket;
            include fastcgi.conf;
        }
      }
           " > $wwwconf/$b.$i.conf
        done

        echo "[ok]创建网站日志存放在:   $wwwconf/log_www_conf.txt"
        echo "[ok]前端转发 nginx 子文件目录: $wwwconf/nginxzhuanfa"
        sudo nginx -s reload
}


main()
{
   configDIR
   create_www_conf
}

main

0 人点赞