This commit is contained in:
2026-02-03 19:37:58 +08:00
parent 8ffe3e0e15
commit db4a0b109c
42 changed files with 6594 additions and 183 deletions

View File

@@ -1,41 +1,44 @@
# ===============================
# 1) 依赖安装 + 构建阶段
# ===============================
FROM node:16-alpine AS build
FROM node:20.15.0-alpine AS builder
# 设定工作目录,后续所有指令基于此,避免路径混乱
WORKDIR /app
# 只拷贝 package.json加快缓存命中
# 配置npm国内源仅1次配置后续所有npm命令自动复用删除冗余的registry传参
ENV NPM_REGISTRY=https://registry.npmmirror.com/
RUN npm config set registry ${NPM_REGISTRY}
# 先拷贝package.json核心利用Docker层缓存代码不变则不重装依赖
COPY package.json ./
# 预先安装依赖(利用缓存
RUN npm install --registry=https://registry.npmmirror.com
# 安装依赖(无需重复写registry已全局配置加快构建
RUN npm install
# 拷贝全部代码
# 拷贝全部项目代码(放在装依赖后,最大化缓存收益)
COPY . .
# 通过构建参数传入构建环境(如 sit/test/prod
# 构建参数默认sitbuild时可通过--build-arg覆盖如prod/test
ARG BUILD_ENV=sit
# 注入环境变量,确保构建脚本能读取到(部分框架需要环境变量透传)
ENV BUILD_ENV=${BUILD_ENV}
# 要求 package.json 中的 script 形如:build:sit, build:test, build:prod
# 执行环境构建命令(兼容package.jsonbuild:sit/build:test/build:prod
RUN npm run build:${BUILD_ENV}
# ===============================
# 2) 生产镜像阶段
# 2) 生产镜像阶段轻量Nginx仅保留构建产物
# ===============================
FROM nginx:1.25-alpine AS runtime
# 清理默认 nginx 静态内容
RUN rm -rf /usr/share/nginx/html/*
# 拷贝 build 结果
COPY --from=build /app/dist /usr/share/nginx/html/
# 权限
RUN chmod -R 755 /usr/share/nginx/html
FROM nginx:stable-alpine AS runtime
# 暴露80端口Docker声明方便容器映射不影响实际运行
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
# 复制自定义Nginx配置解决路由404+禁用所有缓存,核心修正)
COPY ./default.conf /etc/nginx/conf.d/default.conf
# 从构建阶段拷贝最终构建产物到Nginx静态资源根目录
COPY --from=builder /app/dist /usr/share/nginx/html
# Nginx官方镜像默认自动启动nginx无需额外CMD/ENTRYPOINT

View File

@@ -0,0 +1,27 @@
server {
listen 80;
server_name _; # 匹配所有域名,适合容器化部署
root /usr/share/nginx/html; # Nginx静态资源根目录和Dockerfile拷贝路径一致
index index.html index.htm; # 默认入口文件
# 客户端最大请求体大小,适配文件上传等场景
client_max_body_size 100M;
# 核心解决前端Vue/React/Angular等SPA的history模式路由刷新404问题
location / {
try_files $uri $uri/ /index.html;
}
# 全局禁用缓存:禁止浏览器/代理服务器缓存任何资源
add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate" always;
add_header Pragma "no-cache" always;
add_header Expires "0" always;
add_header Surrogate-Control "no-store" always;
# 对所有静态资源单独强化无缓存配置,立即过期
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
try_files $uri =404; # 资源不存在直接返回404避免走index.html
expires -1; # 立即过期,优先级高于全局缓存配置
add_header Cache-Control "no-store, no-cache, must-revalidate" always;
}
}

View File

@@ -1,16 +1,3 @@
# # 使用 Nginx 官方轻量镜像
# FROM nginx:1.25-alpine
# # 拷贝前端构建产物
# COPY dist/ /usr/share/nginx/html/
# # 暴露端口
# EXPOSE 80
# CMD ["nginx", "-g", "daemon off;"]
# 第一阶段:构建阶段 - 使用指定node20.15.0版本安装pnpm
FROM node:20.15.0-alpine AS builder
@@ -46,7 +33,7 @@ FROM nginx:stable-alpine
EXPOSE 80
# 可选替换Nginx默认配置解决前端路由刷新404、开启gzip压缩优化静态资源
# COPY ./nginx.conf /etc/nginx/conf.d/default.conf
# COPY ./default.conf /etc/nginx/conf.d/default.conf
# 从构建阶段builder复制构建后的dist目录到Nginx的静态资源根目录
COPY --from=builder /app/dist /usr/share/nginx/html