0

我想使用 Varnish 4.0.3。
并且我想划分缓存过程是否cookie具有特定的键和值。

当用户访问我的页面时,如果他们的浏览器有“loged_in=true”cookie,我不想缓存该页面。此外,如果它没有 cookie,我想缓存 mypage。

但是这两种设置都不起作用。
它根本不缓存。

另外,当用户进入“类别”页面时,清漆会正确缓存页面。

这是我的 default.vcl。
有谁告诉我我怎么了?

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";
    }
}
4

0 回答 0