diff --git a/app/code/Magento/PageCache/etc/varnish4.vcl b/app/code/Magento/PageCache/etc/varnish4.vcl index d9ff63e5e9fa2..ee68912c4446d 100644 --- a/app/code/Magento/PageCache/etc/varnish4.vcl +++ b/app/code/Magento/PageCache/etc/varnish4.vcl @@ -1,8 +1,8 @@ vcl 4.0; - + import std; # The minimal Varnish version is 4.0 - + backend default { .host = "/* {{ host }} */"; .port = "/* {{ port }} */"; @@ -23,7 +23,7 @@ sub vcl_recv { ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern); return (synth(200, "Purged")); } - + if (req.method != "GET" && req.method != "HEAD" && req.method != "PUT" && @@ -34,7 +34,7 @@ sub vcl_recv { /* Non-RFC2616 or CONNECT which is weird. */ return (pipe); } - + # We only deal with GET and HEAD by default if (req.method != "GET" && req.method != "HEAD") { return (pass); @@ -46,6 +46,30 @@ sub vcl_recv { # collect all cookies std.collect(req.http.Cookie); + # Even though there are few possible values for Accept-Encoding, Varnish treats + # them literally rather than semantically, so even a small difference which makes + # no difference to the backend can reduce cache efficiency by making Varnish cache + # too many different versions of an object. + # https://www.varnish-cache.org/trac/wiki/FAQ/Compression + if (req.http.Accept-Encoding) { + if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$") { + # No point in compressing these + unset req.http.Accept-Encoding; + } elsif (req.http.Accept-Encoding ~ "gzip") { + set req.http.Accept-Encoding = "gzip"; + } elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") { + set req.http.Accept-Encoding = "deflate"; + } else { + # unkown algorithm + unset req.http.Accept-Encoding; + } + } + + # Remove Google gclid parameters to minimize the cache objects + set req.url = regsuball(req.url,"\?gclid=[^&]+$",""); # strips when QS = "?gclid=AAA" + set req.url = regsuball(req.url,"\?gclid=[^&]+&","?"); # strips when QS = "?gclid=AAA&foo=bar" + set req.url = regsuball(req.url,"&gclid=[^&]+",""); # strips when QS = "?foo=bar&gclid=AAA" or QS = "?foo=bar&gclid=AAA&bar=baz" + # static files are always cacheable. remove SSL flag and cookie if (req.url ~ "^/(pub/)?(media|static)/.*\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)$") { unset req.http.Https; @@ -59,6 +83,21 @@ sub vcl_hash { if (req.http.cookie ~ "X-Magento-Vary=") { hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1")); } + + #for multi site configurations to not cache each-other's content + if (req.http.host) { + hash_data(req.http.host); + } else { + hash_data(server.ip); + } + + # mainly to make sure, if the site was cached via a http connection and a visitor opens the + # https version, they won't see an ssl warning about mixed content (if the site was cached via http + # connection, the external resources like css, js will be opened via an http connection as well + # instead of https + if (req.http.X-Forwarded-Proto) { + hash_data(req.http.X-Forwarded-Proto); + } /* {{ design_exceptions_code }} */ } @@ -70,7 +109,7 @@ sub vcl_backend_response { if (bereq.url ~ "\.js$" || beresp.http.content-type ~ "text") { set beresp.do_gzip = true; } - + # cache only successfully responses and 404s if (beresp.status != 200 && beresp.status != 404) { set beresp.ttl = 0s;