fix(admin): nginx.conf 与 deploy 对齐,避免 assets 回退为 HTML MIME 白屏

Made-with: Cursor
This commit is contained in:
whm
2026-03-23 17:00:27 +08:00
parent 0a1fe41314
commit 80176ea6fc
4 changed files with 35 additions and 2 deletions

View File

@@ -11,7 +11,7 @@ RUN npm run build
ARG REGISTRY_MIRROR=docker.m.daocloud.io/library/ ARG REGISTRY_MIRROR=docker.m.daocloud.io/library/
FROM ${REGISTRY_MIRROR}nginx:alpine FROM ${REGISTRY_MIRROR}nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html COPY --from=builder /app/dist /usr/share/nginx/html
# 外层 Nginx 已把 /admin/ 转成 / 转发到本容器,故这里用 location / 提供 SPAbase 为 /admin/ 时静态资源请求为 /assets/... # 与 deploy/admin/default.conf 同逻辑:^~ /assets/ 避免缺失 chunk 时回退到 index.html → MIME text/html 白屏
RUN echo 'server { listen 80; root /usr/share/nginx/html; location / { try_files $uri $uri/ /index.html; } }' > /etc/nginx/conf.d/default.conf COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80 EXPOSE 80
CMD ["nginx", "-g", "daemon off;"] CMD ["nginx", "-g", "daemon off;"]

24
admin/nginx.conf Normal file
View File

@@ -0,0 +1,24 @@
# 与 deploy/admin/default.conf 保持一致Compose 挂载该文件;镜像内也用此配置)
# 外层 Nginx 已把 /admin/ 转成 / 转发到本容器,此处 location / 提供 SPA
server {
listen 80;
root /usr/share/nginx/html;
index index.html;
# 发版后勿长期缓存入口,否则浏览器保留旧 index.html、却拉新 chunk 名 → 白屏
location = /index.html {
add_header Cache-Control "no-cache, no-store, must-revalidate";
expires -1;
}
location ^~ /assets/ {
try_files $uri =404;
access_log off;
expires 7d;
add_header Cache-Control "public, immutable";
}
location / {
try_files $uri $uri/ /index.html;
}
}

View File

@@ -6,3 +6,11 @@
- **deploy/web/default.conf**、**deploy/admin/default.conf**Nginx 配置,已纳入版本库。 - **deploy/web/default.conf**、**deploy/admin/default.conf**Nginx 配置,已纳入版本库。
日常更新:在服务器执行 `./pull-and-restart.sh` 会拉代码、重新构建到上述目录并重启容器。若只改静态资源,也可在服务器上手动构建后只重启对应容器。 日常更新:在服务器执行 `./pull-and-restart.sh` 会拉代码、重新构建到上述目录并重启容器。若只改静态资源,也可在服务器上手动构建后只重启对应容器。
## 后台白屏 / 控制台 “MIME type text/html” 针对 `index-*.js`
表示浏览器拿到的不是 JS而是 HTML常见`/assets/*.js` 被 SPA 回退成 `index.html`,或 404 返回了 HTML 错误页)。
1. **确认 Nginx 配置已更新**`deploy/admin/default.conf` 须含 `location ^~ /assets/``try_files $uri =404`(与仓库内 `admin/nginx.conf` 一致),挂载后重启 `admin` 容器。
2. **确认 dist 完整**`deploy/admin/dist/assets/` 下须有与 `index.html``<script type="module">` 引用**同名**的哈希文件;发版后应**整目录**替换 `dist`(勿只拷 `index.html`)。
3. **本地重建**:在项目根按 `pull-and-restart.sh` 方式在 `admin/` 执行 `npm run build``vite.config``base` 须为 `'/admin/'`

View File

@@ -1,3 +1,4 @@
# 与 admin/nginx.conf 保持内容一致Compose 挂载本文件admin 镜像内 COPY 同配置)
# 外层 Nginx 已把 /admin/ 转成 / 转发到本容器,此处 location / 提供 SPA # 外层 Nginx 已把 /admin/ 转成 / 转发到本容器,此处 location / 提供 SPA
server { server {
listen 80; listen 80;