varnish配置详解及实例

varnish 配置实例:


backend web1 {

    .host = "*.*.*.*";

    .port = "8080";

    .connect_timeout = 1s;

    .first_byte_timeout = 5s;

    .between_bytes_timeout = 2s;

}

backend web2 {

    .host = "127.0.0.1";

    .port = "8080";

    .connect_timeout = 1s;

    .first_byte_timeout = 5s;

    .between_bytes_timeout = 2s;

}

director server round-robin {

    {.backend=web1;}

    {.backend=web2;}

}

acl purge {  

 "localhost";  

 "127.0.0.1";  

 "10.0.0.0"/8;  

}

sub vcl_recv {

    set req.backend = server;

    if (req.request == "PURGE") {  

     if (!client.ip ~ purge) {  

     error 405 "Not allowed.";  

     }

     return (lookup);

    }

     if (req.restarts == 0) {

  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.request != "GET" &&

       req.request != "HEAD" &&

       req.request != "PUT" &&

       req.request != "POST" &&

       req.request != "TRACE" &&

       req.request != "OPTIONS" &&

       req.request != "DELETE") {

         /* Non-RFC2616 or CONNECT which is weird. */

         return (pipe);

     }

 #开启压缩模式,图片格式取消压缩

if (req.http.Accept-Encoding) {

    if (req.url ~ "\.(jpg|png|gif|jpeg|flv)" ) {

        remove req.http.Accept-Encoding;

        remove req.http.Cookie;

    } else if (req.http.Accept-Encoding ~ "gzip") {

        set req.http.Accept-Encoding = "gzip";

    } else if (req.http.Accept-Encoding ~ "deflate") {

        set req.http.Accept-Encoding = "deflate";

    } else {

        remove req.http.Accept-Encoding;

}

}

     if (req.request != "GET" && req.request != "HEAD") {

         /* We only deal with GET and HEAD by default */

         return (pass);

     }

     if (req.http.Authorization || req.http.Cookie) {

         /* Not cacheable by default */

         return (pass);

     }

     return (lookup);

    #移除一些特定格式的cookie  

    if (req.url ~ "^(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz|js|css|html|htm)($|\?)" ) {  

         #移除cookie,以便能缓存到varnish  

         unset req.http.cookie;  

    }  

 }

 

 sub vcl_pipe {

     return (pipe);

 }

 

 sub vcl_pass {

     return (pass);

 }

 sub vcl_hash {

     hash_data(req.url);

     if (req.http.host) {

         hash_data(req.http.host);

     } else {

         hash_data(server.ip);

     }

     return (hash);

 }

 

 sub vcl_hit {

if (req.request == "PURGE") {

    purge;

    error 200 "Purged.";

  }

     return (deliver);

 }

 

 sub vcl_miss {

#更新缓存的请求,提示缓存不存在 Not in cache

  if (req.request == "PURGE") {

    error 404 "Not in cache.";

  }

  #既然缓存不存在了,就去后端取吧

     return (fetch);

 }

 

 sub vcl_fetch {

if (req.request == "GET" && req.url ~ "\.(gif|jpg|swf|css|png|jpg|jpeg|gif|png|tiff|tif|svg|swf|ico|doc|ppt|pps|xls|odc|odb|odf|odg|odi|odp|ods|odt|sxc|sxd|sxi|sxw|txt|mp3|html|fnt|jsp)") {

    remove beresp.http.Set-Cookie;

    set beresp.ttl = 1 d;

  } else if (req.request == "GET" && req.url ~ "\.(css|js|gzjs|json)") {

    remove beresp.http.Set-Cookie;

    set beresp.ttl = 120 s;

  }

     if (beresp.ttl <= 0s ||

         beresp.http.Set-Cookie ||

         beresp.http.Vary == "*") {

  /*

   * Mark as "Hit-For-Pass" for the next 2 minutes

   */

  set beresp.ttl = 120 s;

  return (hit_for_pass);

     }

     return (deliver);

 }

 

 sub vcl_deliver {

  if (obj.hits > 0) {

    set resp.http.X-Cache = "HIT-h5-izhangxin";

    #统计命中了多少次

    set resp.http.X-Cache-Hits = obj.hits;

  } else {

    set resp.http.X-Cache = "MISS-h5-izhangxin";

  }

  #去掉 varnish 中的一些头信息(如果你不想保留的话,建议保留 Age,方便查看)

  remove resp.http.X-Varnish;

  remove resp.http.Via;

  return (deliver);

}

 

 sub vcl_error {

     set obj.http.Content-Type = "text/html; charset=utf-8";

     set obj.http.Retry-After = "5";

     synthetic {"

 <?xml version="1.0" encoding="utf-8"?>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 <html>

   <head>

     <title>"} + obj.status + " " + obj.response + {"</title>

   </head>

   <body>

     <h1>Error "} + obj.status + " " + obj.response + {"</h1>

     <p>"} + obj.response + {"</p>

     <h3>Guru Meditation:</h3>

     <p>XID: "} + req.xid + {"</p>

     <hr>

     <p>Varnish cache server</p>

   </body>

 </html>

 "};

     return (deliver);

 }

 

 sub vcl_init {

  return (ok);

 }

 

 sub vcl_fini {

  return (ok);

 }

9cf7059c7e989a140d001314222250bb_r.jpg


本文转自 蔡小赵 51CTO博客,原文链接:http://blog.51cto.com/zhaopeiyan/1830299


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>