server { listen 80; server_name _; root /usr/share/nginx/html; index index.html; gzip on; gzip_types text/plain text/css application/javascript application/json image/svg+xml; gzip_min_length 1024; # 运行时配置:绝不能被缓存,否则运维改了环境变量重启, # 浏览器仍在用旧的 API 地址,且极难排查。 # 不要在这里再加 expires 指令:它会额外发一个 Cache-Control 头, # 与下面的 add_header 重复。no-store 已经足够。 location = /config.js { add_header Cache-Control "no-store" always; } # index.html 必须每次回源校验(通常是一个很便宜的 304)。 # 它是唯一指向 /assets/ 那些 hash 文件的地方 —— 一旦它被缓存,浏览器就会继续 # 请求旧的 asset URL,而那些 URL 是 immutable 的,用户会被钉死在旧版本且无法自愈。 location = /index.html { add_header Cache-Control "no-cache" always; } # 构建产物文件名带内容 hash(改图片不改文件名,产物名一样会变),可永久缓存。 # 不要用 expires 指令:它会额外发一个 Cache-Control 头,与 add_header 重复。 location /assets/ { add_header Cache-Control "public, max-age=31536000, immutable" always; } location / { try_files $uri $uri/ /index.html; } }