Different TTLs dependent upon req.http.host?

Peter Hinse loco at d0pefish.de
Mon May 4 14:17:16 CEST 2009


Peter Hinse schrieb:
> Hi all,
> 
> I have a varnish config looking roughly like this:
> 
> backend be1 {
>   .host = "10.10.10.10";
>   .port = "80";
> }
> 
> sub vcl_recv {
>     elseif (req.http.host ~ "^x.example.com") {
>         set req.http.Host = "host.example.com";
>         set req.backend = be1;
>     }
>     elseif (req.http.host ~ "^xi.example.com") {
>         set req.http.Host = "host.example.com";
>         set req.backend = be1;
>     }
> }
> 
> The backend server delivers dynamically rendered images I need to cache.
> If the original requests comes in for Host x.example.com, the TTL can be
>  12h, if the request is for xi.example.com, the TTL is 15m.
> 
> Since I cannot use "req.http.host" in vcl_fetch and the req.url look the
> same, how can I distinguish the two cases and set the obj.ttl differently?

What seems to work (at least the VCL compiles without errors):

backend be1 {
  .host = "10.10.10.10";
  .port = "80";
}

sub vcl_recv {
    elseif (req.http.host ~ "^x.example.com") {
        set req.http.Host = "host.example.com";
	set req.http.myhost = "x.example.com";
        set req.backend = be1;
    }
    elseif (req.http.host ~ "^xi.example.com") {
        set req.http.Host = "host.example.com";
	set req.http.myhost = "xi.example.com";
        set req.backend = be1;
    }
}

Now I can use the following part in vcl_fetch:

if (req.request == "GET" && req.http.myhost == "x.example.com") {
	set obj.ttl = 12h;
	deliver;
}
elseif (req.request == "GET" && req.http.myhost == "xi.example.com") {
	set obj.ttl = 15m;
	deliver;
}



More information about the varnish-misc mailing list