fix(deploy): ???????? Nginx ???????

Made-with: Cursor
This commit is contained in:
whm
2026-03-27 12:04:22 +08:00
parent 0980d1fa9c
commit 89cc1d2368
4 changed files with 44 additions and 34 deletions

View File

@@ -85,11 +85,12 @@ sudo systemctl start nginx
逻辑由 **`scripts/lib-yh-compose-deploy.sh`** 统一处理(无需单独启动脚本):
1. **启动前**`docker compose … down --remove-orphans`,只停本项目容器,**不**宿主机 `nginx``start/stop`
2. **`systemctl` 显示宿主机 Nginx 已运行**:只起 `mongo api web admin`**不起**容器 `yh_nginx`**`nginx/yuheng.host.conf`** 生成 `/etc/nginx/conf.d/<域名>.conf` **`nginx -t` + `reload`**(不重载则无法指向新回环端口)
3. **宿主机 Nginx 未运行**起完整栈(**`--profile compose-internal-nginx`**,含容器 `yh_nginx` 终止 TLS
4. **证书**同上,`/etc/ssl/yh_web/yuheng.yuxindazhineng.com/``pull-and-restart.sh` / `restart.sh` 仍会同步仓库内证书到该目录
1. **启动前**`docker compose … down --remove-orphans`,只停本项目容器,**不停止**宿主机 `nginx`
2. **写入宿主机站点配置****`nginx/yuheng.host.conf`** 生成 `/etc/nginx/conf.d/<域名>.conf`,并执行 **`nginx -t`**
3. **检测宿主机 Nginx**若在线则跳过;若不在线则执行 `systemctl start nginx``enable`
4. **启动容器**只起 `mongo api web admin`(不再启动容器 `yh_nginx`
5. **证书**:同上,`/etc/ssl/yh_web/yuheng.yuxindazhineng.com/``pull-and-restart.sh` / `restart.sh` 仍会同步仓库内证书到该目录。
**回环端口**(与 `nginx/yuheng.host.conf` 一致API `127.0.0.1:8088`,前台 `9080`,后台 `9081`
**与「方式 B8443**:方式 B 是宿主机 → 容器 Nginx → 各服务;本节是宿主机 → 各服务,**一层**
**说明**:不再使用容器 `yh_nginx` 作为入口,统一为宿主机 Nginx 单入口方案

View File

@@ -356,16 +356,16 @@ elif [ -f "$ROOT/nginx/$NGINX_DOMAIN/fullchain.pem" ] && [ -f "$ROOT/nginx/$NGIN
fi
# shellcheck disable=SC1091
. "$ROOT/scripts/lib-yh-compose-deploy.sh"
# 先停掉本项目 Compose 栈(不操作宿主机 nginx 服务);再按宿主机 Nginx 是否在线拉起容器
# 先停掉本项目容器(不停止宿主机 nginx→ 准备宿主机站点并确保 nginx 在线 → 启动业务容器
yh_compose_down
echo ""
echo "[3/3] 宿主机 Nginx 站点与服务..."
yh_install_host_nginx_site_conf
ensure_host_nginx_started
yh_compose_up
# 可选web/promotion/视频发布 -> data/uploads + MongoDB须 server/.env 中 YH_IMPORT_PROMOTION_SITE_ID
bash "$ROOT/scripts/run-promotion-import-on-deploy.sh" "$ROOT"
echo ""
echo "[3/3] 宿主机 Nginx 站点(仅当 systemctl 显示 Nginx 已运行时写入 yuheng.host.conf 并重载,不执行 start/stop..."
yh_install_host_nginx_site_conf
echo ""
echo "完成. 对外仅 443反代: https://$NGINX_DOMAIN"

View File

@@ -254,11 +254,11 @@ fi
# shellcheck disable=SC1091
. "$ROOT/scripts/lib-yh-compose-deploy.sh"
yh_compose_down
echo "宿主机 Nginx 站点与服务..."
yh_install_host_nginx_site_conf
ensure_host_nginx_started
yh_compose_up
bash "$ROOT/scripts/run-promotion-import-on-deploy.sh" "$ROOT"
echo "宿主机 Nginx 站点(若 Nginx 已运行则更新并重载)..."
yh_install_host_nginx_site_conf
echo "完成. 对外仅 443反代: https://$NGINX_DOMAIN"

View File

@@ -1,10 +1,9 @@
# shellcheck shell=bash
# 由 pull-and-restart.sh / restart.sh 在定义好 ROOT、compose_cmd、run_sudo 之后 source。
# 依赖:项目根存在 docker-compose.host-nginx.yml与主 compose 合并使用)
# 统一策略:仅使用宿主机 Nginx容器 yh_nginx 不再作为入口
YH_COMPOSE_FILES="-f docker-compose.yml -f docker-compose.host-nginx.yml"
# 宿主机 Nginxsystemd是否在运行不检测 443 归属,以免误判。
host_nginx_online() {
command -v systemctl >/dev/null 2>&1 || return 1
systemctl is-active nginx >/dev/null 2>&1 && return 0
@@ -12,7 +11,23 @@ host_nginx_online() {
return 1
}
# 停止本项目的 Compose 栈(含 yh_nginx 等),不操作宿主机 Nginx 服务。
ensure_host_nginx_started() {
if host_nginx_online; then
echo "宿主机 Nginx 在线,跳过启动。"
return 0
fi
echo "宿主机 Nginx 未在线,尝试启动..."
run_sudo systemctl start nginx 2>/dev/null || run_sudo systemctl start nginx.service
run_sudo systemctl enable nginx 2>/dev/null || true
if host_nginx_online; then
echo "宿主机 Nginx 启动成功。"
return 0
fi
echo "错误: 无法启动宿主机 Nginx请检查 systemctl status nginx" >&2
exit 1
}
# 停止本项目 Compose 栈(包含可能残留的 yh_nginx不停止宿主机 Nginx。
yh_compose_down() {
if [ ! -f "$ROOT/docker-compose.host-nginx.yml" ]; then
compose_cmd down --remove-orphans 2>/dev/null || true
@@ -21,33 +36,22 @@ yh_compose_down() {
compose_cmd $YH_COMPOSE_FILES down --remove-orphans 2>/dev/null || true
}
# 按宿主机 Nginx 是否在线,选择仅起业务容器或起完整栈(含 yh_nginx
# 仅启动业务容器,不再启动容器 yh_nginx。
yh_compose_up() {
if [ ! -f "$ROOT/docker-compose.host-nginx.yml" ]; then
echo "未找到 docker-compose.host-nginx.yml使用默认 compose up."
compose_cmd up -d --force-recreate
echo "未找到 docker-compose.host-nginx.yml使用默认 compose 启动业务容器。"
compose_cmd up -d --force-recreate mongo api web admin
return 0
fi
if host_nginx_online; then
echo "检测到宿主机 Nginx 已运行:不启动容器 yh_nginxmongo/api/web/admin 绑定本机回环(见 nginx/yuheng.host.conf。"
compose_cmd $YH_COMPOSE_FILES up -d --force-recreate mongo api web admin
else
echo "未检测到宿主机 Nginx 运行:启动完整栈(含容器 yh_nginx 终止 TLS。"
compose_cmd $YH_COMPOSE_FILES --profile compose-internal-nginx up -d --force-recreate
fi
}
# 仅当宿主机 Nginx 在线时:从模板写入站点配置并重载(不执行 systemctl start/stop nginx)。
# 从模板生成宿主机站点配置,并在配置检查通过后 reload若离线则 start)。
yh_install_host_nginx_site_conf() {
local domain="${NGINX_DOMAIN:-yuheng.yuxindazhineng.com}"
local tpl="$ROOT/nginx/yuheng.host.conf"
local out="/etc/nginx/conf.d/${domain}.conf"
host_nginx_online || {
echo "宿主机 Nginx 未运行,跳过写入站点配置(由容器 yh_nginx 对外)。"
return 0
}
if [ ! -f "$tpl" ]; then
echo "未找到 $tpl,跳过宿主机站点配置生成。"
return 0
@@ -56,9 +60,14 @@ yh_install_host_nginx_site_conf() {
mkdir -p "$ROOT/verify-root"
sed "s|__VERIFY_ROOT__|$ROOT/verify-root|g" "$tpl" | run_sudo tee "$out" >/dev/null
if run_sudo nginx -t 2>/dev/null; then
if ! run_sudo nginx -t 2>/dev/null; then
echo "错误: 宿主机 nginx -t 失败,请检查 $out" >&2
exit 1
fi
if host_nginx_online; then
run_sudo systemctl reload nginx 2>/dev/null && echo "宿主机 Nginx 已重载($out)。" || true
else
echo "警告: 宿主机 nginx -t 失败,请检查 $out" >&2
ensure_host_nginx_started
fi
}