侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

高命中率的varnish缓存配置分享

技术  /  管理员 发布于 7年前   245

给大家一个连我这么丑的blog,命中率都可以达到75%的varnish配置,大家拿去根据自己网站情况再优化下的话,说不定也可以达到90%,这也不是不可能的事。

系统:centos 5.x
软件:varnish-3.0.5

1.安装varnish

怎么安装我就不说了吧,自己搜去。

2.varnish配置


复制代码 代码如下:
  backend slogra {
     .host = "172.0.0.1";
     .port = "80";
     .connect_timeout = 20s;
     .first_byte_timeout = 20s;
     .between_bytes_timeout = 20s;
 }
 
#允许刷新缓存的规则
acl purgeAllow {
#     只能本机进行刷新
     "localhost";
}
 
# Below is a commented-out copy of the default VCL logic.  If you
# redefine any of these subroutines, the built-in logic will be
# appended to your code.
 
sub vcl_recv {
    #判断请求主机,跳转到相应后端服务器
    if(req.http.host ~ "^(.*)(slogra.com)")
    {
        set req.backend=slogra;
    }else{
        error 408 "Hostname not found"; 
    }
    
    #grace缓存过期仍存放
    # 若backend是健康的,则仅grace 5s,如果backend不健康,则grace 1m。
    # 这里,5s的目的是为了提高高并发时的吞吐率;
    # 1m的目的是,backend挂了之后,还能继续服务一段时间,期望backend挂的不要太久。。。
    if (req.backend.healthy) {
        set req.grace = 5s;
    } else {
        set req.grace = 1m;
    }
 
    #刷新缓存的处理
    if (req.request == "PURGE"){
        if(!client.ip ~ purgeAllow) {
                error 405 "Not allowed.";
        }
    #    #转到hit或者miss处理
        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;
    }
 
   #Accept-Encoding 是浏览器发给服务器,声明浏览器支持的编码类型的
   #修正客户端的Accept-Encoding头信息
   #防止个别浏览器发送类似 deflate, gzip
    if (req.http.Accept-Encoding) {
        if (req.url ~ "^(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz)($|\?)" ) {
            remove req.http.Accept-Encoding;
        }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 if (req.http.Accept-Encoding ~ "sdch"){
            #chrome新增加的压缩
            set req.http.Accept-Encoding = "sdch";
        }else {
            remove req.http.Accept-Encoding;
        }
    }        
    #首次访问增加X-Forwarded-For头信息,方便后端程序获取客户端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;
        }
    }
 
   if (req.request != "GET" &&
       req.request != "HEAD" &&
       req.request != "PUT" &&
       req.request != "POST" &&
       req.request != "TRACE" &&
       req.request != "OPTIONS" &&
       req.request != "DELETE") {
       return (pipe);
   }
     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);
     }
     #js,css文件都有Cookie,不能每次都去后台服务器去取
     #if (req.http.Cookie) {
     #    /* Not cacheable by default */
     #    return (pass);
     #}
    
     #如果请求的是动态页面直接转发到后端服务器
     if (req.url ~ "^(.*)\.(php|jsp|do|aspx|asmx|ashx)($|.*)") {
          return (pass);
     }
     return (lookup);
 }   
 sub vcl_pipe {
     # Note that only the first request to the backend will have
     # X-Forwarded-For set.  If you use X-Forwarded-For and want to
     # have it set for all requests, make sure to have:
     # set bereq.http.connection = "close";
     # here.  It is not set by default as it might break some broken web
     # applications, like IIS with NTLM authentication.
     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);
     }
     #支持压缩的要增加,防止发送给不支持压缩的浏览器压缩的内容
     if(req.http.Accept-Encoding){
          hash_data(req.http.Accept-Encoding);
     }
     return (hash);
 }
 
#缓存服务器lookup查找命中:hit
 sub vcl_hit {
     #刷新缓存的请求操作,设置TTL为0,返回处理结果代码
     if (req.request == "PURGE") {
          set obj.ttl = 0s;
          error 200 "Purged.";
      }  
     #缓存服务器命中后(查找到了)
     return (deliver);
 }   
#缓存服务器lookup查找没有命中:miss
sub vcl_miss {
    #刷新缓存的请求操作,
    #if (req.request == "PURGE") {
    #    error 404 "Not in cache.";
    #}  
    #缓存服务器没有命中(去后台服务器取)
     return (fetch);
 }  
#从后台服务器取回数据后,视情况是否进行缓存
sub vcl_fetch {
    #如果请求的是动态页面直接发转发
    #动态请求回来的,一定要放在前面处理
    if (req.url ~ "^(.*)\.(php|jsp|do|aspx|asmx|ashx)($|.*)") {
        set beresp.http.Cache-Control="no-cache, no-store";
        unset beresp.http.Expires;
        return (deliver);
    }  
    # 仅当该请求可以缓存时,才设置beresp.grace,若该请求不能被缓存,则不设置beresp.grace
    if (beresp.ttl > 0s) {
        set beresp.grace = 1m;
    }    
     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;
            #下次请求时不进行lookup,直接pass
            return (hit_for_pass);
     }  
    #设置从后台服务器获得的特定格式文件的缓存TTL
    if (req.url ~ "^(.*)\.(pdf|xls|ppt|doc|docx|xlsx|pptx|chm|rar|zip)($|\?)")     
    {
        #移除服务器发送的cookie 
        unset beresp.http.Set-Cookie;
        #加上缓存时间
        set beresp.ttl = 30d;
        return (deliver);
    }else if(req.url ~ "^(.*)\.(bmp|jpeg|jpg|png|gif|svg|png|ico|txt|css|js|html|htm)($|\?)"){
        #移除服务器发送的cookie 
        unset beresp.http.Set-Cookie;
        #加上缓存时间
        set beresp.ttl = 15d;
        return (deliver);
    }else if(req.url ~ "^(.*)\.(mp3|wma|mp4|rmvb|ogg|mov|avi|wmv|mpeg|mpg|dat|3pg|swf|flv|asf)($|\?)"){
        #移除服务器发送的cookie 
        unset beresp.http.Set-Cookie;
        #加上缓存时间
        set beresp.ttl = 30d;
        return (deliver);
    }  
    #从后台服务器返回的response信息中,没有缓存的,不缓存
    if (beresp.http.Pragma ~"no-cache" || beresp.http.Cache-Control ~"no-cache" || beresp.http.Cache-Control ~"private") {
            return (deliver);
    }
    return (deliver);
 }   
#缓存服务器发送到客户端前调用
 sub vcl_deliver {
    #下面是添加一个Header标识,以判断缓存是否命中。
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT from cache";
       #set resp.http.X-Varnish = "HIT from cache";
    } else {
        set resp.http.X-Cache = "MISS from cache";
       #set resp.http.X-Varnish = "MISS from cache";
    }
    #去掉不是必须的header
    unset resp.http.Vary;
    unset resp.http.X-Powered-By;
    unset resp.http.X-AspNet-Version;
    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>cache server</p>
   </body>
 </html>
 "};
     return (deliver);
 }   
 sub vcl_init {
    return (ok);
 }  
 sub vcl_fini {
    return (ok);
 }

3.效果图

好了,大家有兴趣的,可以自己去搞.


  • 上一条:
    squid3缓存服务器编译安装和高命中率配置示例
    下一条:
    外贸网站屏蔽中国IP访问的多种方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客