A Coder

Coding My Dream!

0%

使用varnish对typecho博客加速的 vcl 配置

backend default {
.host = “127.0.0.1”;
.port = “88”;
}

sub vcl_recv {
        #匹配get方式且cookies中不存在uid,即非管理员登陆(管理登陆后看到的不是缓存后的)
        if((req.request == "GET"||req.request == "HEAD")&&!(req.http.Cookie && req.http.Cookie ~ "uid")){
            #匹配相应的文件后缀,排除 admin 后台管理目录
            if (req.url ~ "\.(js|css|html|jpg|png|gif|swf|jpeg|ico|mp3)$" && !req.url ~ "/admin/") {
                #去除 cookies 
                unset req.http.cookie;
            }

            #去除首页的 Cookies
            if (req.url ~ "^/$") {
                unset req.http.cookie;
            }
        }
}

sub vcl_fetch {

        #如果是POST方式请求  则清空所有缓存,POST请求一般会对网站内容进行修改 
        #这个不是很准确  可以匹配 评论地址 和  发布文章地址
        if(req.request == "POST"){
            //清空指定网址下面的所有缓存
            ban("req.http.host == " + req.http.host);
        }

        #设置根目录和html 结尾的文件 缓存时间
        if(req.request == "GET"||req.request == "HEAD"){
            if (req.url ~ "^/$"||req.url ~ "html$") {
                    set beresp.ttl = 1d;
            }
         }

}

缓存结果可以查看header部分的age情况