0

I want to use the Varnish 4.0.3.
And I want to divide the caching process whether cookie has specific key and value.

When user comes to mypage, if their browser has "loged_in=true" cookie, I do NOT want to cache the page. Also if it doesn't have the cookie, I want to cache the mypage.

But both settings are not working.
It doesn't cache at all.

Additionaly, when user comes to the "category" page, the varnish caches the page correctly.

Here is my default.vcl.
Does anyone tell me what's wrong with me ?

vcl 4.0;
import directors;

backend ap1 {
  .host = "192.168.0.1";
  .port = "80";
  .first_byte_timeout = 200s;
  .probe = {
         .url = "/";
         .interval = 5s;
         .timeout = 1 s;
         .window = 5;
         .threshold = 3;
         .initial = 1;
  }
}

backend ap2 {
  .host = "192.168.0.2";
  .port = "80";
  .first_byte_timeout = 200s;
  .probe = {
         .url = "/";
         .interval = 5s;
         .timeout = 1 s;
         .window = 5;
         .threshold = 3;
         .initial = 1;
  }
}

sub vcl_init{
  new ws_hash = directors.hash();
  ws_hash.add_backend(ap1, 1.0);
  ws_hash.add_backend(ap2, 1.0);
  new ws_rand  = directors.random();
  ws_rand.add_backend(ap1, 1.0);
  ws_rand.add_backend(ap2, 1.0);
}

sub vcl_recv{
  if( (req.url ~ "(category)") ) {
    // Cache
    set req.backend_hint = ws_hash.backend(req.url + ":" + req.http.host);
    return(hash);
  }

  if( (req.url ~ "(mypage)") ) {
    if (req.http.Cookie ~ "(loged_in=true)" ) {
      // NO Cache
      set req.backend_hint = ws_rand.backend();
      return(pass);
    }

    // Cache
    set req.backend_hint = ws_hash.backend(req.url + ":" + req.http.host);
    return(hash);
  }

  // NO Cache
  set req.backend_hint = ws_rand.backend();
  return(pass);
}

sub vcl_deliver {
    if (obj.hits > 0) {
       set resp.http.V-Cache = "HIT";
    } else {
       set resp.http.V-Cache = "MISS";
    }
}
k16
  • 481
  • 2
  • 6
  • 18
  • Does [this](http://stackoverflow.com/questions/19058660/varnish-how-to-separately-cache-pages-based-on-value-of-a-specific-cookie) help? – Redithion Dec 14 '15 at 13:44
  • you should check varnishlog, I'm quite sure you'll have hit-for-pass. Where is this cookie loged_in being set? – Cesc Feb 17 '16 at 04:57

0 Answers0