Files
tongxunruanjian/scripts/switch-to-host-nginx.sh
T
总工andmultica-agent c9e8e113d0 fix(deploy): 复核修正——补齐 /downloads/ APK 路由并加固切换脚本 (HEL-552)
- host-nginx/jxd.jinniu.ink.conf:线上 jxd-edge 实际配置里有 /downloads/ 路由
  (HEL-279 验收后现场热修,仓库未同步),新模板漏迁会导致已交付验收链接
  /downloads/jinxunda-android-test-1.0.9.apk 失效。补齐该路由,占位符
  __JXD_DOWNLOADS_DIR__ 由切换脚本替换;另加 proxy_cache off 防止宝塔
  http{} 级全局 proxy_cache 缓存 API/WebSocket 响应;http2 改用独立指令。
- docker-compose.edge-fallback.yaml:补 downloads 只读挂载,保证回滚后下载可用。
- deploy/public/nginx.conf:与线上运行配置逐字节对齐(已 diff 验证一致)。
- switch-to-host-nginx.sh:裸 pgrep -x nginx 会被宿主机上其他 nginx 容器
  (solsum 首页容器)误报,导致对已停止的宝塔 Nginx 执行 reload 必然失败
  且不触发回滚、80/443 无人监听。改为按 pidfile 判定系统 Nginx 本尊,
  start/reload 失败立即自动回滚。
- README.md:修正 APK 下载现状描述(原文称线上无下载路由,与实际不符)。

Co-authored-by: multica-agent <github@multica.ai>
2026-09-14 17:22:42 +08:00

