http - 通过 Nginx 无限期缓存 HTTP 响应失败

标签 http caching nginx last-modified if-modified-since

我试图告诉 nginx 永远缓存我的一些资源(js、css),或者至少缓存很长一段时间。

这个想法是,一旦 Assets 包使用 /assets/ URI 前缀(例如 /assets/foo-{fingerprint}.js)进行编译和发布,它留在那里,不需要改变。

互联网告诉我我应该编写以下规则:

location ~ ^/assets/.*-([^.]+)\.(js|css)$ {
  gzip_static on; # there's also a .gz of the asset
  expires max;
  add_header Cache-Control public;
  add_header Last-Modified "";
  add_header ETag "";
  break;
}

我希望这会导致 HTTP 代码 304“未修改”的响应,但每次我得到的都是一致的 HTTP 200(正常)。

我尝试过其他一些方法,例如:

a) 显式地将修改时间设置为过去的某个恒定时间点;

add_header Last-Modified "Thu, 01 Jan 1970 00:00:00 GMT";

b) 切换到 If-None-Match 检查;

add_header ETag $1;
if_modified_since off;

但是,唯一真正按需要起作用的是:

add_header Last-Modified "Thu, 01 Jan 2030 00:00:00 GMT";
if_modified_since before;

我迷路了。这与我认为正确的一切相反。请帮忙。

最佳答案

你应该改变你的互联网,因为他们给你错误的建议。

只需删除您所在位置的所有 add_header 行(以及多余的 brake)即可:

location ~ ^/assets/.*-([^.]+)\.(js|css)$ {
   gzip_static on; # there's also a .gz of the asset
   expires max;
}

并阅读来自真实互联网的文档:http://nginx.org/r/expireshttps://www.rfc-editor.org/rfc/rfc2616

关于http - 通过 Nginx 无限期缓存 HTTP 响应失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20009029/

相关文章:

javascript - 如何在特定的 AJAX 请求中强制使用 HTTP/1.1

java - 将请求重定向到 Controller 中的特定操作 public F.Promise<SimpleResult> call(Http.Context ctx){}

javascript - 用谷歌的sw-precache搭建的service worker真的能做到networkFirst吗?

java - 是否可以按插入/访问顺序迭代 Guava 缓存?

php - 使用 apache 和 nginx 反向代理安装 php-fpm 后 503 服务不可用

php - 如何设置权限以缓存和记录symfony docker容器

web-services - REST 可以通过 FTP 实现吗?

python - 在 Python 中创建和解析多部分 HTTP 请求

asp.net-mvc - 防止用户在 asp.net mvc 注销后返回

apache - 无法在 nginx 上使用 .html 名称重命名特定 .php 文件