Set Different TTL based on website

James Pearson james at ifixit.com
Wed Jan 29 22:33:19 CET 2014


Excerpts from Carlo Alberto Scaglia's message of 2014-01-29 03:22:14 -0800:
> Hi All,
> 
> i'm trying to set different TTLs for different sites which are running on
> the same backend of my Varnish servers, this is how I tried to implement it:
> 
> - Create different directors for sites (e.g Site1_PRD, Site2_PRD,
> Site1_STG, Site2_STG)
> 
> - In sub choose_vhost I set the req.backend based on URL:
> 
>          if ((req.http.host ~ "(?i)^prd.site1.com$") |) ) {  set
> req.backend = Site1_PRD; }
> 
> - In sub vcl_fetch I inserted an IF statement to set TTLs:
> 
>        sub vcl_fetch {
> if ( req.backend == Site1_PRD ) {
> if (req.url !~ "\.aspx" && req.url !~ "\.htmx" && req.url !~ "\.ashx") {
> unset beresp.http.Set-Cookie; }
>                         ...
>        } else { ....
> 
> The problem is that it seems that this causes a lot of random 503 errors,
> while backend is working properly and it is also very difficult to manage
> because for every new site I need to nest another IF in the root else
> statement (and I think this cause an increased computational load).
> Is there a better way to configure different TTLs per site?

The best way is to configure your applications to send back proper cache
control headers, but I understand this isn't always feasible.

Would it help to set the ttl in a variable (that is, a temporary header), then
assign it later in one block?

    if site1:
	     ttl = 5
	 else if site2:
	     ttl = 10
	 else:
	     ttl = 15
    
	 if long and complex:
		  response.ttl = ttl

 - P



More information about the varnish-misc mailing list