简体   繁体   English

使Nginx缓存尊重缓存控制标头

[英]Make Nginx caching respect cache-control headers

Nginx can cache content, ie act as a reverse-proxy, but the purge time must be fixed in the Nginx config. Nginx可以缓存内容,即充当反向代理,但是清除时间必须在Nginx配置中固定。

Is there a way to make Nginx purge automatically according to cache-control headers from upstream app servers? 有没有一种方法可以根据上游应用程序服务器中的缓存控制标头自动清除Nginx?

After researching this issue, I found it's not well documented as most people seem concerned with CMSs where it's easy to say "cache articles for a day", but less so for dynamic app servers which may vary according to each request. 在研究了这个问题之后,我发现它没有被很好地记录下来,因为大多数人似乎都在关注CMS,因为它很容易说“一天缓存文章”,但是对于动态应用程序服务器却很少,后者可能会根据每个请求而有所不同。

Several options are: 有几种选择:

  • Nginx Plus Nginx Plus
  • Ngx Cache Purge module , though it hasn't seen any updates in 2 years Ngx缓存清除模块 ,尽管两年来没有任何更新
  • Define multiple caches with different expiry times and apply them differently by path, if that's possible. 定义多个具有不同到期时间的缓存,并在可能的情况下按路径不同地应用它们。
  • Forget caching on Nginx and put a server like Varnish in front of it. 忘记在Nginx上缓存,并在其前面放置一个类似Varnish的服务器。

Update: Looking further, I now think proxy_cache_valid doesn't even need to be present. 更新:进一步看,我现在认为甚至不需要存在proxy_cache_valid。 It just sets a default expiry if there's no cache-control/expiry from upstream. 如果没有来自上游的缓存控制/过期,它只会设置默认过期。 Nginx will still consider content as stale if there's no proxy_cache_valid, but it won't immediately be purged. 如果没有proxy_cache_valid,Nginx仍将内容视为过时的,但不会立即清除。 The reason it keeps stale content in the cache (determined with "inactive" param and optionally proxy_cache_valid) is that it can serve stale content using proxy_cache_use_stale (useful if there's an error upstream). 它将过时的内容保留在缓存中的原因(由“非活动”参数以及可选的proxy_cache_valid确定)是因为它可以使用proxy_cache_use_stale服务过时的内容(如果上游有错误,则很有用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM