forked from egeland/puppet-varnish_incomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvarnish_lb_with_cache_example.vcl.README
More file actions
360 lines (284 loc) · 8.79 KB
/
varnish_lb_with_cache_example.vcl.README
File metadata and controls
360 lines (284 loc) · 8.79 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# Example config for:
# Every PHP SessID gets an own cache
# Re-Open the same site with same PHP SessID during 10s will be answered from varnish cache. Only first request will be go to the backend
#
#
backend fe0_ff {
.host = "10.28.30.100";
.port = "80";
.probe = {
.url = "/index2.php";
.timeout = 1s;
.interval = 1s;
.window = 10;
.threshold = 3;
}
}
backend fe1_ff {
.host = "10.28.30.101";
.port = "80";
.probe = {
.url = "/index2.php";
.timeout = 1s;
.interval = 1s;
.window = 10;
.threshold = 3;
}
}
director default_director round-robin {
{ .backend = fe0_ff; }
{ .backend = fe1_ff; }
}
#https://www.varnish-cache.org/trac/wiki/VCLExampleNormalizeUserAgent
sub normalize_user_agent {
if (req.http.user-agent ~ "MSIE") {
set req.http.X-UA = "msie";
} else if (req.http.user-agent ~ "Firefox") {
set req.http.X-UA = "firefox";
} else if (req.http.user-agent ~ "Safari") {
set req.http.X-UA = "safari";
} else if (req.http.user-agent ~ "Opera Mini/") {
set req.http.X-UA = "opera-mini";
} else if (req.http.user-agent ~ "Opera Mobi/") {
set req.http.X-UA = "opera-mobile";
} else if (req.http.user-agent ~ "Opera") {
set req.http.X-UA = "opera";
} else {
set req.http.X-UA = "nomatch";
}
}
sub vcl_recv {
set req.backend = default_director;
if (req.restarts == 0) {
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For + ", " + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
}
# Use anonymous, cached pages if all backends are down.
if (!req.backend.healthy) {
unset req.http.Cookie;
set req.grace = 1d;
} else {
set req.grace = 10m;
}
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return(pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return(pass);
}
/* pipeline post requests trac #4124 https://www.varnish-cache.org/trac/ticket/849 */
if (req.request == "POST") {
return(pipe);
}
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
error 200 "Purged.";
return (lookup);
}
if (req.request == "BAN") {
ban("req.http.host == " + req.http.host +
"&& req.url == " + req.url);
error 200 "Ban added";
}
if (req.http.Cache-Control ~ "no-cache") {
if (! (req.http.Via || req.http.User-Agent ~ "bot|MSIE")) {
ban (req.url);
}
}
call normalize_user_agent;
# @see: http:// varnish.projects.linpro.no/wiki/FAQ/Compression
if (req.http.Accept-Encoding) {
if (req.http.Accept-Encoding ~ "gzip") {
# If the browser supports it, we'll use gzip.
set req.http.Accept-Encoding = "gzip";
}
else if (req.http.Accept-Encoding ~ "deflate") {
# Next, try deflate if it is supported.
set req.http.Accept-Encoding = "deflate";
}
else {
# Unknown algorithm. Remove it and send unencoded.
unset req.http.Accept-Encoding;
}
}
if (req.url ~ "(?i)\.(js|txt|gz|zip|lzma|bz2|tgz|tbz|png|gif|jpeg|jpg|ico|swf|css|flv|mp3|mp4|pdf)(\?[a-z0-9]+)?$") {
set req.url = regsub(req.url, "\?.*$", "");
unset req.http.cookie;
unset req.http.Cache-Control;
unset req.http.Max-Age;
unset req.http.Pragma;
}
if (req.http.Authorization || req.http.Authenticate){
return (pass);
}
return(lookup);
}
sub vcl_pipe {
/* Force the connection to be closed afterwards so subsequent reqs don't use pipe */
set bereq.http.connection = "close";
# This is not necessary if you do not do any request rewriting.
set req.http.connection = "close";
}
sub vcl_pass {
return (pass);
}
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
}
if (req.http.cookie) {
hash_data(req.http.cookie);
} else {
hash_data(server.ip);
}
return (hash);
}
sub vcl_hit {
if (req.http.Cache-Control ~ "no-cache") {
if (! (req.http.Via || req.http.User-Agent ~ "bot|MSIE")) {
set obj.ttl = 0s;
return (restart);
}
}
if (obj.ttl<=0s){
return(pass);
}
if (req.request == "PURGE") {
purge;
error 200 "Purged.";
}
return(deliver);
}
sub vcl_miss {
if (req.request == "PURGE") {
purge;
error 200 "Purged.";
}
return (fetch);
}
## Fetch
sub vcl_fetch {
# Allow items to be stale if needed.
set beresp.grace = 10d;
set beresp.ttl = 10s;
set beresp.do_esi = true;
if (beresp.ttl <= 0s ||
beresp.http.Set-Cookie ||
beresp.http.Vary == "*") {
set beresp.ttl = 10 s;
return (hit_for_pass);
}
#https://www.varnish-cache.org/trac/wiki/VCLExampleSaintMode
if (beresp.status == 500) {
set beresp.saintmode = 20s;
if (req.request != "POST") {
return(restart);
} else {
error 500 "Failed";
}
}
if (beresp.status == 404) {
# Cache 404 responses for 15 seconds
error 404;
set beresp.http.Cache-Control = "max-age=15";
set beresp.ttl = 15s;
set beresp.grace = 15s;
}
/* if (beresp.status == 503) {
error 200;
} */
#Respect force-reload, and clear cache accordingly. This means that a ctrl-reload will acutally purge
# the cache for this URL.
if (req.http.Cache-Control ~ "no-cache") {
set beresp.ttl = 0s;
#Make sure ESI includes are processed!
set beresp.do_esi = true;
remove beresp.http.Cache-Control;
remove beresp.http.Expires;
remove beresp.http.Last-Modified;
remove beresp.http.ETag;
remove beresp.http.Pragma;
return (deliver);
}
if (beresp.http.content-type ~ "(css|text|html|json|x-javascript|xml|javascript|rss+xml|atom+xml|rdf+xml)") {
set beresp.do_gzip = true;
}
if (req.http.host ~ "^(www\.)?cablecarproductions\.de$") {
remove beresp.http.Cache-Control;
remove beresp.http.Expires;
remove beresp.http.Last-Modified;
remove beresp.http.ETag;
remove beresp.http.Pragma;
}
# Don't allow static files to set cookies.
if (req.url ~ "(?i)\.(js|txt|gz|zip|lzma|bz2|tgz|tbz|png|gif|jpeg|jpg|ico|swf|css|flv|mp3|mp4|pdf)(\?[a-z0-9]+)?$") {
unset beresp.http.set-cookie;
unset beresp.http.expires;
set beresp.ttl = 7d;
set beresp.http.magicmarker = "1";
}
if (beresp.ttl <= 0s) {
set beresp.http.X-Cacheable = "NO:Not Cacheable";
} elsif (req.http.Cookie ~ "(UserID|_session)") {
set beresp.http.X-Cacheable = "NO:Got Session";
return(hit_for_pass);
} elsif (req.http.Cookie ~ ".*TYPO3_FE_USER_LOGGED_IN=1.*") {
set beresp.http.X-Cacheable = "NO, FRONTEND USER LOGGED IN";
} elsif (beresp.http.Cache-Control ~ "private") {
set beresp.http.X-Cacheable = "NO:Cache-Control=private";
return(hit_for_pass);
} elsif (beresp.ttl < 1s) {
set beresp.ttl = 5s;
set beresp.grace = 5s;
set beresp.http.X-Cacheable = "YES:FORCED";
} else {
set beresp.http.X-Cacheable = "YES";
}
if ( req.request == "POST" || req.http.Authorization ) {
return (hit_for_pass);
}
unset beresp.http.Server;
set beresp.http.Server = "Apache";
return(deliver);
}
## Deliver
sub vcl_deliver {
if (resp.http.magicmarker) {
unset resp.http.magicmarker;
unset resp.http.Server;
set resp.http.Cache-Control = "public,max-age=604800";
set resp.http.Age = "0";
set resp.http.X-Origin= "cache";
set resp.http.Vary= "Accept-Encoding";
}
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
set resp.http.X-Cache-Hits = obj.hits;
} else {
set resp.http.X-Cache = "MISS";
}
}
sub vcl_error {
return(deliver);
}
sub vcl_init {
return (ok);
}
sub vcl_fini {
return (ok);
}