ashreil
ashreil

Reputation: 49

How to tell if a response from the backend originated in a Varnish cache lookup

Is there a way to tell, at a point in the varnish subroutines when you have write access to the response object, whether the object was passed to the backend directly, or first sent through a cache lookup? Right now I am fiddling around with adding application logic to receive some header and then send it back, which can be read by varnish, but I would prefer to have the varnish behaviour be a bit more application independent.

What I'm looking for would be something like the below, though the method where I //DoSomeStuff doesn't have to be the deliver.

sub vcl_recv {  
 if( req.url ~ "^/something/ignored.*$" ) {
   return ( pass );
 }
 else {  
    unset req.http.Cookie;
    return( hash );
  }
}
sub vcl_deliver {
 if( resp.lookup == 1 ) {
   //Do Some Stuff
 }    
}

Upvotes: 0

Views: 310

Answers (2)

Nils Goroll
Nils Goroll

Reputation: 168

In more recent varnish-cache versions, bereq.uncacheable gives you the answer.

Upvotes: 0

Jorge Nerín
Jorge Nerín

Reputation: 766

Yes, there multiple ways. you can hook in vcl_hash{} and add some custom header there in req., you can do the same in vcl_pass{}, you can do that one step before in vcl_recv{}, or one step after in vcl_hit{} and vcl_miss{} (note that a hit-for-pass calls vcl_pass{} also)

Look up the processing states to get your picture clearer.

Upvotes: 0

Related Questions