码迷,mamicode.com
首页 > 其他好文 > 详细

varnishi配置

时间:2016-03-28 16:41:22      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

#定义后端主机
backend xueui {
    #.host = "128.199.245.40";
    #.port = "80";
    .host = "xxx.xxx.xxx.xxx";
    .port = "80";
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
}
backend app_xueui {
    .host = "xxx.xxx.xxx.xxx";
    .port = "80";
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
}
backend blog_0550go {
    .host = "127.0.0.1";
    .port = "8080";
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
}
backend bbs_0550buy {
    .host = "127.0.0.1";
    .port = "8080";
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
}
 
#定义负载均衡(这里没有用到不作配置)
#director lb_test random {
#    {
#      .backend = test1;
#      .weight = 5;
#     }
#     {
#       .backend = test2;
#       .weight = 5;
#     }
#}
#定义清除缓存的权限列表
acl purge {
    "localhost";
    "127.0.0.1";
#    "192.168.100.0"/24;
#    "192.168.0.0"/24;
}
 
#cl_recv函数 用于接受和处理请求,当请求到达成功接收后被调用,通过判断请求的数据来决定如何处理申请
sub vcl_recv {
 
#对www.shencan.net域名进行缓存加速
 
  if (req.http.host ~ "^www.xueui.cn"){
#      set req.http.host = "www.xueui.cn";
      set req.backend = xueui;
  }else
  if (req.http.host ~"^app.xueui.cn"){
#      set req.http.host = "app.xueui.cn";
      set req.backend = app_xueui;
  }else
  if (req.http.host ~"^blog.0550go.com"){
#     set req.http.host = "blog.0550go.com";
      set req.backend = blog_0550go;
  }else
  if (req.http.host ~"^bbs.0550buy.com"){
#      set req.http.host = "bbs.0550buy.com";
      set req.backend = bbs_0550buy;
  }else
  {
    error 408 "Hostname not found"; 
  }  
#如果为purge请求,客户端ip不在访问列表中,返回405拒绝
  if (req.request == "PURGE") {
     if (!client.ip ~purge) {
       error 405 "Not Allowed";
   }
 
#本地缓存查找
   return(lookup);
  }
#如果为GET请求,url后缀为jpg,png,gif等 取出cookie
  if (req.request == "GET"&&req.url ~ ".(html|txt|jpg|png|gif|swf|jpeg|ico|css|mp3|mp4|htm)$") {
        unset req.http.cookie;
  }
 
  if (req.request == "GET" && req.url ~ "^/(tutorials|experience|appreciate|download|ui-tutorial-collection|resume-instruction|category/start|category/index|category/userguide|category/login|category/userprofiles|category/nav|category/图片展示|category/table|category/comment|category/searchbar|category/download|category/details-page|category/feedback|category/settings|category/data|category/about|category/other|game-heng)") {
        unset req.http.cookie;
  }        
#如果GET请求,url为php,则穿过cache,不缓存
  if (req.request =="GET"&&req.url ~ "(?i).php($|?)"){
        return (pass);
  }
#获取客户端ip
#   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;
        }
#   }
#不是以下请求进入pipe模块
    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);
    }
#不是GET 和HEAD及验证请求不缓存
    if (req.request != "GET" && req.request != "HEAD") {
        /* We only deal with GET and HEAD by default */
        return (pass);
    }
    if (req.http.Authorization) {
        /* Not cacheable by default */
        return (pass);
    }
    return (lookup);
 }
 
#下面这部分是pipe函数体
#此函数在进入pipe模式时被调用,用于将请求直接传递至后端主机,在请求
#返回的内容没有改变的情况下,将不变的内容返回给客户端,直到这个链接
#关闭。
sub vcl_pipe {
     return (pipe);
 }
 
#vcl_pass
#此函数在进入pass模式时被调用,用于将请求直接传递至后端主机,后端主机
#应答数据后送给客户端,但不进行任何缓存,在当前连接下每次都返回最新的
#内容
 
sub vcl_pass {
    return (pass);
}
#使用url+host hash算法查找数据
sub vcl_hash {
    hash_data(req.url);
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }
    return (hash);
}
# 如果请求为purge 将清除缓存
sub vcl_hit {
   if (req.request == "PURGE") {
       set obj.ttl = 0s;
       error 200 "Purged";
    }
    return (deliver);
}
 
sub vcl_miss {
    return (fetch);
}
 
#vcl_fetch函数
#在从后端主机更新缓存并且获取内容后调用该方法,接着,通过判断获取的内容来
#决定是否将内容放入缓存,还是直接返回给客户端。
 
sub vcl_fetch {
    if (req.url ~ "html$") {
    set beresp.ttl = 60m;
    set beresp.do_gzip = true;
    unset beresp.http.Cache-Control;
    unset beresp.http.Pragma;
    set beresp.http.Cache-Control = "max-age=3600";
    unset beresp.http.Expires;
   }   
    if (req.url ~ "^/(tutorials|experience|appreciate|download|ui-tutorial-collection|resume-instruction|category/start|category/index|category/userguide|category/login|category/userprofiles|category/nav|category/图片展示|category/table|category/comment|category/searchbar|category/download|category/details-page|category/feedback|category/settings|category/data|category/about|category/other|game-heng)") {
    set beresp.ttl = 60m;
    set beresp.do_gzip = true;
    unset beresp.http.Cache-Control;
    unset beresp.http.Pragma;
    set beresp.http.Cache-Control = "max-age=3600";
    unset beresp.http.Expires;
   }
    if (req.request == "GET" && req.url ~ ".(html|txt|jpg|png|gif|swf|jpeg|ico|css|mp3|mp4|htm)$") {
    set beresp.ttl = 36000s;
   }
    return (deliver);
}
# 设置返回状态
sub vcl_deliver {
     set resp.http.x-hits = obj.hits;
     if (obj.hits > 0) {
      set resp.http.X-Cache = "Hit xueui.cn";
   }else {
       set resp.http.X-Cache = "Miss xueui.cn";
   }
     set resp.http.Server = "jack";
     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);
}

 

varnishi配置

标签:

原文地址:http://www.cnblogs.com/fengjian2016/p/5329525.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!