Varnish 缓存了‘MISS 状态’对象吗?

Varnish 缓存了‘MISS 状态’对象吗?

我的网站使用 nginx、varnish、jboss。某些 URL 会被 varnish 缓存,这取决于 jboss 的响应标头。

第一次,jboss 告诉 varnish 不要缓存这个 url。然后第二次请求,jboss 告诉 varnish 缓存,但 varnish 不会缓存它。我使用 varnishstat 发现 1 个对象缓存在 Varnish 中,那是 'MISS status' 对象吗?

我删除了grace代码,问题仍然存在。当我删除PURGE这个 URL 时,Varnish 工作正常,然后缓存该 URL。但我无法每次启动时清除这么多 URL,我该如何解决这个问题?

配置:

acl local {
    "localhost";
}

backend default {
    .host = "localhost";
    .port = "8080";
    .probe = {
        .url = "/preload.htm";
        .interval = 3s;
        .timeout = 1s;
        .window = 5;
        .threshold = 3;
    }
}

sub vcl_deliver {
    if (req.request == "PURGE") {
        remove resp.http.X-Varnish;
        remove resp.http.Via;
        remove resp.http.Age;
        remove resp.http.Content-Type;
        remove resp.http.Server;
        remove resp.http.Date;
        remove resp.http.Accept-Ranges;
        remove resp.http.Connection;
        set resp.http.keeplive="true";
    } else {
        if (obj.hits > 0) {
            set resp.http.X-Cache = "HIT";
        } else {
            set resp.http.X-Cache = "MISS";
        }
    }
}

sub vcl_recv {
    if(req.url ~ "/check.htm"){
         error 404 "N";
    }
    if( req.http.host ~ "store." || req.request == "POST"){
        return (pipe);
    }
    if (req.backend.healthy) {
         set req.grace = 30s;
    } else {
         set req.grace = 10m;
    }

    set req.http.x-cacheKey = "0";
    if(req.url ~ "/shop/view_shop.htm" || req.url ~ "/shop/viewShop.htm" || req.url ~ "/index.htm"){
        if(req.url ~ "search=y"){
                set req.http.x-cacheKey = req.http.host + "/search.htm";
        }else if(req.url !~ "bbs=y" && req.url !~ "shopIntro=y" && req.url !~ "shop_intro=y"){
                set req.http.x-cacheKey = req.http.host + "/index.htm";
        }
    }else if(req.url ~ "/search"){
        set req.http.x-cacheKey = req.http.host + "/search.htm";
    }

    if( req.http.x-cacheKey == "0" && req.url !~ "/i/"){
        return (pipe);
    }

    if (req.request == "PURGE") {
        if (client.ip ~ local) {
            return (lookup);
        } else {
            error 405 "Not allowed.";
        }
    }

    if (req.url ~ "/i/") {
        set req.http.x-shop-url = req.original_url;
    }else {
        unset req.http.cookie;
    }
}

sub vcl_fetch {
    set beresp.grace = 10m;
    #unset beresp.http.x-cacheKey;
    if (req.url ~ "/i/" || req.url ~ "status" ){
        set beresp.ttl = 0s;      /* ttl=0 for dynamic content */
    } else if(beresp.http.x-varnish-cache != "1"){
        set beresp.do_esi = true; /* Do ESI processing */
        set beresp.ttl = 0s;
        unset beresp.http.set-cookie;
    } else {
        set beresp.do_esi = true; /* Do ESI processing */
        set beresp.ttl = 1800s;
        unset beresp.http.set-cookie;
    }
}

sub vcl_hash {
    hash_data(req.http.x-cacheKey);
    return (hash);
}

sub vcl_error {
    if (req.request == "PURGE") {
        return (deliver);
    } else {
        set obj.http.Content-Type = "text/html; charset=gbk";
        synthetic {"<!--ve-->"};
        return (deliver);
    }
}

sub vcl_hit {
    if (req.request == "PURGE") {
        set obj.ttl = 0s;
        error 200 "Purged.";
    }
}

sub vcl_miss {
    if (req.request == "PURGE") {
        error 404 "N";
    }
}

相关内容