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 \