From 17696f904947ed1fab2da5d1bca31b6dbbe6506a Mon Sep 17 00:00:00 2001 From: zyc <1439655764@qq.com> Date: Tue, 14 Apr 2026 11:43:15 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20Nginx=20=E5=BC=80=E5=90=AF=20gzip=20?= =?UTF-8?q?=E5=8E=8B=E7=BC=A9=E5=92=8C=E9=9D=99=E6=80=81=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E7=BC=93=E5=AD=98=EF=BC=8C=E5=8A=A0=E9=80=9F=E9=A6=96=E6=AC=A1?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - gzip 压缩 JS/CSS/JSON 等资源(676KB→226KB) - /assets/ 静态文件设置 1 年缓存(文件名含 hash) - index.html 设置 no-cache 确保更新及时 Co-Authored-By: Claude Opus 4.6 (1M context) --- frontend/Dockerfile | 4 ++-- frontend/nginx.conf | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 frontend/nginx.conf diff --git a/frontend/Dockerfile b/frontend/Dockerfile index f2c528d..9710ac9 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -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;"] diff --git a/frontend/nginx.conf b/frontend/nginx.conf new file mode 100644 index 0000000..5900922 --- /dev/null +++ b/frontend/nginx.conf @@ -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; + } +}