varnish中忽略cookie进行缓存

varnish不缓存cookie的页面,如果html页面中带有cookie
以下代码为接收到结尾的文件,自动去除掉cookie
sub vcl_recv {
    if (req.request == ”GET” && req.url ~ ”.(js|css|html|jpg|png|gif|swf|jpeg| ico)$”) {

     unset req.http.cookie;

    }
}

对于php的session页面,以下配置可以强制缓存
sub vcl_recv {
    unset req.http.Cookie;
    return (lookup);
}
sub vcl_fetch {
    unset beresp.http.Set-Cookie;
    return(deliver);
}


同时需要在php页面中设置session使用缓存
session_cache_limiter('');
header('Cache-Control: public, s-maxage=60');
session_start();


以下为详细配置

#cache config
backend default {
     .host = "192.168.1.110";
     .port = "1300";
}

sub vcl_recv{
  #clear cookie

   if (req.http.x-forwarded-for) {
       set req.http.X-Forwarded-For =
       req.http.X-Forwarded-For ", " client.ip;
   } else {
       set req.http.X-Forwarded-For = client.ip;
   }

   if(req.url ~ "/Index/verify$" || req.url ~ "/Index/logindo"){

  }else{

        if( req.http.Cookie ~"authkey" ){

  }else{
      unset req.http.cookie;
      return (lookup);
  }
    }

}

sub vcl_deliver {
        set resp.http.x-hits = obj.hits ;

        if (obj.hits > 0) {
                set resp.http.X-Cache = "HIT read.easouu.com";
        }else {
                set resp.http.X-Cache = "MISS read.easou.com";
        }

}

sub vcl_fetch{
    #clear no-cache

 if(req.url ~ "/Index/verify$" || req.url ~ "/Index/logindo"){
 
  }else{
 
   if( req.http.Cookie ~"authkey" ){
 
   }else{
       unset req.http.cookie;
       return (deliver);
   }
 
 
 }
}

原文地址:https://www.cnblogs.com/itfenqing/p/4429445.html