Varnish cached 'MISS status' object?

Posted by Hesey on Server Fault See other posts from Server Fault or by Hesey
Published on 2012-10-20T12:00:35Z Indexed on 2012/10/21 5:09 UTC
Read the original article Hit count: 506

Filed under:
|
|

My site uses nginx, varnish, jboss. And some url will be cached by varnish, it depends a response header from jboss.

The first time, jboss tells varnish doesn't cache this url. Then the second request, jboss tells varnish to cache, but varnish won't cache it. I used varnishstat and found that 1 object is cached in Varnish, is that the 'MISS status' object?

I remove grace code and the problem still exists. When I PURGE this url, varnish works fine and cache the url then. But I can't PURGE so much urls every startup time, how can I fix this?

The configuration:

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";
    }
}

© Server Fault or respective owner

Related posts about reverse-proxy

Related posts about cache