-
Notifications
You must be signed in to change notification settings - Fork 8
/
default.vcl
332 lines (248 loc) · 8.55 KB
/
default.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
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
#<?php
backend master_only { .host = "172.20.2.227"; .connect_timeout = 600s; .first_byte_timeout = 600s; .between_bytes_timeout = 600s; }
director www_director random {
.retries = 50;
# main server
{ .backend = { .host = "172.20.2.227"; .connect_timeout = 600s; .first_byte_timeout = 600s; .between_bytes_timeout = 600s; } .weight = 40; }
# server 2
{ .backend = { .host = "172.20.2.106"; .connect_timeout = 600s; .first_byte_timeout = 600s; .between_bytes_timeout = 600s; } .weight = 60; }
}
sub vcl_recv {
if (req.request == "PURGE") {
error 405 "Not allowed.";
}
# set standard proxied ip header for getting original remote address
set req.http.X-Forwarded-For = client.ip;
set req.grace = 30m;
### NORMALIZE
# Cleanup URLs like /iframe=true&width=90%&height=90%
if(req.url ~ "iframe=true(.*)$") {
set req.url = regsub(req.url,"iframe=true(.*)$","");
error 301;
}
# Normalize the url - first remove any hashtags (shouldn't make it to the server anyway, but just in case)
if (req.url ~ "\#") {
set req.url=regsub(req.url,"\#.*$","");
}
# Strip out Google Analytics campaign variables. They are only needed
# by the javascript running on the page
# utm_source, utm_medium, utm_campaign, gclid
if(req.url ~ "(\?|&)(gclid|utm_[a-z]+)=") {
set req.url = regsuball(req.url, "(gclid|utm_[a-z]+)=[-_A-z0-9]+&?", "");
set req.url = regsub(req.url, "(\?|&)$", "");
}
# remove awesm referrers from query string
if(req.url ~ "(\?|&)awesm=") {
set req.url = regsub(req.url, "\?.*$", "");
}
# remove startIndex from query string
# not sure what this does
if(req.url ~ "(\?|&)startIndex=") {
set req.url = regsub(req.url, "\?.*$", "");
}
# remove fb_xd_fragment from query string
if(req.url ~ "\?fb_xd_fragment") {
set req.url = regsub(req.url, "\?.*$", "");
}
# remove comment-page-1 from query string
# this is the same page as the post page
if(req.url ~ "comment-page-1") {
set req.url = regsub(req.url, "comment-page-1", "");
}
# remove "?: " from query string
# EXAMPLE : http://thenextweb.com/apps/2011/09/12/photo-academy-an-all-in-one-photography-guide-on-your-iphone/?%3A+TheNextWeb+%28The+Next+Web+All+Stories%29
if(req.url ~ "\?esi$") {
} elsif(
req.url ~ "\?:"
|| req.url ~ "\?\:"
|| req.url ~ "\?\%3A"
|| req.url ~ "\?_="
|| req.url ~ "\?all=1"
|| req.url ~ "\?switch="
|| req.url ~ "\?r=contact"
|| req.url ~ "\?r=register_kennisland"
|| req.url ~ "\?utmsource="
|| req.url ~ "\?utm_term="
|| req.url ~ "\?awid="
|| req.url ~ "\?uid="
|| req.url ~ "\?asid="
|| req.url ~ "\?=depth"
|| req.url ~ "(\?|&)replytocom="
|| req.url ~ "\?wptouch_page_template="
|| req.url ~ "\?amp"
|| req.url ~ "\?sess_id"
|| req.url ~ "\? Web social media"
|| req.url ~ "\?audioselect="
|| req.url ~ "\?stream&"
|| req.url ~ "\?\%2Bbutton&\%2Bmedia"
|| req.url ~ "\?\+button"
|| req.url ~ "\?volt="
|| req.url ~ "\?pushpress=hub"
|| req.url ~ "The\+Next\+Web\+Top\+Stories"
|| req.url ~ "The\+Next\+Web\+Google"
|| req.url ~ "The\+Next\+Web\+Shareables"
|| req.url ~ "The\+Next\+Web\+Appetite"
|| req.url ~ "The\+Next\+Web\+Apps"
|| req.url ~ "\?\.com"
|| req.url ~ "\?\.it"
|| req.url ~ "\?campaign="
|| req.url ~ "\?action=com_google"
) {
set req.url = regsub(req.url, "\?.*$", "");
}
# remove double // in urls,
set req.url = regsuball( req.url, "//", "/" );
// remove extra http:// calls at the end of the url
if (req.url ~ "^/\?http://") {
set req.url = regsub(req.url, "\?http://.*", "");
}
### END NORMALIZE
# Normalize Content-Encoding
if (req.http.Accept-Encoding)
{
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|lzma|tbz)(\?.*|)$") {
remove req.http.Accept-Encoding;
} else if (req.url ~ "\.(js|css|txt|html|htm)(\?.*|)$") {
# text files - do compression
if (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
remove req.http.Accept-Encoding;
}
}
}
# Make sure all static files are server from the main server, so no nfs share is needed to all slave machines to share the images
if (req.url ~ "^/wp-(login|admin)" || req.url ~ "^/[^?]+/wp-(login|admin)" req.url ~ "^/podcast" || req.url ~ "^/cronjobs" || req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif)(\?.*|)$") {
set req.backend = master_only;
}
else {
set req.backend = www_director;
}
# never cache twitter oauth login unless it are images
if (req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?.*|)$") {
# Remove cookies and query string for real static files
unset req.http.cookie;
}
else if( req.url ~ "^/register" || req.url ~ "livefyre=" || req.url ~ "/real-auth" || req.http.Cookie ~ "wptouch-pro-view"){
return (pass);
}
if( req.url ~ "oauth_token" || req.url ~ "twc_req_key"){
set req.backend = master_only;
return (pass);
}
# File type that we will always cache
if (req.request == "GET" && req.url ~ "\.(gif|jpg|swf|css|js|png|jpg|jpeg|gif|png|tiff|tif|svg|swf|ico|css|js|vsd|doc|ppt|pps|xls|pdf|mp3|mp4|m4a|ogg|mov|avi|wmv|sxw|zip|gz|bz2|tgz|tar|rar|odc|odb|odf|odg|odi|odp|ods|odt|sxc|sxd|sxi|sxw|dmg|torrent|deb|msi|iso|rpm)$") {
return(lookup);
}
# logged in users must always pass
# Removed req.http.Cookie ~ "wordpress_logged_in_"
if(req.url ~ "\?esi$") {
} else if( req.url ~ "^/[^?]+/wp-(login|admin)" || req.url ~ "^/wp-(login|admin)" || req.url ~ "preview=true") {
return (pass);
}
# Resize images
if( req.url ~ "\.png\?s=60" ){
}
elsif( req.url ~ "\?s="){
# don't cache search results
return (pass);
}
# always pass through posted requests and those with basic auth
if ( req.request == "POST" || req.http.Authorization ) {
return (pass);
}
# else ok to fetch a cached page
unset req.http.Cookie;
return (lookup);
}
sub vcl_fetch {
# Serve items up to half an hour past their expire time
set beresp.grace = 30m;
set beresp.ttl = 48h;
# cache ESI requests for 5 minutes
if (req.url ~ "\?esi$") {
set beresp.ttl = 5m;
}
# remove some headers we never want to see
unset beresp.http.Server;
unset beresp.http.X-Powered-By;
unset beresp.http.x-backend;
# Enable ESI mode in varnish based on backend responce headers
if (beresp.http.esi-enabled == "1") {
set beresp.do_esi = true;
#unset beresp.http.esi-enabled;
}
# You are respecting the Cache-Control=private header from the backend
if (beresp.http.Cache-Control ~ "private") {
set beresp.http.X-Cacheable = "NO:Cache-Control=private";
# You are extending the lifetime of the object artificially
} elsif (beresp.ttl < 1s) {
set beresp.ttl = 5s;
set beresp.grace = 5s;
set beresp.http.X-Cacheable = "YES:FORCED";
# Varnish determined the object was cacheable
} else {
set beresp.http.X-Cacheable = "YES";
}
# don't cache response to posted requests or those with basic auth
if ( req.request == "POST" || req.http.Authorization ) {
return (hit_for_pass);
}
# cache 404s and 301s for 1 minute
if (beresp.status == 404 || beresp.status == 500 || beresp.status == 301 || beresp.status == 302) {
set beresp.ttl = 1m;
return (deliver);
}
# any other request except 200
if ( beresp.status != 200) {
return (hit_for_pass);
}
# else ok to cache the response
#set beresp.http.X-expires = beresp.ttl;
return (deliver);
}
sub vcl_deliver {
# add debugging headers, so we can see what's cached
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
}
else {
set resp.http.X-Cache = "MISS";
}
# remove some headers added by varnish
unset resp.http.Via;
unset resp.http.X-Varnish;
}
sub vcl_hash {
# convert request in hash for cache lookup
hash_data(req.url);
# altering hash so subdomains are ignored.
# don't do this if you actually run different sites on different subdomains
if (req.http.host) {
hash_data(req.http.host);
}
# MOBILE
# ensure separate cache for mobile clients (WPTouch workaround)
if( req.http.User-Agent ~ "(?i)(iphone|ipod|aspen|incognito|webmate|android|dream|cupcake|froyo|blackberry|webos|samsung|bada|IEMobile|htc|Maemo|Fennec)" ){
hash_data("touch");
} else { # We're AB-ing only the desktop site
if( req.http.X-AB ) {
hash_data(req.http.X-AB);
}
}
return (hash);
}
sub vcl_error {
# retry connecting to apache 3 times before giving up
if (obj.status == 503 && req.restarts < 2) {
set obj.http.X-Restarts = req.restarts;
return(restart);
}
if (obj.status == 301) {
set obj.http.Location = req.url;
set obj.status = 301;
return(deliver);
}
}