fix(infra): web 容器运行时覆盖 nginx 配置,防止环境错配 #1645

Merged
auto-approve-bot merged 1 commits from fix/nginx-staging-runtime-override into develop 2026-09-03 15:33:20 +08:00
Owner

问题

staging web 容器 nginx upstream 指向 xiaoxia-api-production,原因是镜像被 skip 构建后 retag 了旧镜像(内含 production 默认配置)。staging 容器在 xiaoxia-net-staging 网络无法解析 production 容器名 → DNS 失败 → nginx 崩溃重启 → 502。

修复

compose.yml 为 web 服务添加 volume mount,运行时将正确环境的 nginx 配置挂载到 /etc/nginx/conf.d/default.conf,覆盖镜像内的默认配置:

  • staging: ./nginx-staging.confxiaoxia-api-staging:8000
  • production: ./nginx-production.confxiaoxia-api-production:8000

变更文件

  • infra/docker/compose.yml:web 服务添加 volumesenvironment

注意

  • 只挂载 nginx 配置文件(/etc/nginx/conf.d/default.conf),不挂载 /usr/share/nginx/html
  • 部署时需确保 nginx-staging.conf / nginx-production.conf 文件存在于 compose.yml 同级目录
  • nginx-staging.conf 已存在于仓库中
## 问题 staging web 容器 nginx upstream 指向 `xiaoxia-api-production`,原因是镜像被 skip 构建后 retag 了旧镜像(内含 production 默认配置)。staging 容器在 `xiaoxia-net-staging` 网络无法解析 production 容器名 → DNS 失败 → nginx 崩溃重启 → 502。 ## 修复 compose.yml 为 web 服务添加 volume mount,运行时将正确环境的 nginx 配置挂载到 `/etc/nginx/conf.d/default.conf`,覆盖镜像内的默认配置: - staging: `./nginx-staging.conf` → `xiaoxia-api-staging:8000` - production: `./nginx-production.conf` → `xiaoxia-api-production:8000` ## 变更文件 - `infra/docker/compose.yml`:web 服务添加 `volumes` 和 `environment` ## 注意 - 只挂载 nginx 配置文件(`/etc/nginx/conf.d/default.conf`),不挂载 `/usr/share/nginx/html` - 部署时需确保 `nginx-staging.conf` / `nginx-production.conf` 文件存在于 compose.yml 同级目录 - `nginx-staging.conf` 已存在于仓库中
xiaoxia added 1 commit 2026-09-03 15:20:51 +08:00
fix(infra): web 容器运行时覆盖 nginx 配置,防止环境错配
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 1s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 2s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 20s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 22s
AI Code Review / AI Code Review (pull_request) Failing after 1m5s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 1m10s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 1m44s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 1m56s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 2m17s
CI/CD Pipeline / Validate - Style (pull_request) Successful in 2m22s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 2m49s
CI/CD Pipeline / Validate - Security (pull_request) Successful in 7m36s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 8m16s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Successful in 4s
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
ACR Cleanup / ACR Image Cleanup (pull_request_target) Successful in 6s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 29s
dbcddc87b4
问题:staging web 容器 nginx upstream 指向 xiaoxia-api-production,
原因是镜像被 skip 构建后 retag 了旧镜像(内含 production 默认配置)。
staging 容器在 xiaoxia-net-staging 网络无法解析 production 容器名,
导致 DNS 失败 → nginx 崩溃重启 → 502。

修复:compose.yml 为 web 服务添加 volume mount,运行时将正确环境的
nginx 配置文件挂载到 /etc/nginx/conf.d/default.conf,覆盖镜像内的默认配置。
- staging: ./nginx-staging.conf → xiaoxia-api-staging:8000
- production: ./nginx-production.conf → xiaoxia-api-production:8000

注意:只挂载 nginx 配置文件,不挂载 /usr/share/nginx/html(静态文件)。
Collaborator

【阻塞级判定】

  • 是否存在阻塞级问题:是
  • 阻塞级问题数量:1 个

📊 审查概览

  • 整体评价:需修改
  • 建议级问题数量:1 个

🔴 阻塞级问题(必须修复)

  1. [infra/docker/compose.yml: 183] 动态路径挂载存在运行时崩溃风险
    • 问题类型:逻辑bug / 边界条件遗漏
    • 问题描述:volumes 配置使用了变量插值 ./nginx-${ENV:-staging}.conf。如果宿主机上不存在对应的配置文件(例如当 ENV=production 但目录下只有 nginx-staging.conf 时),Docker 守护进程将拒绝启动容器,导致服务不可用。代码中未包含对文件存在性的校验机制。
    • 修改建议:在部署脚本中增加文件存在性检查,或者确保所有环境的配置文件均已提交至仓库。建议在文档中明确说明该挂载对宿主机文件结构的依赖。

