From 9ad016434820d8c7ddbfac987d516929cc2f864d Mon Sep 17 00:00:00 2001 From: saas-backend-agent Date: Thu, 3 Sep 2026 15:41:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(deploy):=20=E9=83=A8=E7=BD=B2=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E5=86=85=E5=B5=8C=20nginx=20=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=EF=BC=8C=E8=BF=90=E8=A1=8C=E6=97=B6=E8=A6=86=E7=9B=96=E5=AE=B9?= =?UTF-8?q?=E5=99=A8=E5=86=85=20upstream?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因:compose.yml 的 volume mount 不生效,因为 ci_staging_deploy.sh 使用 docker run 直接启动容器,不走 docker compose。 Build Staging Web Image 被 skip 后 retag 旧镜像,镜像内 nginx 配置 指向 production upstream,导致 staging web 容器 502。 修复:在部署脚本中内嵌 nginx 配置(staging/production 各一份), 部署时写入服务器本地文件,docker run 时 volume mount 到容器内 /etc/nginx/conf.d/default.conf,作为运行时安全网覆盖镜像内的配置。 - ci_staging_deploy.sh: 写入 nginx-staging.conf + volume mount - ci_production_deploy.sh: 写入 nginx-production.conf + volume mount - 回滚逻辑同步修改,确保回滚时也使用正确配置 --- scripts/ci_production_deploy.sh | 49 ++++++++++++++++++++++++++++++++ scripts/ci_staging_deploy.sh | 50 +++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/scripts/ci_production_deploy.sh b/scripts/ci_production_deploy.sh index 8a24aaabd..10c4e0f7c 100644 --- a/scripts/ci_production_deploy.sh +++ b/scripts/ci_production_deploy.sh @@ -59,6 +59,7 @@ REGISTRY_TOKEN="${ACR_PASSWORD:-${REGISTRY_TOKEN:-}}" ENV_FILE="${ENV_FILE:-/var/lib/xiaoxia-saas-production/.env}" GENERATED_DIR="${GENERATED_DIR:-/var/lib/xiaoxia-saas-production/generated}" LEGACY_ASSETS_DIR="${LEGACY_ASSETS_DIR:-/var/lib/xiaoxia-saas-production/legacy-assets}" +NGINX_CONF_FILE="${NGINX_CONF_FILE:-/var/lib/xiaoxia-saas-production/nginx-production.conf}" SKIP_MIGRATION="${SKIP_MIGRATION:-false}" SKIP_ROLLBACK="${SKIP_ROLLBACK:-false}" @@ -72,6 +73,52 @@ test -f "$ENV_FILE" mkdir -p "$GENERATED_DIR" mkdir -p "$LEGACY_ASSETS_DIR" +# ── 写入 Production Nginx 配置 ── +echo "Writing production nginx config..." +cat > "$NGINX_CONF_FILE" << 'NGINX_EOF' +server { + listen 80; + server_name _; + root /usr/share/nginx/html; + index index.html; + + gzip on; + gzip_vary on; + gzip_min_length 1024; + gzip_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml+rss; + + client_max_body_size 800m; + + location / { + try_files $uri /index.html; + } + + resolver 127.0.0.11 valid=10s; + resolver_timeout 5s; + + location /api/ { + proxy_pass http://xiaoxia-api-production:8000/api/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_read_timeout 300s; + proxy_send_timeout 300s; + proxy_request_buffering off; + } + + location /generated-files/ { + alias /app/generated/; + } + + location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { + expires 1y; + add_header Cache-Control "public, immutable"; + } +} +NGINX_EOF +echo "✅ Nginx config written: $NGINX_CONF_FILE" + echo "===========================================" echo " Production 部署 - $IMAGE_TAG" echo "===========================================" @@ -188,6 +235,7 @@ rollback() { --cpus 0.5 \ --memory 512m \ $LEGACY_VOLUME \ + -v "$NGINX_CONF_FILE:/etc/nginx/conf.d/default.conf:ro" \ --health-cmd "wget --spider -q http://127.0.0.1:80" \ --health-interval 30s \ --health-timeout 5s \ @@ -385,6 +433,7 @@ docker run -d \ --restart unless-stopped \ --cpus 0.5 \ --memory 512m \ + -v "$NGINX_CONF_FILE:/etc/nginx/conf.d/default.conf:ro" \ $LEGACY_VOLUME \ --health-cmd "wget --spider -q http://127.0.0.1:80" \ --health-interval 30s \ diff --git a/scripts/ci_staging_deploy.sh b/scripts/ci_staging_deploy.sh index 9d8cefc39..aee1c9132 100755 --- a/scripts/ci_staging_deploy.sh +++ b/scripts/ci_staging_deploy.sh @@ -46,6 +46,7 @@ REGISTRY_TOKEN="${ACR_PASSWORD:-${REGISTRY_TOKEN:-}}" ENV_FILE="${ENV_FILE:-/var/lib/xiaoxia-saas-staging/.env}" GENERATED_DIR="${GENERATED_DIR:-/var/lib/xiaoxia-saas-staging/generated}" LEGACY_ASSETS_DIR="${LEGACY_ASSETS_DIR:-/var/lib/xiaoxia-saas-staging/legacy-assets}" +NGINX_CONF_FILE="${NGINX_CONF_FILE:-/var/lib/xiaoxia-saas-staging/nginx-staging.conf}" SKIP_MIGRATION="${SKIP_MIGRATION:-false}" SKIP_ROLLBACK="${SKIP_ROLLBACK:-false}" @@ -65,6 +66,53 @@ echo "✅ .env file found: $ENV_FILE ($(wc -l < "$ENV_FILE") lines)" mkdir -p "$GENERATED_DIR" mkdir -p "$LEGACY_ASSETS_DIR" +# ── 写入 Staging Nginx 配置 ── +# 运行时覆盖 nginx 配置,确保 upstream 指向正确的 staging 网络 +echo "Writing staging nginx config..." +cat > "$NGINX_CONF_FILE" << 'NGINX_EOF' +server { + listen 80; + server_name _; + root /usr/share/nginx/html; + index index.html; + + gzip on; + gzip_vary on; + gzip_min_length 1024; + gzip_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml+rss; + + client_max_body_size 800m; + + location / { + try_files $uri /index.html; + } + + resolver 127.0.0.11 valid=10s; + resolver_timeout 5s; + + location /api/ { + proxy_pass http://xiaoxia-api-staging:8000/api/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_read_timeout 300s; + proxy_send_timeout 300s; + proxy_request_buffering off; + } + + location /generated-files/ { + alias /app/generated/; + } + + location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { + expires 1y; + add_header Cache-Control "public, immutable"; + } +} +NGINX_EOF +echo "✅ Nginx config written: $NGINX_CONF_FILE" + echo "===========================================" echo " Staging 部署 - $IMAGE_TAG (并行优化版)" echo "===========================================" @@ -165,6 +213,7 @@ rollback() { -p 127.0.0.1:3001:80 \ --restart unless-stopped \ $LEGACY_VOLUME \ + -v "$NGINX_CONF_FILE:/etc/nginx/conf.d/default.conf:ro" \ --health-cmd "wget --spider -q http://127.0.0.1:80" \ --health-interval 30s \ --health-timeout 5s \ @@ -467,6 +516,7 @@ docker run -d \ -p 127.0.0.1:3001:80 \ --restart unless-stopped \ $LEGACY_VOLUME \ + -v "$NGINX_CONF_FILE:/etc/nginx/conf.d/default.conf:ro" \ --health-cmd "wget --spider -q http://127.0.0.1:80" \ --health-interval 30s \ --health-timeout 5s \ -- 2.54.0