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 );
@@ -51,6 +51,30 @@ sub vcl_recv {
51
51
# collect all cookies
52
52
std.collect (req.http.Cookie );
53
53
54
+ # Even though there are few possible values for Accept-Encoding, Varnish treats
55
+ # them literally rather than semantically, so even a small difference which makes
56
+ # no difference to the backend can reduce cache efficiency by making Varnish cache
57
+ # too many different versions of an object.
58
+ # https://www.varnish-cache.org/trac/wiki/FAQ/Compression
59
+ if (req.http.Accept-Encoding ) {
60
+ if (req.url ~ " \.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$" ) {
61
+ # No point in compressing these
62
+ unset req.http.Accept-Encoding ;
63
+ } elsif (req.http.Accept-Encoding ~ " gzip" ) {
64
+ set req.http.Accept-Encoding = " gzip" ;
65
+ } elsif (req.http.Accept-Encoding ~ " deflate" && req.http.user-agent !~ " MSIE" ) {
66
+ set req.http.Accept-Encoding = " deflate" ;
67
+ } else {
68
+ # unkown algorithm
69
+ unset req.http.Accept-Encoding ;
70
+ }
71
+ }
72
+
73
+ # Remove Google gclid parameters to minimize the cache objects
74
+ set req.url = regsuball (req.url ," \?gclid=[^&]+$" ," " ); # strips when QS = "?gclid=AAA"
75
+ set req.url = regsuball (req.url ," \?gclid=[^&]+&" ," ?" ); # strips when QS = "?gclid=AAA&foo=bar"
76
+ set req.url = regsuball (req.url ," &gclid=[^&]+" ," " ); # strips when QS = "?foo=bar&gclid=AAA" or QS = "?foo=bar&gclid=AAA&bar=baz"
77
+
54
78
# static files are always cacheable. remove SSL flag and cookie
55
79
if (req.url ~ " ^/(pub/)?(media|static)/.*\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)$" ) {
56
80
unset req.http.Https ;
@@ -64,6 +88,21 @@ sub vcl_hash {
64
88
if (req.http.cookie ~ " X-Magento-Vary=" ) {
65
89
hash_data (regsub (req.http.cookie , " ^.*?X-Magento-Vary=([^;]+);*.*$" , " \1" ));
66
90
}
91
+
92
+ #for multi site configurations to not cache each-other's content
93
+ if (req.http.host ) {
94
+ hash_data (req.http.host );
95
+ } else {
96
+ hash_data (server.ip );
97
+ }
98
+
99
+ # mainly to make sure, if the site was cached via a http connection and a visitor opens the
100
+ # https version, they won't see an ssl warning about mixed content (if the site was cached via http
101
+ # connection, the external resources like css, js will be opened via an http connection as well
102
+ # instead of https
103
+ if (req.http.X-Forwarded-Proto ) {
104
+ hash_data (req.http.X-Forwarded-Proto );
105
+ }
67
106
/* {{ design_exceptions_code }} */
68
107
}
69
108
@@ -75,7 +114,7 @@ sub vcl_backend_response {
75
114
if (bereq.url ~ " \.js$" || beresp.http.content-type ~ " text" ) {
76
115
set beresp.do_gzip = true ;
77
116
}
78
-
117
+
79
118
# cache only successfully responses and 404s
80
119
if (beresp.status != 200 && beresp.status != 404 ) {
81
120
set beresp.ttl = 0s ;
0 commit comments