简体   繁体   English

如何设置清漆缓存控制标头

[英]How to Set Varnish Cache-Control Headers

I am hoping someone can advise on the proper method for getting Varnish to send cache-control headers.我希望有人可以就让 Varnish 发送缓存控制标头的正确方法提出建议。 Currently, my configuration is sending "Cache-Control: no-cache" to clients.目前,我的配置正在向客户端发送“缓存控制:无缓存”

Thanks in advance to anyone who might be able to help...在此先感谢任何可能提供帮助的人...

Your back-end is sending "Cache-Control: no-cache" to Varnish which implies two things:您的后端正在向 Varnish 发送“Cache-Control: no-cache”,这意味着两件事:

  • Varnish will not store the response in the cache (so a next lookup will fail) Varnish 不会将响应存储在缓存中(因此下一次查找将失败)
  • Your clients (browsers and intermediate proxies) will not cache responses (and request them over and over).您的客户端(浏览器和中间代理)不会缓存响应(并一遍又一遍地请求它们)。

The solution is simple: remove the cache-control headers after fetching the response from the back-end (and before storing them in the cache).解决方案很简单:在从后端获取响应之后(以及在将它们存储在缓存中之前)删除缓存控制标头。

In your vcl file do:在您的 vcl 文件中执行以下操作:

sub vcl_fetch {
  remove beresp.http.Cache-Control;
  set beresp.http.Cache-Control = "public";
}

You can choose to only do this for certain urls (wrap it in ( if req.url ~ "" ) logic) and do way more advanced stuff.您可以选择仅对某些 url 执行此操作(将其包装在( if req.url ~ "" )逻辑中)并执行更高级的操作。

Varnish ignores Cache-Control: nocache as per the documentation.根据文档,Varnish 会忽略 Cache-Control: nocache。 Here is evidence confirming that:以下是证实这一点的证据:

http://drupal.org/node/1418908 http://drupal.org/node/1418908

To get that result, you should detect the header Cache-Control .要获得该结果,您应该检测标头 Cache-Control 。 nocache.没有缓存。 from your backend, and then invalidate the cache, set the backend response to not cacheable, or issue max-age: 0 in the other header (I forget the name right now).从您的后端,然后使缓存无效,将后端响应设置为不可缓存,或在另一个标头中发出 max-age: 0 (我现在忘记了名称)。

[ivy] has good advice, and/but it gets a little complicated when you try to obey a servers intent for end user (browser) caching. [ivy] 有很好的建议,并且/但是当您尝试遵守服务器对最终用户(浏览器)缓存的意图时,它会变得有点复杂。 I found this resource to be helpful in understanding a way to configure Varnish to hold onto a cache longer than a browser is instructed to...我发现这个资源有助于理解一种配置 Varnish 以保持缓存的时间比浏览器指示的时间更长的方法......

https://www.varnish-cache.org/trac/wiki/VCLExampleLongerCaching https://www.varnish-cache.org/trac/wiki/VCLExampleLongerCaching

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

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