-
Notifications
You must be signed in to change notification settings - Fork 29
/
vcl_recv.vcl.tmpl
70 lines (62 loc) · 2.61 KB
/
vcl_recv.vcl.tmpl
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
sub vcl_recv {
# Only cache GET and HEAD requests (pass through POST requests).
if (req.method != "GET" && req.method != "HEAD") {
set req.http.X-VC-Cacheable = "NO:Request method:" + req.method;
return(pass);
}
# Implementing websocket support.
if (req.http.Upgrade ~ "(?i)websocket") {
return (pipe);
}
# Do not cache ajax requests.
if (req.http.X-Requested-With == "XMLHttpRequest") {
set req.http.X-VC-Cacheable = "NO:Requested with: XMLHttpRequest";
return(pass);
}
# Strip hash, server does not need it.
if (req.url ~ "\#") {
set req.url = regsub(req.url, "\#.*$", "");
}
# Strip a trailing ? if it exists
if (req.url ~ "\?$") {
set req.url = regsub(req.url, "\?$", "");
}
{{ if not (getenv "VARNISH_KEEP_ALL_COOKIES") }}
set req.http.vckey = ";" + req.http.Cookie;
set req.http.vckey = regsuball(req.http.vckey, "; +", ";");
set req.http.vckey = regsuball(req.http.vckey, ";(VCKEY-[a-zA-Z0-9-_]+)=", "; \1=");
set req.http.vckey = regsuball(req.http.vckey, ";[^ ][^;]*", "");
set req.http.vckey = regsuball(req.http.vckey, "^[; ]+|[; ]+$", "");
{{ $cookies := (getenv "VARNISH_STRIP_COOKIES" "__[a-z]+|wooTracker|VCKEY-[a-zA-Z0-9-_]+") }}
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)({{ $cookies }})=[^;]*", "");
set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
if (req.http.Cookie ~ "^\s*$") {
unset req.http.Cookie;
}
{{ end }}
{{ if not (getenv "VARNISH_KEEP_ALL_PARAMS") }}
# Strip query parameters from all urls (so they cache as a single object).
{{ if getenv "VARNISH_STRIP_ALL_PARAMS" }}
if (req.url ~ "\?.*") {
set req.url = regsub(req.url, "\?.*", "");
}
{{ else }}
{{ $params := (getenv "VARNISH_STRIP_PARAMS" "utm_[a-z]+|gclid|cx|ie|cof|siteurl|fbclid") }}
if (req.url ~ "(\?|&)({{ $params }})=") {
set req.url = regsuball(req.url, "&({{ $params }})=([A-z0-9_\-\.%25]+)", "");
set req.url = regsuball(req.url, "\?({{ $params }})=([A-z0-9_\-\.%25]+)", "?");
set req.url = regsub(req.url, "\?&", "?");
set req.url = regsub(req.url, "\?$", "");
}
{{ end }}
{{ end }}
### Pagespeed
# Based on the suggestions https://www.modpagespeed.com/doc/downstream-caching
{{ if getenv "VARNISH_PAGESPEED_SECRET_KEY" }}
{{ if getenv "VARNISH_CACHE_STATIC_FILES" }}
# Tell PageSpeed not to use optimizations specific to this request.
set req.http.PS-CapabilityList = "fully general optimizations only";
{{ end }}
{{ end }}
### End of Pagespeed
}