0
votes

I am using varnish 4.0.3 as revers proxy caching and load balancer.
I want to avoid varnish caching for links that start with /api/v1/ or any link that contains feed in its link and to serve the request from the backend servers directly. I have done this:

sub vcl_recv {
    if ((req.url ~ "^/api/v1/" || req.url ~ "feed") &&
        req.http.host ~ "api.example.com") {
        set req.backend_hint = apis.backend();
    }

But based on access log, it serves the first request from Backend and then it serves the new requests from varnish directly! have i done anything wrong? or is there anything else i need to do?

1

1 Answers

1
votes

It should be:

sub vcl_recv {
    if ((req.url ~ "^/api/v1/" || req.url ~ "feed") 
      && req.http.host == "api.example.com") {
        return (pass);
    }
}

The return (pass) will switch Varnish to pass mode for the matching requests. In pass mode, Varnish will neither put result to cache, nor deliver from cache (always talks to backend).

A micro-optimisation of a kind is matching req.http.host using == operator. Regex matching not really needed in this case.