nginx - 在更新时提供过时的缓存响应

Jer*_*NER 5 nginx cache

在 nginx 中,我使用的是

proxy_cache_use_stale updating
Run Code Online (Sandbox Code Playgroud)

指令(http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_use_stale),以便对无效缓存内容的并发请求仅向上游发送 1 个请求(非第一个请求以陈旧内容响应,而第一个请求是更新内容)。

这很好用。

现在有没有办法让第一个请求立即以陈旧的内容响应,同时触发更新?

现在,响应时间对于非第一个无效请求非常好,但第一个需要等到后端响应。我更喜欢在缓存更新之前提供陈旧的内容。

Mik*_*den 11

您要查找的内容称为 stale-while-revalidate (RFC 5861),它在 nginx 中作为名为proxy_cache_background_update的指令实现在 Varnish 4和 Squid 中可以找到类似的功能,这称为Collapsed Forwarding


Ric*_*ich 7

此功能在nginx1.11(2016 年 4 月)中作为proxy_cache_background_update 添加

  • 包含此功能的稳定版本是 1.12。希望一个月左右。 (2认同)
  • 我们目前有 1.12.1。另请参阅:[来自 Varnish 的 Stale-while-revalidate 缓存替换](/sf/answers/2809392561/) (2认同)