-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.vcl
More file actions
341 lines (288 loc) · 13.1 KB
/
default.vcl
File metadata and controls
341 lines (288 loc) · 13.1 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
vcl 4.1;
import std;
import directors;
include "config/acl.vcl";
include "config/backends.vcl";
include "config/imageproxy-loader.vcl";
include "vcl/includes/purge.vcl";
include "vcl/includes/static.vcl";
sub vcl_init {
new backends = directors.round_robin();
backends.add_backend(backend1);
}
# ----------------------------------------------------------------------
# vcl_recv
# ----------------------------------------------------------------------
sub vcl_recv {
# -- Backend selection --
set req.backend_hint = backends.backend();
call imageproxy_recv;
# -- X-Forwarded-For: keep only the first (client) IP behind a trusted proxy --
if (req.http.X-Forwarded-For) {
set req.http.X-Forwarded-For = regsub(req.http.X-Forwarded-For, "^([^,]+).*", "\1");
}
call purge_recv;
call static_recv;
# -- Normalize Accept-Encoding to reduce cache fragmentation (br > gzip > none) --
if (req.http.Accept-Encoding) {
if (req.http.Accept-Encoding ~ "br") {
set req.http.Accept-Encoding = "br";
} elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} else {
unset req.http.Accept-Encoding;
}
}
# -- Pipe unknown HTTP methods --
if (
req.method != "GET" &&
req.method != "HEAD" &&
req.method != "PUT" &&
req.method != "POST" &&
req.method != "PATCH" &&
req.method != "TRACE" &&
req.method != "OPTIONS" &&
req.method != "DELETE"
) {
return (pipe);
}
# -- Pipe WebSocket upgrades --
# https://www.varnish-cache.org/docs/4.0/users-guide/vcl-example-websockets.html
if (req.http.Upgrade ~ "(?i)websocket") {
return (pipe);
}
# -- Pass non-cacheable HTTP methods --
if (req.method != "GET" && req.method != "HEAD") {
set req.http.X-Cacheable = "NO:REQUEST-METHOD";
return (pass);
}
# -- Pass special URLs, authenticated users, and plugin-specific paths --
if (
req.http.Cookie ~ "wordpress_(?!test_)[a-zA-Z0-9_]+|wp-postpass|comment_author_[a-zA-Z0-9_]+|woocommerce_cart_hash|woocommerce_items_in_cart|wp_woocommerce_session_[a-zA-Z0-9]+|wordpress_logged_in_|comment_author|PHPSESSID" ||
req.http.Authorization ||
req.url ~ "add_to_cart" ||
req.url ~ "edd_action" ||
req.url ~ "nocache" ||
req.url ~ "^/addons" ||
req.url ~ "^/bb-admin" ||
req.url ~ "^/bb-login.php" ||
req.url ~ "^/bb-reset-password.php" ||
req.url ~ "^/cart" ||
req.url ~ "^/checkout" ||
req.url ~ "^/control.php" ||
req.url ~ "^/login" ||
req.url ~ "^/logout" ||
req.url ~ "^/lost-password" ||
req.url ~ "^/my-account" ||
req.url ~ "^/register" ||
req.url ~ "^/register.php" ||
req.url ~ "^/server-status" ||
req.url ~ "^/signin" ||
req.url ~ "^/signup" ||
req.url ~ "^/stats" ||
req.url ~ "^/wc-api" ||
req.url ~ "^/wp-admin" ||
req.url ~ "^/wp-comments-post.php" ||
req.url ~ "^/wp-cron.php" ||
req.url ~ "^/wp-login.php" ||
req.url ~ "^/wp-activate.php" ||
req.url ~ "^/wp-mail.php" ||
req.url ~ "(\?|&)add-to-cart=" ||
req.url ~ "(\?|&)wc-api=" ||
req.url ~ "(\?|&)preview=" ||
req.url ~ "^/\.well-known/acme-challenge/"
) {
set req.http.X-Cacheable = "NO:Logged in/Got Sessions";
if (req.http.X-Requested-With == "XMLHttpRequest") {
set req.http.X-Cacheable = "NO:Ajax";
}
return (pass);
}
# -- Pass AJAX requests --
if (req.http.X-Requested-With == "XMLHttpRequest") {
set req.http.X-Cacheable = "NO:AJAX";
return (pass);
}
# -- URL cleanup --
set req.url = regsub(req.url, "\?replytocom=.*$", "");
# -- Strip tracking/analytics cookies --
if (req.http.Cookie) {
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(has_js|wooTracker)=[^;]*", "");
set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "_ga=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "_gat=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "utmctr=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "utmcmd.=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "utmccn.=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "__gads=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "__atuv.=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "^;\s*", "");
if (req.http.Cookie ~ "^\s*$") {
unset req.http.Cookie;
}
}
# -- HTTPOXY mitigation (https://httpoxy.org/) --
unset req.http.proxy;
# -- Strip tracking query parameters (origin excluded: used by APIs/CORS) --
if (req.url ~ "(\?|&)(_branch_match_id|_bta_[a-z]+|_bta_c|_bta_tid|_ga|_gl|_ke|_kx|campid|cof|customid|cx|dclid|dm_i|ef_id|epik|fbclid|gad_source|gbraid|gclid|gclsrc|gdffi|gdfms|gdftrk|hsa_acc|hsa_ad|hsa_cam|hsa_grp|hsa_kw|hsa_mt|hsa_net|hsa_src|hsa_tgt|hsa_ver|ie|igshid|irclickid|matomo_campaign|matomo_cid|matomo_content|matomo_group|matomo_keyword|matomo_medium|matomo_placement|matomo_source|mc_[a-z]+|mc_cid|mc_eid|mkcid|mkevt|mkrid|mkwid|msclkid|mtm_campaign|mtm_cid|mtm_content|mtm_group|mtm_keyword|mtm_medium|mtm_placement|mtm_source|nb_klid|ndclid|pcrid|piwik_campaign|piwik_keyword|piwik_kwd|pk_campaign|pk_keyword|pk_kwd|redirect_log_mongo_id|redirect_mongo_id|rtid|s_kwcid|sb_referer_host|sccid|si|siteurl|sms_click|sms_source|sms_uph|srsltid|toolid|trk_contact|trk_module|trk_msg|trk_sid|ttclid|twclid|utm_[a-z]+|utm_campaign|utm_content|utm_creative_format|utm_id|utm_marketing_tactic|utm_medium|utm_source|utm_source_platform|utm_term|vmcid|wbraid|yclid|zanpid)=") {
set req.url = regsuball(req.url, "(_branch_match_id|_bta_[a-z]+|_bta_c|_bta_tid|_ga|_gl|_ke|_kx|campid|cof|customid|cx|dclid|dm_i|ef_id|epik|fbclid|gad_source|gbraid|gclid|gclsrc|gdffi|gdfms|gdftrk|hsa_acc|hsa_ad|hsa_cam|hsa_grp|hsa_kw|hsa_mt|hsa_net|hsa_src|hsa_tgt|hsa_ver|ie|igshid|irclickid|matomo_campaign|matomo_cid|matomo_content|matomo_group|matomo_keyword|matomo_medium|matomo_placement|matomo_source|mc_[a-z]+|mc_cid|mc_eid|mkcid|mkevt|mkrid|mkwid|msclkid|mtm_campaign|mtm_cid|mtm_content|mtm_group|mtm_keyword|mtm_medium|mtm_placement|mtm_source|nb_klid|ndclid|pcrid|piwik_campaign|piwik_keyword|piwik_kwd|pk_campaign|pk_keyword|pk_kwd|redirect_log_mongo_id|redirect_mongo_id|rtid|s_kwcid|sb_referer_host|sccid|si|siteurl|sms_click|sms_source|sms_uph|srsltid|toolid|trk_contact|trk_module|trk_msg|trk_sid|ttclid|twclid|utm_[a-z]+|utm_campaign|utm_content|utm_creative_format|utm_id|utm_marketing_tactic|utm_medium|utm_source|utm_source_platform|utm_term|vmcid|wbraid|yclid|zanpid)=[-_A-Za-z0-9+(){}%.*]+&?", "");
set req.url = regsub(req.url, "[?|&]+$", "");
}
# -- Strip URL fragment (hash) --
if (req.url ~ "\#") {
set req.url = regsub(req.url, "\#.*$", "");
}
# -- Strip trailing empty query string --
if (req.url ~ "\?$") {
set req.url = regsub(req.url, "\?$", "");
}
# -- Strip port number from Host header --
if (req.http.Host) {
set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");
}
# -- Sort query string for cache normalization --
set req.url = std.querysort(req.url);
return (hash);
}
# ----------------------------------------------------------------------
# vcl_backend_fetch
# ----------------------------------------------------------------------
sub vcl_backend_fetch {
call imageproxy_backend_fetch;
}
# ----------------------------------------------------------------------
# vcl_hash
# ----------------------------------------------------------------------
sub vcl_hash {
/**
Cache key for pages: proto, Accept-Encoding (normalized in vcl_recv), then
Varnish default (host + URL).
Static files: see vcl/includes/static.vcl (path-only hash for multisite).
**/
if (req.http.X-Forwarded-Proto) {
hash_data(req.http.X-Forwarded-Proto);
}
if (req.http.Accept-Encoding) {
hash_data(req.http.Accept-Encoding);
}
call static_hash;
}
# ----------------------------------------------------------------------
# vcl_backend_response
# ----------------------------------------------------------------------
sub vcl_backend_response {
# -- Bypass cache for files > 10 MB and stream them directly --
if (std.integer(beresp.http.Content-Length, 0) > 10485760) {
set beresp.uncacheable = true;
set beresp.do_stream = true;
set beresp.ttl = 120s;
return (deliver);
}
call static_backend_response;
# -- Fix backend port leaking into redirect Location headers --
if (beresp.status == 301 || beresp.status == 302) {
set beresp.http.Location = regsub(beresp.http.Location, ":[0-9]+", "");
}
# -- Inject URL and Host for asynchronous banning --
set beresp.http.x-url = bereq.url;
set beresp.http.x-host = bereq.http.host;
# -- Ensure grace is at least 2 hours --
if (beresp.grace < 2h) {
set beresp.grace = 2h;
}
# -- Responses with Set-Cookie are not cacheable --
if (beresp.http.Set-Cookie) {
set beresp.http.X-Cacheable = "NO:Got Cookies";
set beresp.ttl = 0s;
return (deliver);
}
# -- Never cache temporary redirects (302); they must not use the default TTL below --
if (beresp.status == 302) {
set beresp.http.X-Cacheable = "NO:302";
set beresp.uncacheable = true;
set beresp.ttl = 120s;
return (deliver);
}
# -- Default TTL when the backend sends no Cache-Control --
# When the backend omits Cache-Control, we force a 1h TTL so responses are cached.
# To disable: comment out the block below. Responses without Cache-Control will then
# keep beresp.ttl = 0 and be marked uncacheable by the "beresp.ttl <= 0s" block.
# See README "Default TTL when backend has no Cache-Control".
if (!beresp.http.Cache-Control) {
set beresp.ttl = 1h;
set beresp.http.X-Cacheable = "YES:Forced";
}
# -- Mark as uncacheable: zero TTL, private/no-store, or Vary: * --
if (beresp.ttl <= 0s) {
set beresp.uncacheable = true;
set beresp.http.X-Cacheable = "NO:TTL0";
set beresp.ttl = 120s;
} else if (beresp.http.Cache-Control ~ "private" || beresp.http.Cache-Control ~ "no-store") {
set beresp.http.X-Cacheable = "NO:Cache-Control=private,no-store";
set beresp.uncacheable = true;
set beresp.ttl = 0s;
} else if (beresp.http.Vary == "*") {
set beresp.http.X-Cacheable = "NO:Vary=*";
set beresp.uncacheable = true;
set beresp.ttl = 120s;
}
# -- Short-lived cache for 5xx errors --
# Absorbs traffic spikes while the backend recovers.
# Only set TTL when the backend did not send Cache-Control and TTL is 0 (respect HTTP semantics).
# Kept intentionally brief (1m TTL, 30s grace) to avoid masking persistent failures.
if (beresp.status >= 500) {
if (!beresp.http.Cache-Control && beresp.ttl <= 0s) {
set beresp.ttl = 1m;
set beresp.grace = 30s;
set beresp.http.X-Cacheable = "YES:500";
}
}
# -- Cache 404 responses normally (respecting backend headers) --
if (beresp.status == 404) {
set beresp.http.X-Cacheable = "YES:404";
}
return (deliver);
}
# ----------------------------------------------------------------------
# vcl_deliver
# ----------------------------------------------------------------------
sub vcl_deliver {
# -- Strip backend-revealing headers --
unset resp.http.X-Powered-By;
unset resp.http.Server;
unset resp.http.Via;
unset resp.http.X-Varnish;
# -- Debug headers (only when the request includes X-Debug) --
if (req.http.X-Debug) {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
set resp.http.X-Cache-Hits = obj.hits;
if (req.http.X-Cacheable) {
set resp.http.X-Cacheable = req.http.X-Cacheable;
} elsif (obj.uncacheable) {
if (!resp.http.X-Cacheable) {
set resp.http.X-Cacheable = "NO:UNCACHEABLE";
}
} elsif (!resp.http.X-Cacheable) {
set resp.http.X-Cacheable = "YES";
}
} else {
unset resp.http.X-Cacheable;
}
# -- Cleanup internal headers used for banning --
unset resp.http.x-url;
unset resp.http.x-host;
}
# ----------------------------------------------------------------------
# vcl_pipe
# ----------------------------------------------------------------------
sub vcl_pipe {
# -- Forward WebSocket upgrade header to the backend --
if (req.http.upgrade) {
set bereq.http.upgrade = req.http.upgrade;
}
}