Categories
Sys Admin

Varnish cache

Varnish has been updated to version 4 in Arch (for a while now, actually…). I use varnish mostly to speed up my wordpress instances. Unfortunately however, I couldn’t find any good VCL sample for wordpress and varnish 4. It’s funny that varnish’s website still has a few samples of VCLs for wordpress and varnish 3. It’s beyond me why a company would not want to help people to use their latest products (although they provide a pretty good guide to update VCL 3.0 to VCL 4.0 for those willing to dig deeper).but in any case. I updated an old VCL file I had to VCL 4.0 and I’m sharing it with you.

By no means I’m a varnish expert and there may be bugs in this VCL, but feel free to use it and please let me know if you find anything wrong with it.

# /etc/varnish/default.vcl
vcl 4.0;

backend default {
    .host = "127.0.0.1";
    .port = "8080";
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
}

acl purge {
        "localhost";
        "192.168.0.0"/16;
}

sub vcl_recv {
        if (req.method == "PURGE") {
                if (!client.ip ~ purge) {
                        return(synth(405,"Not allowed."));
                }
                return (purge);
        }
}

sub vcl_hit {
        if (req.method == "PURGE") {
                if (!client.ip ~ purge) {
                        return(synth(403, "Not allowed."));
                }
                ban("req.http.host == " + req.http.host +
                      " && req.url == " + req.url);
                return(synth(200, "Purged"));        }
}

sub vcl_miss {
        if (req.method == "PURGE") {
                if (!client.ip ~ purge) {
                        return(synth(403, "Not allowed."));
                }
                ban("req.http.host == " + req.http.host +
                      " && req.url == " + req.url);
                return(synth(200, "Purged"));        }
}


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.