chore: Docker生产部署配置 + 部署脚本

This commit is contained in:
2026-08-07 11:53:40 +08:00
parent bff8787e62
commit 41dfc53f83
5 changed files with 300 additions and 0 deletions

18
frontend/Dockerfile.prod Normal file
View File

@ -0,0 +1,18 @@
# ============================================================
# 前端生产 Dockerfile — 多阶段构建Vite 打包 → Nginx 托管
# ============================================================
# --- 阶段 1编译 React ---
FROM node:20-alpine AS builder
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --production=false
COPY . .
RUN npx vite build
# --- 阶段 2Nginx 托管静态文件 ---
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

32
frontend/nginx.conf Normal file
View File

@ -0,0 +1,32 @@
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.html;
# Gzip 压缩
gzip on;
gzip_types text/css application/javascript application/json image/svg+xml;
gzip_min_length 256;
# 静态文件缓存
location /assets/ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# API 代理到后端
location /api/ {
proxy_pass http://backend:8000;
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;
}
# SPA 路由回退
location / {
try_files $uri $uri/ /index.html;
}
}