nginx.conf 1.4 KB

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