popopanda
popopanda

Reputation: 381

Varnish cache only a specific url path

Im trying to use varnish to cache 2 websites:

  1. http://services.gradle.org
  2. http://nodejs.org/dist/

I have 2 DNS entries configured: nodejs-proxy and gradle-proxy

In my browser, if I visit http://gradle-proxy, I get redirected to the varnish cached website for services.gradle.org. So this part works.

However, how can I configure it so my varnish machine only caches and display http://nodejs.org/dist/ when I go to http://nodejs-proxy?

my default.vcl configuration is:

backend gradle {
    .host = "207.223.250.8";
    .port = "80";
    .connect_timeout = 6000s;
    .first_byte_timeout = 6000s;
    .between_bytes_timeout = 6000s;
}

sub vcl_recv {
    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.

   if (req.http.host == "gradle-proxy.corp.appdynamics.com") {
      set req.backend_hint = gradle;
   }

    if (! req.url ~ "^/dist/") {
        return(pass);
    }
    return(lookup);

}

Thank you

Upvotes: 1

Views: 4067

Answers (1)

popopanda
popopanda

Reputation: 381

Ah, I got this working by rm:

if (! req.url ~ "^/dist/") {
    return(pass);
}
return(lookup);

and adding:

sub vcl_recv {
    if (req.http.host == "nodejs-proxy") {
            set req.http.host = "nodejs.org";
            set req.url = regsub(req.url, "^", "/dist");
    }
}

backend nodejs {
    .host = "165.225.133.150";
    .port = "80";
    .connect_timeout = 6000s;
    .first_byte_timeout = 6000s;
    .between_bytes_timeout = 6000s;
}

Upvotes: 2

Related Questions