chore: 优化 Docker 配置,添加生产环境部署脚本和文档
All checks were successful
Deploy to Server / deploy (push) Successful in 19s

This commit is contained in:
爽哒哒
2026-03-18 22:48:36 +08:00
parent f655aa0ede
commit e3a620b395
8 changed files with 610 additions and 43 deletions

View File

@@ -1,13 +1,10 @@
# Use an official Node runtime as a parent image
FROM node:22-alpine
# 构建阶段
FROM node:20-alpine AS builder
# Set working directory
# 设置工作目录
WORKDIR /app
# Install build dependencies for native modules
RUN apk add --no-cache autoconf automake libtool make g++ zlib-dev nasm python3
# Install dependencies
# 安装依赖
COPY package.json package-lock.json* ./
RUN npm install --registry=https://registry.npmmirror.com
@@ -16,14 +13,22 @@ COPY . .
# 接收构建参数
ARG VITE_API_URL=/api
# 设置环境变量供构建时使用
ENV VITE_API_URL=$VITE_API_URL
ENV VITE_API_URL=${VITE_API_URL}
# 构建生产环境代码
RUN npm run build
# 暴露应用运行的端口
EXPOSE 15173
# 生产阶段 - 使用 Nginx
FROM nginx:alpine
# 启动应用 (Preview 模式)
CMD ["npm", "run", "preview", "--", "--host", "0.0.0.0", "--port", "15173"]
# 复制 Nginx 配置
COPY nginx.conf /etc/nginx/conf.d/default.conf
# 从构建阶段复制构建产物
COPY --from=builder /app/dist /usr/share/nginx/html
# 暴露端口
EXPOSE 80
# 启动 Nginx
CMD ["nginx", "-g", "daemon off;"]

67
frontend/nginx.conf Normal file
View File

@@ -0,0 +1,67 @@
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
# Gzip 压缩
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;
# 缓存静态资源
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|otf)$ {
expires 1y;
add_header Cache-Control "public, immutable";
try_files $uri =404;
}
# 前端路由支持 - 所有路由指向 index.html
location / {
try_files $uri $uri/ /index.html;
add_header Cache-Control "no-cache";
}
# API 代理 - 将 /api 请求转发到后端
location /api/ {
proxy_pass http://backend:8000/api/;
proxy_http_version 1.1;
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;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# 媒体文件代理
location /media/ {
proxy_pass http://backend:8000/media/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# 静态文件代理
location /static/ {
proxy_pass http://backend:8000/static/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# 健康检查
location /health {
access_log off;
return 200 "healthy\n";
add_header Content-Type text/plain;
}
# 错误页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}