Varnish: purge if I have cookie in hash_data

纵饮孤独 提交于 2020-01-05 03:33:48

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/41065044/varnish-purge-if-i-have-cookie-in-hash-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!