nginx_shell脚本扩展配置虚拟主机三种方式

#需要注意的是

代理proxy_pass http://192.168.29.188:5000; 我这里使用的是容器中的flask地址

bash 复制代码
#!/bin/bash

# 1. 查看当前系统 nginx 配置文件位置
nginxconf_path=$(whereis nginx | awk '{print $2}')/conf/nginx.conf
echo "nginxconf_path: $nginxconf_path"

# 2. 查看主配置文件是否有导入扩展配置文件的指令
if grep -q "include /usr/local/nginx/conf/conf.d/*.conf;" $nginxconf_path; then
    echo "Import extended configuration: /usr/local/nginx/conf/conf.d/*.conf"
else
    echo "No import extended configuration"
    # 2.1. 若没有,则需要在主配置文件中添加 include /usr/local/nginx/conf/conf.d/*.conf;
    sudo sed -i '/http {/a include /usr/local/nginx/conf/conf.d/*.conf;' $nginxconf_path
    echo "Added import extended configuration: /usr/local/nginx/conf/conf.d/*.conf"
fi

# 3. 查看扩展配置文件目录是否存在
if [ -d "/usr/local/nginx/conf/conf.d" ]; then
    echo "Extended configuration directory exists"
else
    echo "Extended configuration directory does not exist"
    # 3.1. 若不存在,则需要创建扩展配置文件目录
    sudo mkdir -p /usr/local/nginx/conf/conf.d
    echo "Created extended configuration directory"
fi

# 4. 定义配置文件名数组
conf_files=("domain.conf" "ip.conf" "port.conf")

# 5. 检查并创建配置文件
for file in "${conf_files[@]}"; do
    if [ -f "/usr/local/nginx/conf/conf.d/$file" ]; then
        echo "Extended configuration file exists: $file"
    else
        echo "Extended configuration file does not exist: $file"
        # 5.1. 若不存在,则需要创建扩展配置文件
        echo "Creating extended configuration file: $file"
        sudo touch "/usr/local/nginx/conf/conf.d/$file"
    fi
done

# 6. 定义函数来写入配置文件
write_config() {
    local file_path=$1
    local server_name=$2
    local listen_port=$3

    cat <<EOF | sudo tee "$file_path" > /dev/null
server {
    listen $listen_port;
    server_name $server_name;
    location / {
        # 代理转发到我的docker中的flask项目
        proxy_pass http://192.168.29.188:5000;
    }
}
EOF
}

# 7. 交互式选择配置
echo "Select virtual host configuration type:"
select conf_type in "IP" "Domain" "Port" "Quit"; do
    echo "You selected: $conf_type"  # 调试输出
    case "$conf_type" in
        "Domain") 
            write_config "/usr/local/nginx/conf/conf.d/${conf_files[0]}" "example.com" 80
            break
            ;;
        "IP") 
            write_config "/usr/local/nginx/conf/conf.d/${conf_files[1]}" "192.168.29.129" 80
            break
            ;;
        "Port") 
            write_config "/usr/local/nginx/conf/conf.d/${conf_files[2]}" "example.com" 9888
            break
            ;;
        "Quit") 
            exit 0
            ;;
        *) 
            echo "Invalid input. Please try again."
            ;;
    esac
done

# 8. 重启 nginx 服务并检查是否成功
if sudo systemctl restart nginx; then
    echo "Nginx service restarted successfully"
else
    echo "Failed to restart Nginx service"
    exit 1
fi
相关推荐
不惑_3 分钟前
Logstash 安装与部署(无坑版)
运维·jenkins
自律的kkk13 分钟前
docker配置镜像加速器
运维·docker·容器
繁依Fanyi42 分钟前
828 华为云征文|华为 Flexus 云服务器部署 RustDesk Server,打造自己的远程桌面服务器
运维·服务器·开发语言·人工智能·pytorch·华为·华为云
优思学院1 小时前
优思学院|如何从零开始自己学习六西格玛?
大数据·运维·服务器·学习·六西格玛黑带·cssbb
Flying_Fish_roe1 小时前
linux-软件包管理-包管理工具(RedHat/CentOS 系)
linux·运维·centos
千寻简1 小时前
Cursor免费 GPT-4 IDE 工具的保姆级使用教程
java·运维·ide·ai
ps酷教程1 小时前
nginx进阶篇(二)
nginx
双普拉斯1 小时前
微信小程序点赞动画特效实现
nginx·微信小程序·notepad++
陈小唬2 小时前
云服务器docker中Hbase整合java-api需要放行的接口
服务器·docker·hbase
苏少朋2 小时前
Docker安装 ▎Docker详细讲解 ▎数据卷挂载 ▎Nginx安装理解
linux·nginx·docker·容器