/
plone.vcl.in
226 lines (194 loc) · 5.62 KB
/
plone.vcl.in
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
vcl 4.0;
# This is a basic VCL configuration file for varnish. See the vcl(7)
# man page for details on VCL syntax and semantics.
# Non-default backend definition. Port set to instance's http-address.
backend instance1 {
.host = "localhost";
.port = "8010";
.connect_timeout = 0.4s;
.first_byte_timeout = 300s;
.between_bytes_timeout = 60s;
.probe = {
.url = "/";
.timeout = 5s;
.interval = 15s;
.window = 10;
.threshold = 8;
}
}
backend instance2 {
.host = "localhost";
.port = "8020";
.connect_timeout = 0.4s;
.first_byte_timeout = 300s;
.between_bytes_timeout = 60s;
.probe = {
.url = "/";
.timeout = 5s;
.interval = 15s;
.window = 10;
.threshold = 8;
}
}
backend sorryserver {
.host = "localhost";
.port = "80";
}
import directors;
sub vcl_init {
new vdir = directors.round_robin();
vdir.add_backend(instance1);
vdir.add_backend(instance2);
}
acl purge {
"localhost";
}
import std;
sub vcl_recv {
set req.backend_hint = vdir.backend();
if (! std.healthy(req.backend_hint)) {
set req.backend_hint = sorryserver;
return(pass);
}
if (req.method == "PURGE") {
# Not from an allowed IP? Then die with an error.
if (!client.ip ~ purge) {
return (synth(405, "This IP is not allowed to send PURGE requests."));
}
return(purge);
}
if (req.method == "BAN") {
if (!client.ip ~ purge) {
return(synth(403, "Not allowed."));
}
#ban("req.url ~ " + req.url);
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"));
}
# Only deal with "normal" types
if (req.method != "GET" &&
req.method != "HEAD" &&
req.method != "PUT" &&
req.method != "POST" &&
req.method != "TRACE" &&
req.method != "OPTIONS" &&
req.method != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return(pipe);
}
# Only cache GET or HEAD requests. This makes sure the POST requests are always passed.
if (req.method != "GET" && req.method != "HEAD") {
return(pass);
}
if (req.http.Expect) {
return(pipe);
}
if (req.http.If-None-Match && !req.http.If-Modified-Since) {
return(pass);
}
/* Do not cache other authorized content by default */
if (req.http.Authenticate || req.http.Authorization) {
return(pass);
}
if (req.url ~ "^[^?]*\.(mp3,mp4|rar|tar|tgz|gz|wav|zip)(\?.*)?$") {
return(pipe);
}
return(hash);
}
sub vcl_pass {
return (fetch);
}
sub vcl_purge {
return (synth(200, "Purged"));
}
sub vcl_hit {
if (obj.ttl >= 0s) {
# normal hit
return (deliver);
}
# backend is healthy – lets look at stale candidate.
if (std.healthy(req.backend_hint)) {
# Backend is healthy.
if (obj.ttl > 0s) {
set req.http.grace = "normal(limited)";
return (deliver);
} else {
# No candidate for grace - fetch a fresh object.
return(fetch);
}
} else {
# backend is sick - use full grace
// Object is in grace, deliver it
// Automatically triggers a background fetch
if (obj.ttl + obj.grace > 0s) {
set req.http.grace = "full";
return (deliver);
} else {
# no graced object.
return (fetch);
}
}
if (req.method == "PURGE") {
set req.method = "GET";
set req.http.X-purger = "Purged";
return(synth(200, "Purged. in hit " + req.url));
}
return (fetch);
}
sub vcl_miss {
if (req.method == "PURGE") {
set req.method = "GET";
set req.http.X-purger = "Purged-possibly";
return(synth(200, "Purged. in miss " + req.url));
}
return (fetch);
}
sub vcl_backend_fetch{
return (fetch);
}
sub vcl_deliver {
set resp.http.grace = req.http.grace;
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
/* Rewrite s-maxage to exclude from intermediary proxies
(to cache *everywhere*, just use 'max-age' token in the response to avoid
this override) */
if (resp.http.Cache-Control ~ "s-maxage") {
set resp.http.Cache-Control = regsub(resp.http.Cache-Control, "s-maxage=[0-9]+", "s-maxage=0");
}
/* Remove proxy-revalidate for intermediary proxies */
if (resp.http.Cache-Control ~ ", proxy-revalidate") {
set resp.http.Cache-Control = regsub(resp.http.Cache-Control, ", proxy-revalidate", "");
}
}
/*
We can come here "invisibly" with the following errors: 413, 417 & 503
*/
sub vcl_synth {
set resp.http.Content-Type = "text/html; charset=utf-8";
set resp.http.Retry-After = "5";
synthetic( {"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Varnish cache server: "} + resp.status + " " + resp.reason + {" </title>
</head>
<body>
<h1>Error "} + resp.status + " " + resp.reason + {"</h1>
<p>"} + resp.reason + {"</p>
<h3>Guru Meditation:</h3>
<p>XID: "} + req.xid + {"</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
"} );
return (deliver);
}