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


相关文章
|
5月前
|
缓存 负载均衡 应用服务中间件
nginx.conf 配置解析及常用配置
nginx.conf 配置解析及常用配置
142 6
|
7月前
|
存储 缓存 运维
LAMP+Varnish缓存详解(一)——Varnish简介
LAMP+Varnish缓存详解(一)——Varnish简介
73 0
|
存储 缓存 Java
缓存varnish的管理及配置详解
一 工作原理 在当前主流的Web服务架构体系中,Cache担任着越来越重要的作用。常见的基于浏览器的C/S架构,Web Cache更是节约服务器资源的关键。而最近几年由FreeBSD创始人之一Kamp开发的varnish更是一个不可多得的Web Cache Server。
1778 0
|
应用服务中间件 nginx
Nginx 常用配置(学习笔记三)
#drop_sql为防爬虫、SQL注入等常用配置 user www www; worker_processes 1; error_log  /usr/local/local/logs/nginx_error.
1199 0
|
缓存 负载均衡 Unix
Nginx 配置详解(学习笔记二)
转载:http://www.cnblogs.com/crazylqy/p/7149774.html 一、 Nginx配置文件nginx.conf中文详解 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数。
1103 0
|
Web App开发 存储 缓存
|
存储 缓存 监控
|
存储 缓存 安全