Docker + Nginx 前端部署实战
前端项目部署用 Docker 的好处:环境一致、部署可重复、回滚方便。
多阶段构建 Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| FROM node:22-alpine AS builder WORKDIR /app COPY package.json pnpm-lock.yaml ./ RUN corepack enable && pnpm install --frozen-lockfile COPY . . RUN pnpm build
FROM nginx:alpine COPY --from=builder /app/dist /usr/share/nginx/html COPY nginx.conf /etc/nginx/conf.d/default.conf EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
|
两阶段分离:构建用 Node,运行用 Nginx。最终镜像只有 20MB 左右。
Nginx 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.html;
gzip on; gzip_types text/plain text/css application/json application/javascript text/xml; gzip_min_length 1024;
location /assets/ { expires 1y; add_header Cache-Control "public, immutable"; }
location / { try_files $uri $uri/ /index.html; }
add_header X-Frame-Options "SAMEORIGIN" always; add_header X-Content-Type-Options "nosniff" always; }
|
HTTPS 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| server { listen 443 ssl http2; server_name example.com;
ssl_certificate /etc/ssl/certs/example.com.pem; ssl_certificate_key /etc/ssl/private/example.com.key;
}
server { listen 80; server_name example.com; return 301 https://$host$request_uri; }
|
Docker Compose
1 2 3 4 5 6 7 8 9 10 11
| version: '3.8' services: web: build: . ports: - "80:80" - "443:443" volumes: - ./nginx.conf:/etc/nginx/conf.d/default.conf - ./ssl:/etc/ssl restart: unless-stopped
|
常用命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| docker build -t my-frontend .
docker run -d -p 80:80 my-frontend
docker logs -f <container_id>
docker exec -it <container_id> sh
docker image prune -f
|
性能优化
- 镜像缓存 — 先 COPY package.json 再 RUN install,利用 Docker 层缓存
- .dockerignore — 排除 node_modules、dist、.git
- Alpine 基础镜像 — 比 debian 小 10 倍
- 多阶段构建 — 构建产物之外的东西都不进最终镜像
踩坑
- SPA 路由 404 — 必须配
try_files $uri /index.html
- 时区问题 — Alpine 默认 UTC,需要
apk add tzdata
- 静态资源路径 — 确保
base 配置和实际部署路径一致