105 lines
4.3 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# HEL-551:把公网 80/443 从 jxd-edge(host 网络容器)切换到系统/宝塔 Nginx。
# 由总工在服务器上执行;施工员本卡不跑本脚本,也不 SSH 改现网。
#
# 用法: ./scripts/switch-to-host-nginx.sh
# 失败时自动回滚到 jxd-edge;也可以随时手动跑 ./scripts/rollback-to-jxd-edge.sh。
#
# 执行前可以按实际环境覆盖这几个变量,默认按宝塔面板的常见路径:
# HOST_NGINX_VHOST_DIR 站点配置目录(宝塔: /www/server/panel/vhost/nginx)
# HOST_NGINX_BIN nginx 可执行文件,用来做 -t 语法检查
# HOST_NGINX_INIT initscript,用来启动/重载(宝塔一般装了 /etc/init.d/nginx)
# HOST_NGINX_PIDFILE 系统 Nginx 的 pid 文件(宝塔: /www/server/nginx/logs/nginx.pid)
set -euo pipefail
cd "$(dirname "$0")/.."
REPO_DIR="$(pwd)"
SITE_CONF_SRC="deploy/public/host-nginx/jxd.jinniu.ink.conf"
SITE_CONF_NAME="jxd.jinniu.ink.conf"
ACME_WEBROOT="${REPO_DIR}/deploy/public/acme"
DOWNLOADS_DIR="${REPO_DIR}/deploy/public/downloads"
HOST_NGINX_VHOST_DIR="${HOST_NGINX_VHOST_DIR:-/www/server/panel/vhost/nginx}"
HOST_NGINX_BIN="${HOST_NGINX_BIN:-/www/server/nginx/sbin/nginx}"
HOST_NGINX_INIT="${HOST_NGINX_INIT:-/etc/init.d/nginx}"
HOST_NGINX_PIDFILE="${HOST_NGINX_PIDFILE:-/www/server/nginx/logs/nginx.pid}"
# 判断"系统 Nginx 本尊"是否在跑,只认 pidfile 里的进程。
# 不能用裸 pgrep -x nginx:宿主机上还有别的 nginx 容器(如 solsum 首页容器的
# nginx -g daemon off),它们会让 pgrep 误报"系统 Nginx 已在跑",导致对已停止的
# 系统 Nginx 执行 reload(必然失败),80/443 陷入无人监听。
nginx_running() {
[ -f "$HOST_NGINX_PIDFILE" ] && kill -0 "$(cat "$HOST_NGINX_PIDFILE" 2>/dev/null)" 2>/dev/null
}
BACKUP_DIR="/root/jxd-nginx-switch-backup-$(date +%Y%m%d%H%M%S)"
rollback() {
echo "!! 切换失败,回滚到 jxd-edge" >&2
./scripts/rollback-to-jxd-edge.sh || true
exit 1
}
echo "== 1/5 备份系统 Nginx 站点目录、金讯达 compose、证书续期引用 =="
mkdir -p "$BACKUP_DIR"
if [ -d "$HOST_NGINX_VHOST_DIR" ]; then
cp -a "$HOST_NGINX_VHOST_DIR" "$BACKUP_DIR/vhost-nginx"
fi
cp -a docker-compose.yaml deploy/public/docker-compose.public.yaml "$BACKUP_DIR/"
if [ -f "/etc/letsencrypt/renewal/jxd.jinniu.ink.conf" ]; then
mkdir -p "$BACKUP_DIR/letsencrypt-renewal"
cp -a /etc/letsencrypt/renewal/jxd.jinniu.ink.conf "$BACKUP_DIR/letsencrypt-renewal/"
fi
echo "备份完成: $BACKUP_DIR"
echo "== 2/5 生成站点配置(替换 ACME webroot / downloads 占位符)=="
mkdir -p "$ACME_WEBROOT"
TMP_CONF="$(mktemp)"
sed -e "s#__JXD_ACME_WEBROOT__#${ACME_WEBROOT}#g" \
-e "s#__JXD_DOWNLOADS_DIR__#${DOWNLOADS_DIR}#g" \
"$SITE_CONF_SRC" > "$TMP_CONF"
if [ ! -d "$HOST_NGINX_VHOST_DIR" ]; then
echo "站点目录 $HOST_NGINX_VHOST_DIR 不存在,请先用 HOST_NGINX_VHOST_DIR= 指定正确路径" >&2
rm -f "$TMP_CONF"
exit 1
fi
cp "$TMP_CONF" "${HOST_NGINX_VHOST_DIR}/${SITE_CONF_NAME}"
rm -f "$TMP_CONF"
echo "== 3/5 校验系统 Nginx 配置语法(先检查,不合格立即退出,不动服务)=="
if ! "$HOST_NGINX_BIN" -t; then
echo "系统 Nginx 配置检查失败,移除新站点文件,不触碰任何服务" >&2
rm -f "${HOST_NGINX_VHOST_DIR}/${SITE_CONF_NAME}"
exit 1
fi
echo "== 4/5 停 jxd-edge,启动/重载系统 Nginx =="
docker compose -f docker-compose.yaml -f deploy/public/docker-compose.public.yaml \
-f deploy/public/docker-compose.edge-fallback.yaml stop edge 2>/dev/null || true
docker compose -f docker-compose.yaml -f deploy/public/docker-compose.public.yaml \
-f deploy/public/docker-compose.edge-fallback.yaml rm -f edge 2>/dev/null || true
if nginx_running; then
"$HOST_NGINX_INIT" reload || systemctl reload nginx || rollback
else
"$HOST_NGINX_INIT" start || systemctl start nginx || rollback
fi
sleep 1
if ! nginx_running; then
rollback
fi
echo "== 5/5 健康检查 =="
sleep 1
CODE="$(curl -sk -o /dev/null -w '%{http_code}' --max-time 5 https://jxd.jinniu.ink/ || echo 000)"
if [ "$CODE" = "000" ]; then
echo "https://jxd.jinniu.ink/ 无响应(code=$CODE)" >&2
rollback
fi
echo "切换完成。系统 Nginx 已独占 80/443,jxd-edge 已下线。"
echo "备份目录: $BACKUP_DIR"
echo "如需回滚: ./scripts/rollback-to-jxd-edge.sh"