varnish-4.x VCL之强制cache miss

varnish-4.x VCL之强制cache miss



sub vcl_recv {
    set req.hash_always_miss = true; 
}
最前面的某段vcl_recv写入 set req.hash_always_miss = true,即启用强制cache miss
这样varnish会把所有的请求都miss掉,直接丢到后端的web server,将最新的内容返回给客户端并缓存一份最新的副本到varnish本地,注意,仅仅只是缓存,给客户端的永远最最新的内容
注意: 因为缓存多个副本,随着时间的推移,消耗内存也会逐渐增加,调试时可以用到

vcl 4.0;

import std;
import directors;

probe healthcheck_nginx {
    .url = "/healthy.jpg";
    .timeout = 3s;
    .interval = 5s;
    .window = 5;
    .threshold = 3;
}
probe healthcheck_apache {
    .url = "/healthy.jpg";
    .timeout = 3s;
    .interval = 5s;
    .window = 5;
    .threshold = 3;
}
backend nginx {
    .host = "192.168.192.10";
    .port = "8080";
    .connect_timeout = 3s;
    .first_byte_timeout = 5s;
    .between_bytes_timeout = 5s;
    .max_connections = 30000;
    .probe = healthcheck_nginx;
}
backend apache {
    .host = "192.168.192.10";
    .port = "8081";
    .connect_timeout = 3s;
    .first_byte_timeout = 5s;
    .between_bytes_timeout = 5s;
    .max_connections = 3000;
    .probe = healthcheck_apache;
}

sub vcl_init {
    new round_robin_director = directors.round_robin();
    round_robin_director.add_backend(apache);
    round_robin_director.add_backend(nginx);

    new random_director = directors.random();
    random_director.add_backend(nginx, 10);
    random_director.add_backend(apache, 5);

    new hash_director = directors.hash();
    hash_director.add_backend(nginx, 10);
    hash_director.add_backend(apache, 5);
}

sub vcl_recv {
    set req.hash_always_miss = true; 
}

sub vcl_recv {
    if (req.method == "PRI") {
       
        return (synth(405));
    }
    if (req.method != "GET" &&
        req.method != "HEAD" &&
        req.method != "PUT" &&
        req.method != "POST" &&
        req.method != "TRACE" &&
        req.method != "OPTIONS" &&
        req.method != "DELETE") {
           
            return (pipe);
    }
    if (req.method != "GET" && req.method != "HEAD") {
       
        return (pass);
    }
    if (req.http.Authorization || req.http.Cookie) {
       
        return (pass);
    }
    return (hash);
}

acl purgers {
    "127.0.0.1";
    "192.168.0.0"/24;
}

sub vcl_recv {
    # allow PURGE from localhost and 192.168.0...
    if (req.restarts == 0) {
        unset req.http.X-Purger;
    }
    if (req.method == "PURGE") {
        if (!client.ip ~ purgers) {
            return (synth(405, "Purging not allowed for " + client.ip));
        }
        return (purge);
    }
    #set req.backend_hint = round_robin_director.backend();
    set req.backend_hint = hash_director.backend(req.http.cookie);
}

sub vcl_purge {
    set req.method = "GET";
    set req.http.X-Purger = "Purged";
    return (restart);
}

sub vcl_deliver {
    if (req.http.X-Purger) {
        set resp.http.X-Purger = req.http.X-Purger;
    }
}

posted @ 2016-04-14 13:37  李庆喜  阅读(203)  评论(0编辑  收藏  举报