From 7108d2a58b9c5eb22584b4802fac34857628f87d Mon Sep 17 00:00:00 2001 From: hhs <386998068@qq.com> Date: Sun, 14 Jun 2026 13:38:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=89=8D=E7=AB=AF=20?= =?UTF-8?q?Dockerfile=20=E5=92=8C=20Nginx=20=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 多阶段构建:node:22-alpine 构建,nginx:stable-alpine 运行 - Nginx 配置:静态文件服务 + /api /ws 反代到后端 --- frontend/Dockerfile | 25 ++++++++++++++++++++++++ frontend/nginx.conf | 46 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 frontend/Dockerfile create mode 100644 frontend/nginx.conf diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 0000000..2e72310 --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,25 @@ +# ---- 构建阶段 ---- +FROM node:22-alpine AS builder + +WORKDIR /app + +# 先复制依赖清单,利用 Docker 缓存层 +COPY package.json package-lock.json ./ +RUN npm ci + +# 复制源码并构建 +COPY . . +RUN npm run build + +# ---- 运行阶段 ---- +FROM nginx:stable-alpine + +# 复制 Nginx 配置 +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;"] diff --git a/frontend/nginx.conf b/frontend/nginx.conf new file mode 100644 index 0000000..1490d6a --- /dev/null +++ b/frontend/nginx.conf @@ -0,0 +1,46 @@ +server { + listen 80; + server_name _; + + root /usr/share/nginx/html; + index index.html; + + # 前端静态资源 + location / { + try_files $uri $uri/ /index.html; + } + + # REST API 反代 + location /api/ { + proxy_pass http://backend:8080; + 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; + } + + # WebSocket 反代 + location /ws { + proxy_pass http://backend:8080; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + 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_read_timeout 86400s; + proxy_send_timeout 86400s; + } + + # 静态资源缓存 + location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { + expires 7d; + add_header Cache-Control "public, immutable"; + } + + # Gzip 压缩 + gzip on; + gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; + gzip_min_length 256; +}