Varnish: purge if I have cookie in hash_data
Asked Answered
I

1

1

Problem:

I couldn't purge my page. After many time I decided to find out how purge works and find!

As you can see we have used a new action - return(purge). This ends execution of vcl_recv and jumps to vcl_hash. This is just like we handle a regular request. When vcl_hash calls return(lookup) varnish will purge the object and then call vcl_purge. Here you have the option of adding any particular actions you want Varnish to take once it has purge the object. docs

And then I understood that I have cookie in hash_data and I can't purge specific url.

Question:

How can purge all my pages by URI? I think ban system can't help me. Maybe you suggest me something?

Indispose answered 9/12, 2016 at 16:40 Comment(1)
No one can help me or question so stupid?..Indispose
U
2

The Purge method will work only for the specific url that is being requested. It is not possible to use regular expressions with Purge. For example: a request for www.example.com/uri is made and a Purge is called, only the object for this whole URL will be removed from cache. Then, if you want to use Purge you will have to implement the following feature on your VCL:

acl purge {
    "localhost";
    "192.168.55.0"/24;
}
sub vcl_recv {
        ...
        # allow PURGE from localhost and 192.168.55...

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

After that, all you need to do to purge an URL is to send a request with the Purge method to your Varnish like this:

$ curl -X PURGE "www.example.com/desired/uri"

The Ban is used when you want to remove many objects with only one request.This can be accomplished using regular expressions that are not available in Purge. In order to use it you should have a code similar to the one below, maintaining the acl from before:

sub vcl_recv {
        ...
        if (req.method == "BAN") {
                # Same ACL check as above:
                if (!client.ip ~ purge) {
                        return(synth(403, "Not allowed."));
                }
                ban("req.http.host == " + req.http.host +
                      " && req.url ~ " + req.url);

                # Throw a synthetic page so the
                # request won't go to the backend.
                return(synth(200, "Ban added"));
        }
        ...
}

To ban objects is similar to purge but now you can send Regex. The example below shows how to ban all png files from the host www.example.com:

$ curl -X BAN "www.example.com/.png$"

All the information was retrieved from Varnish docs.

I hope this answer can help you understand how Bans and Purges work and that you can identify how they can help you.

If anything is missing or is not addressing your needs, please edit your question to be clearer in your doubts (there is a divergence between the tittle and the question that may lead to confusion) and I'll be happy to edit the answer.

Unmindful answered 12/12, 2016 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.