1
1
vcl 4 .0 ;
2
-
2
+
3
3
import std;
4
4
# The minimal Varnish version is 4.0
5
-
5
+
6
6
backend default {
7
7
.host = " /* {{ host }} */" ;
8
8
.port = " /* {{ port }} */" ;
@@ -23,7 +23,7 @@ sub vcl_recv {
23
23
ban (" obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern );
24
24
return (synth (200, "Purged"));
25
25
}
26
-
26
+
27
27
if (req.method != " GET" &&
28
28
req.method != " HEAD" &&
29
29
req.method != " PUT" &&
@@ -34,7 +34,7 @@ sub vcl_recv {
34
34
/* Non-RFC2616 or CONNECT which is weird. */
35
35
return (pipe );
36
36
}
37
-
37
+
38
38
# We only deal with GET and HEAD by default
39
39
if (req.method != " GET" && req.method != " HEAD" ) {
40
40
return (pass );
@@ -46,6 +46,30 @@ sub vcl_recv {
46
46
# collect all cookies
47
47
std.collect (req.http.Cookie );
48
48
49
+ # Even though there are few possible values for Accept-Encoding, Varnish treats
50
+ # them literally rather than semantically, so even a small difference which makes
51
+ # no difference to the backend can reduce cache efficiency by making Varnish cache
52
+ # too many different versions of an object.
53
+ # https://www.varnish-cache.org/trac/wiki/FAQ/Compression
54
+ if (req.http.Accept-Encoding ) {
55
+ if (req.url ~ " \.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$" ) {
56
+ # No point in compressing these
57
+ unset req.http.Accept-Encoding ;
58
+ } elsif (req.http.Accept-Encoding ~ " gzip" ) {
59
+ set req.http.Accept-Encoding = " gzip" ;
60
+ } elsif (req.http.Accept-Encoding ~ " deflate" && req.http.user-agent !~ " MSIE" ) {
61
+ set req.http.Accept-Encoding = " deflate" ;
62
+ } else {
63
+ # unkown algorithm
64
+ unset req.http.Accept-Encoding ;
65
+ }
66
+ }
67
+
68
+ # Remove Google gclid parameters to minimize the cache objects
69
+ set req.url = regsuball (req.url ," \?gclid=[^&]+$" ," " ); # strips when QS = "?gclid=AAA"
70
+ set req.url = regsuball (req.url ," \?gclid=[^&]+&" ," ?" ); # strips when QS = "?gclid=AAA&foo=bar"
71
+ set req.url = regsuball (req.url ," &gclid=[^&]+" ," " ); # strips when QS = "?foo=bar&gclid=AAA" or QS = "?foo=bar&gclid=AAA&bar=baz"
72
+
49
73
# static files are always cacheable. remove SSL flag and cookie
50
74
if (req.url ~ " ^/(pub/)?(media|static)/.*\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)$" ) {
51
75
unset req.http.Https ;
@@ -59,6 +83,21 @@ sub vcl_hash {
59
83
if (req.http.cookie ~ " X-Magento-Vary=" ) {
60
84
hash_data (regsub (req.http.cookie , " ^.*?X-Magento-Vary=([^;]+);*.*$" , " \1" ));
61
85
}
86
+
87
+ #for multi site configurations to not cache each-other's content
88
+ if (req.http.host ) {
89
+ hash_data (req.http.host );
90
+ } else {
91
+ hash_data (server.ip );
92
+ }
93
+
94
+ # mainly to make sure, if the site was cached via a http connection and a visitor opens the
95
+ # https version, they won't see an ssl warning about mixed content (if the site was cached via http
96
+ # connection, the external resources like css, js will be opened via an http connection as well
97
+ # instead of https
98
+ if (req.http.X-Forwarded-Proto ) {
99
+ hash_data (req.http.X-Forwarded-Proto );
100
+ }
62
101
/* {{ design_exceptions_code }} */
63
102
}
64
103
@@ -70,7 +109,7 @@ sub vcl_backend_response {
70
109
if (bereq.url ~ " \.js$" || beresp.http.content-type ~ " text" ) {
71
110
set beresp.do_gzip = true ;
72
111
}
73
-
112
+
74
113
# cache only successfully responses and 404s
75
114
if (beresp.status != 200 && beresp.status != 404 ) {
76
115
set beresp.ttl = 0s ;
0 commit comments