通过nginx替换指纹文件服务器时,在浏览器中过期资产缓存

前端之家收集整理的这篇文章主要介绍了通过nginx替换指纹文件服务器时,在浏览器中过期资产缓存前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我通过Nginx提供单页JavaScript应用程序,当我部署新版本时,我想强制浏览器使其JS缓存无效,并请求/使用可用的最新版本.

因此,例如,当我使用名为my-app-eaea342.js的文件替换服务器文件夹中名为my-app-8e8faf9.js的文件时,我不希望浏览器将my-app-8e8faf9.js从他们的缓存已经不再了但是当没有新版本可用时,我仍然希望他们从缓存中读取资产.

如何使用Nginx配置实现?这是我现有的配置:

  1. server {
  2. listen 80;
  3. server_name my.server.com;
  4. root /u/apps/my_client_production/current;
  5. index index.html;
  6. # ~2 seconds is often enough for most folks to parse HTML/CSS and
  7. # retrieve needed images/icons/frames,connections are cheap in
  8. # Nginx so increasing this is generally safe...
  9. keepalive_timeout 10;
  10. client_max_body_size 100M;
  11. access_log /u/apps/my_client_production/shared/log/Nginx.access.log;
  12. error_log /u/apps/my_client_production/shared/log/Nginx.error.log info;
  13. location / {
  14. try_files $uri $uri/ =404;
  15. gzip_static on;
  16. expires max;
  17. add_header Cache-Control public;
  18. }
  19. # Error pages
  20. error_page 500 502 503 504 /500.html;
  21. }
最佳答案
通过更改资源网址来实现缓存无效是一种常规做法.

但是为了工作,您需要您的html文件不被永久缓存,以便浏览器将在这些名称更改时具有一些信息.

所以html和资产的单独位置. Matcher可以不同,具体取决于您如何存储它们,例如:

  1. location / {
  2. try_files $uri $uri/ =404;
  3. gzip_static on;
  4. }
  5. location ^~ /assets/ {
  6. gzip_static on;
  7. expires max;
  8. add_header Cache-Control public;
  9. }

猜你在找的Nginx相关文章