perf: Nginx 开启 gzip 压缩和静态资源缓存,加速首次加载
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m2s

- gzip 压缩 JS/CSS/JSON 等资源(676KB→226KB)
- /assets/ 静态文件设置 1 年缓存(文件名含 hash)
- index.html 设置 no-cache 确保更新及时

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
zyc 2026-04-14 11:43:15 +08:00
parent feb305c454
commit 17696f9049
2 changed files with 37 additions and 2 deletions

View File

@ -9,7 +9,7 @@ RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/dist/ /usr/share/nginx/html/
# SPA fallback: all routes to index.html
RUN printf 'server {\n listen 80;\n root /usr/share/nginx/html;\n index index.html;\n location / {\n try_files $uri $uri/ /index.html;\n }\n location /api/ {\n return 404;\n }\n}\n' > /etc/nginx/conf.d/default.conf
# Nginx config: gzip + cache + SPA fallback
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

35
frontend/nginx.conf Normal file
View File

@ -0,0 +1,35 @@
server {
listen 80;
root /usr/share/nginx/html;
index index.html;
# Gzip compression
gzip on;
gzip_vary on;
gzip_min_length 256;
gzip_comp_level 6;
gzip_types
text/plain
text/css
text/javascript
application/javascript
application/json
application/xml
image/svg+xml;
# Static assets with hash in filename long cache
location /assets/ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# SPA fallback
location / {
try_files $uri $uri/ /index.html;
add_header Cache-Control "no-cache";
}
location /api/ {
return 404;
}
}