From f8da9ff8f71e8a734a6929eebbd06d2b41d57046 Mon Sep 17 00:00:00 2001 From: milky0217 Date: Tue, 2 Jun 2026 12:13:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20switch-env.sh=E4=BF=AE=E5=A4=8D=E8=B7=AF?= =?UTF-8?q?=E5=BE=84/=E5=8D=8F=E8=AE=AE/=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E5=90=8D+=E6=B7=BB=E5=8A=A0SSH=5FHOST=E7=A1=AC=E7=BC=96?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/switch-env.sh | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/scripts/switch-env.sh b/scripts/switch-env.sh index 20e1ba7..a33cdd2 100755 --- a/scripts/switch-env.sh +++ b/scripts/switch-env.sh @@ -12,10 +12,12 @@ PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" ENV_FILE="$PROJECT_ROOT/.env" # ---------- 配置 ---------- -SSH_HOST="root@1panel-server" -PROXY_DIR="/www/sites/xmclassmate.top/proxy" +# 1Panel 管理的 OpenResty 容器的实际配置文件路径 +BASE_DIR="/opt/1panel/apps/openresty/openresty" +PROXY_DIR="${BASE_DIR}/www/sites/xmclassmate.top/proxy" ROOT_CONF="${PROXY_DIR}/root.conf" NGINX_CONTAINER="1Panel-openresty-ABu5" +SSH_HOST="root@1panel-server" # ---------- 颜色输出 ---------- RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m' @@ -62,11 +64,11 @@ show_status() { echo " 绿色 :4434 $( [ "$active" = "green" ] && echo '★ 活动中' || echo '○ 待命中' )" echo "" echo " 反向代理: xmclassmate.top" - echo " 数据库: milkydata_dev" + echo " 数据库: milkydata (blue/green) / milkydata_dev (dev)" echo "" # 检查两个端口是否可达 - local blue_ok; blue_ok=$(ssh "$SSH_HOST" "curl -s -o /dev/null -w '%{http_code}' --max-time 3 https://127.0.0.1:4433/health -k" 2>/dev/null || echo "000") + local blue_ok; blue_ok=$(ssh "$SSH_HOST" "curl -s -o /dev/null -w '%{http_code}' --max-time 3 https://127.0.0.1:4433/health -k" 2>/dev/null || echo "000") local green_ok; green_ok=$(ssh "$SSH_HOST" "curl -s -o /dev/null -w '%{http_code}' --max-time 3 http://127.0.0.1:4434/health" 2>/dev/null || echo "000") echo " 蓝色健康: HTTP $blue_ok" echo " 绿色健康: HTTP $green_ok" @@ -92,16 +94,19 @@ switch_to() { # 先验证目标环境健康 echo "验证目标环境健康..." - local target_ok; target_ok=$(ssh "$SSH_HOST" "curl -s -o /dev/null -w '%{http_code}' --max-time 3 http://127.0.0.1:${target_port}/health" 2>/dev/null || echo "000") + local proto="http"; [ "$target_port" = "4433" ] && proto="https" + local extra=""; [ "$proto" = "https" ] && extra="-k" + local target_ok; target_ok=$(ssh "$SSH_HOST" "curl -s -o /dev/null -w '%{http_code}' --max-time 3 ${proto}://127.0.0.1:${target_port}/health ${extra}" 2>/dev/null || echo "000") if [ "$target_ok" != "200" ]; then error "目标环境 :${target_port} 健康检查失败 (HTTP $target_ok),取消切换" exit 1 fi info "目标环境健康 ✅" - # 修改 proxy_pass - info "修改 OpenResty proxy_pass → :${target_port}..." - ssh "$SSH_HOST" "docker exec ${NGINX_CONTAINER} sh -c \"sed -i 's|proxy_pass http://127.0.0.1:[0-9]*|proxy_pass http://127.0.0.1:${target_port}|' ${ROOT_CONF}\"" 2>&1 || { + # 修改 proxy_pass(根据端口自动选择 http/https) + local target_proto="http"; [ "$target_port" = "4433" ] && target_proto="https" + info "修改 OpenResty proxy_pass → ${target_proto}://127.0.0.1:${target_port}..." + ssh "$SSH_HOST" "docker exec ${NGINX_CONTAINER} sh -c \"sed -i 's|proxy_pass https\?://127.0.0.1:[0-9]*|proxy_pass ${target_proto}://127.0.0.1:${target_port}|' ${ROOT_CONF}\"" 2>&1 || { error "修改 proxy_pass 失败" exit 1 }