forked from dreamhost/varnish-vcl-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
purge.vcl
93 lines (82 loc) · 2.89 KB
/
purge.vcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# purge.vcl -- Cache Purge Library for Varnish
#
# Copyright (C) 2013 DreamHost (New Dream Network, LLC)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# There are 3 possible behaviors of purging.
# Regex purging
# Treat the request URL as a regular expression.
sub purge_regex {
ban("obj.http.X-Req-URL ~ " + req.url + " && obj.http.X-Req-Host == " + req.http.host);
}
# Exact purging
# Use the exact request URL (including any query params)
sub purge_exact {
ban("obj.http.X-Req-URL == " + req.url + " && obj.http.X-Req-Host == " + req.http.host);
}
# Page purging (default)
# Use the exact request URL, but ignore any query params
sub purge_page {
set req.url = regsub(req.url, "\?.*$", "");
ban("obj.http.X-Req-URL-Base == " + req.url + " && obj.http.X-Req-Host == " + req.http.host);
}
# The purge behavior can be controlled with the X-Purge-Method header.
#
# Setting the X-Purge-Method header to contain "regex" or "exact" will use
# those respective behaviors. Any other value for the X-Purge header will
# use the default ("page") behavior.
#
# The X-Purge-Method header is not case-sensitive.
#
# If no X-Purge-Method header is set, the request url is inspected to attempt
# a best guess as to what purge behavior is expected. This should work for
# most cases, although if you want to guarantee some behavior you should
# always set the X-Purge-Method header.
sub vcl_recv {
if (req.method == "PURGE") {
if (client.ip !~ purge) {
return(synth(405, "Not allowed."));
}
if (req.http.X-Purge-Method) {
if (req.http.X-Purge-Method ~ "(?i)regex") {
call purge_regex;
} elsif (req.http.X-Purge-Method ~ "(?i)exact") {
call purge_exact;
} else {
call purge_page;
}
} else {
# No X-Purge-Method header was specified.
# Do our best to figure out which one they want.
if (req.url ~ "\.\*" || req.url ~ "^\^" || req.url ~ "\$$" || req.url ~ "\\[.?*+^$|()]") {
call purge_regex;
} elsif (req.url ~ "\?") {
call purge_exact;
} else {
call purge_page;
}
}
return(synth(200, "Purged."));
}
}
sub vcl_backend_response {
set beresp.http.X-Req-Host = bereq.http.host;
set beresp.http.X-Req-URL = bereq.url;
set beresp.http.X-Req-URL-Base = regsub(bereq.url, "\?.*$", "");
}
sub vcl_deliver {
unset resp.http.X-Req-Host;
unset resp.http.X-Req-URL;
unset resp.http.X-Req-URL-Base;
}