简体   繁体   English

基于标题的清漆缓存

[英]Varnish cache based on header

Currently I'm running Varnish on a server which runs about 30-40 different websites. 目前我在运行大约30-40个不同网站的服务器上运行Varnish。 All of these websites use the same library for handling requests (every website has it's own domain). 所有这些网站都使用相同的库来处理请求(每个网站都有自己的域)。 However some of these websites are very simple and can be fully cached. 但是,其中一些网站非常简单,可以完全缓存。 What I would like to do is enable a flag in a project/website (certain header) that tells varnish to cache the request once delivered. 我想要做的是在项目/网站(特定标题)中启用一个标志,告诉varnish在交付后缓存请求。 Is such a construction possible because I don't want to edit the VCL for every project that can be fully cached and add an entry to vcl_fetch like: 这样的构造是否可行,因为我不想为每个可以完全缓存的项目编辑VCL,并向vcl_fetch添加一个条目,如:

if (req.http.host ~ "<website>")
{
    unset req.http.cookie;
    return (lookup)
}

Is there a proper way to do this? 有没有正确的方法来做到这一点? I did look at the Varnish flowchart but can't come up with a good solution. 我确实看过Varnish流程图,但无法找到一个好的解决方案。

Thanks in advance! 提前致谢!

I hope that this can help you. 我希望这可以帮到你。 In the example below, a custom header is used as conditional. 在下面的示例中,自定义标头用作条件。

sub vcl_fetch {
    if (req.http.Custom-Header == "www.site.com" {
       set beresp.ttl = [...]
       [...]
     }
     elsif (req.http.Custom-Header == "www.site2.com" {
       set beresp.ttl = [...] 
      }
      else {
      [...]
      }
      return(deliver);
}

Well I just started using Varnish and liked the idea. 好吧,我刚开始使用Varnish并喜欢这个主意。 Because well I have the trouble that I only want to cache a couple of domains and don't want to change the vcl all the time. 因为我有麻烦,我只想缓存几个域,并且不想一直更改vcl。

I looked into setting a "special" header and then let varnish do the magic. 我研究设置一个“特殊”标题然后让清漆做魔术。

But then I looked into the docs and there is more easy way. 但后来我查看了文档,还有更简单的方法。

header('Cache-Control: public, max-age=10');

This way varnish caches the content 10 seconds. 这种方式清漆将内容缓存10秒。 So if you would like to cache it for ever then you would come close by using a really high integer. 因此,如果您想永久缓存它,那么您可以使用一个非常高的整数。

// Caches the content for a year, if my calculations are right :P
header('Cache-Control: public, max-age=' . (60 * 60 * 24 * 365));

Varnish will honor the TTL expressed by the backend in the response headers. Varnish将尊重响应头中后端表示的TTL。 If you want site X to be cached, use mod_expires (or similar) and set TTLs correctly from the backend. 如果要缓存站点X,请使用mod_expires(或类似)并从后端正确设置TTL。 If you want site Y not to be cached, set Cache-Control: s-maxage=0 and Varnish will not cache it. 如果您希望不缓存站点Y,请设置Cache-Control: s-maxage=0 ,Varnish不会缓存它。

If you must have a specific response header, here is some example VCL: 如果您必须有一个特定的响应头,这里有一些示例VCL:

  sub vcl_fetch {
       if (beresp.http.x-do-not-cache) {
           set beresp.ttl = 0s;
       }
  }

Note that I'm not doing return() here. 请注意,我在这里没有做return()。 By setting the TTL and falling through to the default VCL Varnish will handle this by itself. 通过设置TTL并降至默认VCL Varnish将自行处理。

Varnish, by default, without changing any VCL, reads the HTTP 1.1 standard cache headers returned by the back end (Cache-Control, Expires, etc) and caches the object according to those headers. 默认情况下,Varnish在不更改任何VCL的情况下读取后端返回的HTTP 1.1标准缓存标头(Cache-Control,Expires等),并根据这些标头缓存对象。 So as long as you return (lookup); 所以只要你return (lookup); in vcl_recv, Varnish is already configured to do what you want it to. 在vcl_recv中,Varnish已经配置为执行您想要的操作。

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

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