Skip to content

Commit 6f4235b

Browse files
author
Hayder Sharhan
committed
Merge commit 'refs/pull/2028/head' of https://github.com/magento/magento2 into MAGETWO-49876
2 parents 1f37b76 + d1299d0 commit 6f4235b

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

app/code/Magento/PageCache/etc/varnish4.vcl

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
vcl 4.0;
2-
2+
33
import std;
44
# The minimal Varnish version is 4.0
5-
5+
66
backend default {
77
.host = "/* {{ host }} */";
88
.port = "/* {{ port }} */";
@@ -23,7 +23,7 @@ sub vcl_recv {
2323
ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
2424
return (synth(200, "Purged"));
2525
}
26-
26+
2727
if (req.method != "GET" &&
2828
req.method != "HEAD" &&
2929
req.method != "PUT" &&
@@ -34,7 +34,7 @@ sub vcl_recv {
3434
/* Non-RFC2616 or CONNECT which is weird. */
3535
return (pipe);
3636
}
37-
37+
3838
# We only deal with GET and HEAD by default
3939
if (req.method != "GET" && req.method != "HEAD") {
4040
return (pass);
@@ -51,6 +51,30 @@ sub vcl_recv {
5151
# collect all cookies
5252
std.collect(req.http.Cookie);
5353

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+
5478
# static files are always cacheable. remove SSL flag and cookie
5579
if (req.url ~ "^/(pub/)?(media|static)/.*\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)$") {
5680
unset req.http.Https;
@@ -64,6 +88,21 @@ sub vcl_hash {
6488
if (req.http.cookie ~ "X-Magento-Vary=") {
6589
hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
6690
}
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+
}
67106
/* {{ design_exceptions_code }} */
68107
}
69108

@@ -75,7 +114,7 @@ sub vcl_backend_response {
75114
if (bereq.url ~ "\.js$" || beresp.http.content-type ~ "text") {
76115
set beresp.do_gzip = true;
77116
}
78-
117+
79118
# cache only successfully responses and 404s
80119
if (beresp.status != 200 && beresp.status != 404) {
81120
set beresp.ttl = 0s;

0 commit comments

Comments
 (0)