当前位置: 首页 > article >正文

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

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

#!/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


http://www.kler.cn/news/302828.html

相关文章:

  • 为什么np.arrange(0.97,3.0,0.01)最后一个值是3.0018,大于3
  • Qt-常用控件(2)-按钮类和显示类
  • [概率论] 随机变量的分布函数 (一)
  • Spring Boot项目更改项目名称
  • 亲测可用导航网站源码分享 – 幽络源
  • Rust的常量
  • 云微客AI文案编写,有手就能出“爆款”
  • 【全网唯一中文】bt回测框架中文文档,不是backtrader!是bt
  • win11 MySQL的坑
  • Machine Learning: A Probabilistic Perspective 机器学习:概率视角 PDF免费分享
  • 手机TF卡格式化后数据恢复:方法、挑战与预防措施
  • 【Hot100】LeetCode—62. 不同路径
  • Flask中的上下文(Context)
  • apache文件共享和访问控制
  • 深入理解 Milvus:新一代向量数据库的基础技术与实战指南
  • Linux系统上Oracle12C Release 2 (12.2)打补丁
  • 【Python机器学习】长短期记忆网络(LSTM)
  • 在 Debian 12 上安装中文五笔输入法
  • Zabbix企业级应用案列
  • C#学习笔记 .NET Core使用注意事项
  • 基于相亲交友系统的高效匹配算法研究
  • 快速排序(分治思想)
  • USB 3.1 标准 A 型连接器及其引脚分配
  • Leetcode Hot 100刷题记录 -Day10(合并区间)
  • druid连接gbase8s数据库报错空指针
  • vue2 组件通信
  • MySql Index索引使用注意
  • 数据分析-13-时间序列异常值检测的类型及常见的检测方法
  • 专题三_二分查找算法_算法详细总结
  • Jmeter之beanshell使用