Skip to content

Commit 24f6e61

Browse files
authored
Merge pull request #133 from ho-nl/update-varnish-vcl
Update varnish6.vcl with changes from recent Magento versions
2 parents ceeb556 + 71a8512 commit 24f6e61

File tree

1 file changed

+58
-48
lines changed

1 file changed

+58
-48
lines changed

varnish/default.vcl

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ sub vcl_recv {
5858
return (pass);
5959
}
6060

61-
# Bypass shopping cart and checkout
62-
if (req.url ~ "/checkout") {
61+
# Bypass customer, shopping cart, checkout
62+
if (req.url ~ "/customer" || req.url ~ "/checkout") {
6363
return (pass);
6464
}
6565

@@ -137,21 +137,6 @@ sub vcl_recv {
137137
# collect all cookies
138138
std.collect(req.http.Cookie);
139139

140-
# Compression filter. See https://www.varnish-cache.org/trac/wiki/FAQ/Compression
141-
if (req.http.Accept-Encoding) {
142-
if (req.url ~ "\.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$") {
143-
# No point in compressing these
144-
unset req.http.Accept-Encoding;
145-
} elsif (req.http.Accept-Encoding ~ "gzip") {
146-
set req.http.Accept-Encoding = "gzip";
147-
} elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") {
148-
set req.http.Accept-Encoding = "deflate";
149-
} else {
150-
# unknown algorithm
151-
unset req.http.Accept-Encoding;
152-
}
153-
}
154-
155140
# Remove all marketing get parameters to minimize the cache objects
156141
if (req.url ~ "(\?|&)(gclid|cx|ie|cof|siteurl|zanpid|origin|fbclid|mc_[a-z]+|utm_[a-z]+|_bta_[a-z]+)=") {
157142
set req.url = regsuball(req.url, "(gclid|cx|ie|cof|siteurl|zanpid|origin|fbclid|mc_[a-z]+|utm_[a-z]+|_bta_[a-z]+)=[-_A-z0-9+()%.]+&?", "");
@@ -169,34 +154,43 @@ sub vcl_recv {
169154
#unset req.http.Cookie;
170155
}
171156

172-
# Authenticated GraphQL requests should not be cached by default
173-
if (req.url ~ "/graphql" && req.http.Authorization ~ "^Bearer") {
157+
# Bypass authenticated GraphQL requests without a X-Magento-Cache-Id
158+
if (req.url ~ "/graphql" && !req.http.X-Magento-Cache-Id && req.http.Authorization ~ "^Bearer") {
174159
return (pass);
175160
}
176161

177162
return (hash);
178163
}
179164

180165
sub vcl_hash {
181-
if (req.http.cookie ~ "X-Magento-Vary=") {
166+
if ((req.url !~ "/graphql" || !req.http.X-Magento-Cache-Id) && req.http.cookie ~ "X-Magento-Vary=") {
182167
hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
183168
}
184169

185170
# To make sure http users don't see ssl warning
186171
if (req.http.X-Forwarded-Proto) {
187172
hash_data(req.http.X-Forwarded-Proto);
188173
}
189-
190174

191175
if (req.url ~ "/graphql") {
192176
call process_graphql_headers;
193177
}
194178
}
195179

196180
sub process_graphql_headers {
181+
if (req.http.X-Magento-Cache-Id) {
182+
hash_data(req.http.X-Magento-Cache-Id);
183+
184+
# When the frontend stops sending the auth token, make sure users stop getting results cached for logged-in users
185+
if (req.http.Authorization ~ "^Bearer") {
186+
hash_data("Authorized");
187+
}
188+
}
189+
197190
if (req.http.Store) {
198191
hash_data(req.http.Store);
199192
}
193+
200194
if (req.http.Content-Currency) {
201195
hash_data(req.http.Content-Currency);
202196
}
@@ -218,53 +212,58 @@ sub vcl_backend_response {
218212
set beresp.http.X-Magento-Cache-Control = beresp.http.Cache-Control;
219213
}
220214

221-
# cache only successfully responses and 404s
222-
if (beresp.status != 200 && beresp.status != 404) {
223-
set beresp.ttl = 0s;
224-
set beresp.uncacheable = true;
225-
return (deliver);
226-
} elsif (beresp.http.Cache-Control ~ "private") {
215+
# cache only successfully responses and 404s that are not marked as private
216+
if ((beresp.status != 200 && beresp.status != 404) || beresp.http.Cache-Control ~ "private") {
227217
set beresp.uncacheable = true;
228218
set beresp.ttl = 86400s;
229219
return (deliver);
230220
}
231221

232222
# validate if we need to cache it and prevent from setting cookie
233223
if (beresp.ttl > 0s && (bereq.method == "GET" || bereq.method == "HEAD")) {
224+
# Collapse beresp.http.set-cookie in order to merge multiple set-cookie headers
225+
# Although it is not recommended to collapse set-cookie header,
226+
# it is safe to do it here as the set-cookie header is removed below
227+
std.collect(beresp.http.set-cookie);
228+
# Do not cache the response under current cache key (hash),
229+
# if the response has X-Magento-Vary but the request does not.
230+
if ((bereq.url !~ "/graphql" || !bereq.http.X-Magento-Cache-Id)
231+
&& bereq.http.cookie !~ "X-Magento-Vary="
232+
&& beresp.http.set-cookie ~ "X-Magento-Vary=") {
233+
set beresp.ttl = 0s;
234+
set beresp.uncacheable = true;
235+
}
234236
unset beresp.http.set-cookie;
235237
}
236238

237-
# If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
238-
if (beresp.ttl <= 0s ||
239-
beresp.http.Surrogate-control ~ "no-store" ||
240-
(!beresp.http.Surrogate-Control &&
241-
beresp.http.Cache-Control ~ "no-cache|no-store") ||
242-
beresp.http.Vary == "*") {
239+
# If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
240+
if (beresp.ttl <= 0s ||
241+
beresp.http.Surrogate-control ~ "no-store" ||
242+
(!beresp.http.Surrogate-Control &&
243+
beresp.http.Cache-Control ~ "no-cache|no-store") ||
244+
beresp.http.Vary == "*") {
243245
# Mark as Hit-For-Pass for the next 2 minutes
244246
set beresp.ttl = 120s;
245247
set beresp.uncacheable = true;
246248
}
247249

250+
# If the cache key in the Magento response doesn't match the one that was sent in the request, don't cache under the request's key
251+
if (bereq.url ~ "/graphql" && bereq.http.X-Magento-Cache-Id && bereq.http.X-Magento-Cache-Id != beresp.http.X-Magento-Cache-Id) {
252+
set beresp.ttl = 0s;
253+
set beresp.uncacheable = true;
254+
}
255+
248256
return (deliver);
249257
}
250258

251259
sub vcl_deliver {
252-
if (resp.http.X-Magento-Debug) {
253-
if (resp.http.x-varnish ~ " ") {
254-
set resp.http.X-Magento-Cache-Debug = "HIT";
255-
set resp.http.Grace = req.http.grace;
256-
} else {
257-
set resp.http.X-Magento-Cache-Debug = "MISS";
258-
}
260+
if (obj.uncacheable) {
261+
set resp.http.X-Magento-Cache-Debug = "UNCACHEABLE";
262+
} else if (obj.hits) {
263+
set resp.http.X-Magento-Cache-Debug = "HIT";
264+
set resp.http.Grace = req.http.grace;
259265
} else {
260-
unset resp.http.Age;
261-
unset resp.http.X-Magento-Debug;
262-
unset resp.http.X-Magento-Tags;
263-
unset resp.http.X-Powered-By;
264-
unset resp.http.Server;
265-
unset resp.http.X-Varnish;
266-
unset resp.http.Via;
267-
unset resp.http.Link;
266+
set resp.http.X-Magento-Cache-Debug = "MISS";
268267
}
269268

270269
# Not letting browser to cache non-static files.
@@ -273,6 +272,17 @@ sub vcl_deliver {
273272
set resp.http.Expires = "-1";
274273
set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
275274
}
275+
276+
if (!resp.http.X-Magento-Debug) {
277+
unset resp.http.Age;
278+
}
279+
unset resp.http.X-Magento-Debug;
280+
unset resp.http.X-Magento-Tags;
281+
unset resp.http.X-Powered-By;
282+
unset resp.http.Server;
283+
unset resp.http.X-Varnish;
284+
unset resp.http.Via;
285+
unset resp.http.Link;
276286
}
277287

278288
sub vcl_hit {

0 commit comments

Comments
 (0)