readme changelog updated, stable tag updated 1.4.3
[varnish-caching-wordpress-plugin.git] / varnish-conf / v4 / lib / purge.vcl
Razvan Stanga 1 vcl 4.0;
2 # purge.vcl -- Cache Purge Library for Varnish
3
4 # Regex purging
5 # Treat the request URL as a regular expression.
6 sub purge_regex {
7 ban("obj.http.X-VC-Req-URL ~ " + req.url + " && obj.http.X-VC-Req-Host == " + req.http.host);
8 }
9
10 # Exact purging
11 # Use the exact request URL (including any query params)
12 sub purge_exact {
13 ban("obj.http.X-VC-Req-URL == " + req.url + " && obj.http.X-VC-Req-Host == " + req.http.host);
14 }
15
16 # Page purging (default)
17 # Use the exact request URL, but ignore any query params
18 sub purge_page {
19 set req.url = regsub(req.url, "\?.*$", "");
20 ban("obj.http.X-VC-Req-URL-Base == " + req.url + " && obj.http.X-VC-Req-Host == " + req.http.host);
21 }
22
23 # The purge behavior can be controlled with the X-VC-Purge-Method header.
24 #
25 # Setting the X-VC-Purge-Method header to contain "regex" or "exact" will use
26 # those respective behaviors. Any other value for the X-Purge header will
27 # use the default ("page") behavior.
28 #
29 # The X-VC-Purge-Method header is not case-sensitive.
30 #
31 # If no X-VC-Purge-Method header is set, the request url is inspected to attempt
32 # a best guess as to what purge behavior is expected. This should work for
33 # most cases, although if you want to guarantee some behavior you should
34 # always set the X-VC-Purge-Method header.
35
Razvan Stanga 36 sub vcl_recv {
Razvan Stanga 37 if (req.method == "PURGE") {
Razvan Stanga 38 if (req.http.X-VC-Purge-Key == "ff93c3cb929cee86901c7eefc8088e9511c005492c6502a930360c02221cf8f4") {
39 set req.http.X-VC-Purge-Key-Auth = "true";
40 } else {
41 set req.http.X-VC-Purge-Key-Auth = "false";
42 }
43 if (client.ip !~ purge && req.http.X-VC-Purge-Key-Auth != "true") {
Razvan Stanga 44 return (synth(405, "Not allowed from " + client.ip));
45 }
46
47 if (req.http.X-VC-Purge-Method) {
48 if (req.http.X-VC-Purge-Method ~ "(?i)regex") {
49 call purge_regex;
50 } elsif (req.http.X-VC-Purge-Method ~ "(?i)exact") {
51 call purge_exact;
52 } else {
53 call purge_page;
54 }
55 } else {
Razvan Stanga 56 # No X-VC-Purge-Method header was specified.
57 # Do our best to figure out which one they want.
58 if (req.url ~ "\.\*" || req.url ~ "^\^" || req.url ~ "\$$" || req.url ~ "\\[.?*+^$|()]") {
59 call purge_regex;
60 } elsif (req.url ~ "\?") {
61 call purge_exact;
62 } else {
63 call purge_page;
64 }
65 }
Razvan Stanga 66 return (synth(200,"Purged " + req.url + " " + req.http.host));
67 }
Razvan Stanga 68 }
69
70 sub vcl_backend_response {
71 set beresp.http.X-VC-Req-Host = bereq.http.host;
72 set beresp.http.X-VC-Req-URL = bereq.url;
73 set beresp.http.X-VC-Req-URL-Base = regsub(bereq.url, "\?.*$", "");
Razvan Stanga 74 }
75
76 sub vcl_deliver {
77 unset resp.http.X-VC-Req-Host;
78 unset resp.http.X-VC-Req-URL;
79 unset resp.http.X-VC-Req-URL-Base;
80
81 if (obj.hits > 0) {
82 set resp.http.X-VC-Cache = "HIT";
83 } else {
84 set resp.http.X-VC-Cache = "MISS";
85 }
86
87 if (resp.http.X-VC-Debug ~ "true") {
88 set resp.http.X-VC-Hash = req.url+"#"+req.http.host;
89 } else {
90 unset resp.http.X-VC-Enabled;
Razvan Stanga 91 unset resp.http.X-VC-Cache;
Razvan Stanga 92 unset resp.http.X-VC-Debug;
Razvan Stanga 93 unset resp.http.X-VC-Cacheable;
94 unset resp.http.X-VC-Purge-Key-Auth;
95 unset resp.http.X-VC-TTL;
96 unset resp.http.X-VC-GotSession;
97 unset resp.http.X-VC-GotUrl;
Razvan Stanga 98 }
99 }
Razvan Stanga 100
comments