From 722f08a1597d22d9e3216427018fdb59b0cefb90 Mon Sep 17 00:00:00 2001 From: zhangxiang Date: Thu, 8 Oct 2026 17:41:35 +0800 Subject: [PATCH] =?UTF-8?q?fix(deploy):=20=E4=B8=8A=E4=BC=A0=E5=89=8D?= =?UTF-8?q?=E7=94=9F=E6=88=90=20nginx=20web=20root=20=E9=95=9C=E5=83=8F?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E6=8A=8A=E9=83=A8=E7=BD=B2=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E5=8D=87=E7=BA=A7=E4=B8=BA=E8=B5=84=E6=BA=90=E7=BA=A7=E6=96=AD?= =?UTF-8?q?=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 2026-10-08 生产事故:deploy.sh 打出「🎉 部署成功」而线上 CSS/JS/图标全 404。 根因是 nginx 站点根就是上传后的 dist/(nginx-static-production.conf:132 的 root /var/www/novalon + :136-142 的 try_files $uri =404),要求磁盘上存在 dist/_next/static/**;而 next build 出的是 dist/static/**,postbuild 只补 standalone 容器形态。历史镜像由旧流程产出,被本次 rsync --delete 抹掉。 - 步骤 1.5:上传前本地生成镜像(dist/static → dist/_next/static、public/. → dist/), dist/static 缺失即终止部署而非静默继续 - verify_remote:从首页 HTML 实际引用的 URL 取前 5 个 /_next/static/*.{js,css} 加 /favicon.ico 逐个断言 200,任一非 200 即 return 1。负对照实测:换成不存在 的取样 URL ⇒ RED 404 且 failed=1,断言可失败 - 只查五个 HTML 路径的门禁,在破站状态下会判绿——这是本次事故的直接放大原因 --- scripts/deploy.sh | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index b2a971b..d3ee319 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -117,6 +117,34 @@ verify_remote() { fi done + # 资源级断言:只查 HTML 页会把「CSS/JS 全 404 的破站」判成部署成功 + # (2026-10-08 事故:nginx web root 镜像缺失,5 个页面全 200 而站点无样式)。 + log "" + log "📋 静态资源断言(从首页 HTML 实际引用的 URL 取样)" + local html assets asset status checked=0 + html=$(curl -fsSL --max-time 15 "${DOMAIN}/" 2>/dev/null || echo "") + if [[ -z "$html" ]]; then + log " ❌ 首页 HTML 取回为空,无法取样资源" + failed=1 + else + assets=$(printf '%s\n' "$html" | grep -o '/_next/static/[^"]*\.\(js\|css\)' | sort -u | head -5) + if [[ -z "$assets" ]]; then + log " ❌ 首页 HTML 里没出现 /_next/static 引用(构建异常)" + failed=1 + fi + for asset in $assets "/favicon.ico"; do + checked=$((checked + 1)) + status=$(curl -s -o /dev/null -w '%{http_code}' --max-time 15 "${DOMAIN}${asset}" 2>/dev/null || echo "000") + if [[ "$status" == "200" ]]; then + log " ✅ ${asset} ($status)" + else + log " ❌ ${asset} ($status)" + failed=1 + fi + done + log " 取样数: $checked" + fi + if [[ "$failed" == "1" ]]; then log "❌ 生产环境验证失败" return 1 @@ -201,6 +229,23 @@ cmd_deploy() { DIST_SIZE=$(du -sh "$DIST_DIR" | cut -f1) log "✅ $DIST_DIR 目录存在,大小: $DIST_SIZE" + # 步骤 1.5: 生成 nginx web root 镜像 + # 远端 nginx 的 root 就是本脚本上传的 dist/(nginx-static-production.conf:132,136-142 的 + # location /_next/static/ 用 try_files $uri =404),而 next build 的布局是 + # dist/static/**(无 _next 前缀)+ 独立的 public/**。不同步这份镜像,rsync --delete 就会 + # 抹掉上一次部署留下的镜像 ⇒ HTML 全 200 而 CSS/JS/图标全 404(2026-10-08 线上事故根因)。 + log "" + log "📋 步骤 1.5: 生成 nginx web root 镜像 (dist/_next/static + public → dist 根)" + if [[ ! -d "$DIST_DIR/static" ]]; then + log "❌ $DIST_DIR/static 不存在,产物未装配,终止部署" + exit 1 + fi + rm -rf "$DIST_DIR/_next/static" + mkdir -p "$DIST_DIR/_next" + cp -R "$DIST_DIR/static" "$DIST_DIR/_next/static" + [[ -d "$PROJECT_ROOT/public" ]] && cp -R "$PROJECT_ROOT/public/." "$DIST_DIR/" + log "✅ 镜像完成: _next=$(du -sh "$DIST_DIR/_next" | cut -f1) dist=$(du -sh "$DIST_DIR" | cut -f1)" + # 步骤 2: 验证 SSH 连接 log "" log "📋 步骤 2: 验证 SSH 连接"