Skip to content

Commit 2c832ba

Browse files
committed
feat(docker): 添加前端 Dockerfile + nginx.conf(SPA + API 反代)
1 parent 28793f1 commit 2c832ba

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Stage 1: Build
2+
FROM node:20-alpine AS builder
3+
WORKDIR /app
4+
RUN npm install -g pnpm
5+
COPY package.json pnpm-lock.yaml ./
6+
RUN pnpm install --frozen-lockfile
7+
COPY . .
8+
RUN pnpm build
9+
10+
# Stage 2: Serve via nginx
11+
FROM nginx:alpine AS runner
12+
COPY --from=builder /app/.output/public /usr/share/nginx/html
13+
COPY nginx.conf /etc/nginx/conf.d/default.conf
14+
EXPOSE 80
15+
CMD ["nginx", "-g", "daemon off;"]

nginx.conf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
server {
2+
listen 80;
3+
server_name _;
4+
root /usr/share/nginx/html;
5+
index index.html;
6+
7+
# SPA fallback — 所有路由交给 index.html 处理
8+
location / {
9+
try_files $uri $uri/ /index.html;
10+
}
11+
12+
# API 反向代理到后端
13+
location /api/ {
14+
proxy_pass http://backend:3000/;
15+
proxy_set_header Host $host;
16+
proxy_set_header X-Real-IP $remote_addr;
17+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
18+
proxy_set_header X-Forwarded-Proto $scheme;
19+
}
20+
21+
# 静态资源缓存
22+
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
23+
expires 1y;
24+
add_header Cache-Control "public, immutable";
25+
}
26+
27+
# gzip
28+
gzip on;
29+
gzip_types text/plain text/css application/json application/javascript text/xml application/xml text/javascript;
30+
gzip_min_length 1024;
31+
}

0 commit comments

Comments
 (0)