💡 改进建议(不阻塞合并)

  1. [infra/docker/compose.yml: 180] 环境变量可能未生效
    • 具体内容:引入了环境变量 NGINX_ENV。标准的 Nginx Docker 镜像(官方 nginx:alpine 等)并不会自动读取此变量来改变 Nginx 的运行配置。请确认当前使用的镜像是否包含自定义的 entrypoint 脚本来处理此变量;如果是标准镜像,该行代码无效,建议移除以免造成误导。

良好实践

  • 使用 :ro 标志以只读模式挂载配置文件,符合最小权限原则。
  • 使用 ${ENV:-staging} 设置了合理的默认值,避免了变量未定义时的空指针风险。
  • 注释清晰说明了不挂载 /usr/share/nginx/html 的重要性,有助于维护。

🤖 由 AI 代码审查机器人自动生成 | 2026-09-03 07:21:56 | 模型:

### 【阻塞级判定】 - 是否存在阻塞级问题:是 - 阻塞级问题数量:1 个 ### 📊 审查概览 - 整体评价:需修改 - 建议级问题数量:1 个 ### 🔴 阻塞级问题(必须修复) 1. **[infra/docker/compose.yml: 183] 动态路径挂载存在运行时崩溃风险** - 问题类型:逻辑bug / 边界条件遗漏 - 问题描述:`volumes` 配置使用了变量插值 `./nginx-${ENV:-staging}.conf`。如果宿主机上不存在对应的配置文件(例如当 `ENV=production` 但目录下只有 `nginx-staging.conf` 时),Docker 守护进程将拒绝启动容器,导致服务不可用。代码中未包含对文件存在性的校验机制。 - 修改建议:在部署脚本中增加文件存在性检查,或者确保所有环境的配置文件均已提交至仓库。建议在文档中明确说明该挂载对宿主机文件结构的依赖。 ### 💡 改进建议(不阻塞合并) 1. **[infra/docker/compose.yml: 180] 环境变量可能未生效** - 具体内容:引入了环境变量 `NGINX_ENV`。标准的 Nginx Docker 镜像(官方 nginx:alpine 等)并不会自动读取此变量来改变 Nginx 的运行配置。请确认当前使用的镜像是否包含自定义的 entrypoint 脚本来处理此变量;如果是标准镜像,该行代码无效,建议移除以免造成误导。 ### ✅ 良好实践 - 使用 `:ro` 标志以只读模式挂载配置文件,符合最小权限原则。 - 使用 `${ENV:-staging}` 设置了合理的默认值,避免了变量未定义时的空指针风险。 - 注释清晰说明了不挂载 `/usr/share/nginx/html` 的重要性,有助于维护。 --- <sub>🤖 由 AI 代码审查机器人自动生成 | 2026-09-03 07:21:56 | 模型: </sub> <!-- AI_CODE_REVIEW_AUTO_COMMENT -->

🚀 预览环境已部署

项目 详情
PR号 #1645
预览链接 https://pr-1645.preview.xiaoxiajianji.com
API环境 staging

💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。

🔄 每次提交新代码后预览环境会自动更新。

🗑️ PR 关闭或合并后,预览环境会自动清理。

🚀 **预览环境已部署** | 项目 | 详情 | |------|------| | PR号 | #1645 | | 预览链接 | [https://pr-1645.preview.xiaoxiajianji.com](https://pr-1645.preview.xiaoxiajianji.com) | | API环境 | staging | > 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。 > > 🔄 每次提交新代码后预览环境会自动更新。 > > 🗑️ PR 关闭或合并后,预览环境会自动清理。
auto-approve-bot approved these changes 2026-09-03 15:32:49 +08:00
auto-approve-bot left a comment
Collaborator

CI全绿,自动审批通过。

CI全绿,自动审批通过。
auto-approve-bot approved these changes 2026-09-03 15:32:49 +08:00
auto-approve-bot left a comment
Collaborator

CI全绿,自动审批通过。

CI全绿,自动审批通过。
auto-approve-bot merged commit 0e2dd60a8d into develop 2026-09-03 15:33:20 +08:00
auto-approve-bot deleted branch fix/nginx-staging-runtime-override 2026-09-03 15:33:21 +08:00

🗑️ 预览环境已清理

PR #1645 已关闭或合并,对应的预览环境已被清理。

如有需要,可以重新打开 PR 来重新生成预览环境。

🗑️ **预览环境已清理** PR #1645 已关闭或合并,对应的预览环境已被清理。 > 如有需要,可以重新打开 PR 来重新生成预览环境。
Sign in to join this conversation.