Varnish Purge - Ban, custom hash

Lane, Richard rlane at ahbelo.com
Mon Oct 28 20:34:02 CET 2013


I am using multiple hash_data calls to customize the hash key. I need to
cache potentially three different version of  the page.

My problem is how do I purge the items. I have tried implementing purge; in
recv, hit, miss functions like Varnish docs show but still not working.

I have tried implementing ban using a regex or == on the URL.

Below is my VCL hash function. How do I purge a URL that is hashed with
key...

"/mysection/mystory.htmlwww.example.com_preview_mobile"

I had this working prior to Varnish 3 but seems to have stopped working
after that.

##VCL Hash function below

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) {
    #    set req.hash += req.http.Accept-Encoding;
    #}

    ## /* Normalize Accept-Encoding to reduce effects of Vary:
Accept-Encoding
    ## (cf. http://varnish-cache.org/wiki/FAQ/Compression)
    ## Also note that Vary: User-Agent is considered harmful */
    if (req.http.Accept-Encoding) {
       if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
         // Don't compress already-compressed files
         remove req.http.Accept-Encoding;
       }
       elsif (req.http.Accept-Encoding ~ "gzip") {
         set req.http.Accept-Encoding = "gzip";
       }
       elsif (req.http.Accept-Encoding ~ "deflate") {
         set req.http.Accept-Encoding = "deflate";
       }
       else {
         // unknown algorithm
         remove req.http.Accept-Encoding;
       }
    }

    if(req.http.x-preview == "yes" && (req.url ~ ".shtml"  || req.url ~
".html") )
    {
        hash_data("_preview");
        ##Add device view to hash
        hash_data("_" + req.http.x-preferred-device);

    }else{

        ##Add device view to hash
        hash_data("_" + req.http.x-device);
    }
    return (hash);
}

#Purge pieces
vcl_recv {

    if (req.request == "PURGE") {
                if (!client.ip ~ localaddr) {
                    error 405 "Method not allowed";
                }
              ban("obj.http.x-url ~ " + req.url + " && obj.http.x-host == "
+ req.http.host + " && obj.http.x-pd ~ .*" );
              std.log("Purge : yes really purge.");
              return(lookup);
         }

vcl_hit {
        if (req.request == "PURGE") {
                purge;
                error 200 "Purged.";
        }
}

vcl_miss {
        if (req.request == "PURGE") {
                purge;
                error 200 "Purged.";
        }

 vcl_fetch {
      #setup for banning
     set beresp.http.x-url = req.url;
     set beresp.http.x-host = req.http.host;
     set beresp.http.x-pd = req.http.x-device;

  }

 vcl_deliver {
   #Clean up banning
    unset resp.http.x-url;
    unset resp.http.x-host;
    unset resp.http.x-pd;

 }

Thanks in advance,

Richard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20131028/fbb14b02/attachment.html>


More information about the varnish-misc mailing list