Skip to content

Commit d1299d0

Browse files
author
Norbert Nagy
committed
Add improvements for varnish vcl files
Hi, I have created this PR to improve the varnish configuration a little bit and fix some issues. I don't have varnish 3 at the moment, so I did not add the same configuration to the Varnish 3 files, because I can't test it. This PR is for #1575
1 parent b6c0003 commit d1299d0

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);
@@ -46,6 +46,30 @@ sub vcl_recv {
4646
# collect all cookies
4747
std.collect(req.http.Cookie);
4848

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+
4973
# static files are always cacheable. remove SSL flag and cookie
5074
if (req.url ~ "^/(pub/)?(media|static)/.*\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)$") {
5175
unset req.http.Https;
@@ -59,6 +83,21 @@ sub vcl_hash {
5983
if (req.http.cookie ~ "X-Magento-Vary=") {
6084
hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
6185
}
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+
}
62101
/* {{ design_exceptions_code }} */
63102
}
64103

@@ -70,7 +109,7 @@ sub vcl_backend_response {
70109
if (bereq.url ~ "\.js$" || beresp.http.content-type ~ "text") {
71110
set beresp.do_gzip = true;
72111
}
73-
112+
74113
# cache only successfully responses and 404s
75114
if (beresp.status != 200 && beresp.status != 404) {
76115
set beresp.ttl = 0s;

0 commit comments

Comments
 (0)