From c6e5779b7618fdf8d8dfc5f9d271ce19dac6d387 Mon Sep 17 00:00:00 2001 From: whm <973418690@qq.com> Date: Sun, 22 Mar 2026 01:54:43 +0800 Subject: [PATCH] =?UTF-8?q?perf(nginx):=20=E9=9D=99=E6=80=81=E5=9B=BE/?= =?UTF-8?q?=E6=8E=A8=E5=B9=BF=E8=B5=84=E6=BA=90=20Cache-Control=207d?= =?UTF-8?q?=EF=BC=8Cindex.html=20=E7=A6=81=E6=AD=A2=E9=95=BF=E6=9C=9F?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Made-with: Cursor --- deploy/web/default.conf | 20 ++++++++++++++++---- nginx/web.conf | 15 +++++++++++++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/deploy/web/default.conf b/deploy/web/default.conf index 39ee35c..f0acb7b 100644 --- a/deploy/web/default.conf +++ b/deploy/web/default.conf @@ -17,27 +17,39 @@ server { add_header Cache-Control "public, immutable"; } + # SPA 入口:勿长期缓存,否则发版后用户仍可能拿到旧 index + location = /index.html { + add_header Cache-Control "no-cache, no-store, must-revalidate"; + expires -1; + } + # web/public 挂载到 /var/www/yh-public:与 dist 根目录同 URL(如 /logo.png),优先读挂载,无则回退 dist location ~ ^/([^/]+\.(?:png|jpe?g|gif|ico|svg|webp|webmanifest))$ { root /var/www/yh-public; try_files /$1 @dist_root_public; + expires 7d; + add_header Cache-Control "public, max-age=604800"; } location @dist_root_public { root /usr/share/nginx/html; try_files $uri =404; + expires 7d; + add_header Cache-Control "public, max-age=604800"; } # web/public/social/ → 关注我们二维码等(挂载 /var/www/yh-public) location ^~ /social/ { alias /var/www/yh-public/social/; - add_header Cache-Control "public, max-age=300"; + expires 7d; + add_header Cache-Control "public, max-age=604800"; } - # 推广素材:文件必须真实存在,禁止落到 SPA index.html(否则前端误判「静态存在」、视频拿到 HTML) - # 宿主机 ./web/promotion 挂载覆盖本路径,改 social 下文件无需重新 npm build + # 推广素材:含视频/封面等大文件,浏览器可长期缓存减轻二次访问流量 + # 宿主机 ./web/promotion 挂载覆盖本路径,改素材后依赖 max-age 到期或用户强刷 location ^~ /promotion/ { try_files $uri =404; - add_header Cache-Control "public, max-age=86400"; + expires 7d; + add_header Cache-Control "public, max-age=604800"; } location = / { diff --git a/nginx/web.conf b/nginx/web.conf index 3ef66ea..7253d65 100644 --- a/nginx/web.conf +++ b/nginx/web.conf @@ -15,24 +15,35 @@ server { add_header Cache-Control "public, immutable"; } + location = /index.html { + add_header Cache-Control "no-cache, no-store, must-revalidate"; + expires -1; + } + # web/public 挂载 /var/www/yh-public;单段文件名同 dist 根 URL,优先挂载后回退 dist location ~ ^/([^/]+\.(?:png|jpe?g|gif|ico|svg|webp|webmanifest))$ { root /var/www/yh-public; try_files /$1 @dist_root_public; + expires 7d; + add_header Cache-Control "public, max-age=604800"; } location @dist_root_public { root /usr/share/nginx/html; try_files $uri =404; + expires 7d; + add_header Cache-Control "public, max-age=604800"; } location ^~ /social/ { alias /var/www/yh-public/social/; - add_header Cache-Control "public, max-age=300"; + expires 7d; + add_header Cache-Control "public, max-age=604800"; } location ^~ /promotion/ { try_files $uri =404; - add_header Cache-Control "public, max-age=86400"; + expires 7d; + add_header Cache-Control "public, max-age=604800"; } location = / {