Skip to content

Commit 7b5655e

Browse files
committed
Don't cache private content in Varnish, only 200 and 404 responses. Also fixes some whitespace inconsistencies.
1 parent ffeb0a0 commit 7b5655e

File tree

3 files changed

+38
-42
lines changed

3 files changed

+38
-42
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,22 +183,22 @@ sub vcl_backend_response {
183183
unset beresp.http.set-cookie;
184184
}
185185

186-
# If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
187-
if (beresp.ttl <= 0s ||
188-
beresp.http.Surrogate-control ~ "no-store" ||
189-
(!beresp.http.Surrogate-Control &&
190-
beresp.http.Cache-Control ~ "no-cache|no-store") ||
191-
beresp.http.Vary == "*") {
192-
# Mark as Hit-For-Pass for the next 2 minutes
186+
# If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
187+
if (beresp.ttl <= 0s ||
188+
beresp.http.Surrogate-control ~ "no-store" ||
189+
(!beresp.http.Surrogate-Control &&
190+
beresp.http.Cache-Control ~ "no-cache|no-store") ||
191+
beresp.http.Vary == "*") {
192+
# Mark as Hit-For-Pass for the next 2 minutes
193193
set beresp.ttl = 120s;
194194
set beresp.uncacheable = true;
195195
}
196196

197-
# 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
198-
if (bereq.url ~ "/graphql" && bereq.http.X-Magento-Cache-Id && bereq.http.X-Magento-Cache-Id != beresp.http.X-Magento-Cache-Id) {
199-
set beresp.ttl = 0s;
200-
set beresp.uncacheable = true;
201-
}
197+
# 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
198+
if (bereq.url ~ "/graphql" && bereq.http.X-Magento-Cache-Id && bereq.http.X-Magento-Cache-Id != beresp.http.X-Magento-Cache-Id) {
199+
set beresp.ttl = 0s;
200+
set beresp.uncacheable = true;
201+
}
202202

203203
return (deliver);
204204
}

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,8 @@ sub vcl_backend_response {
168168
set beresp.http.X-Magento-Cache-Control = beresp.http.Cache-Control;
169169
}
170170

171-
# cache only successfully responses and 404s
172-
if (beresp.status != 200 &&
173-
beresp.status != 404 &&
174-
beresp.http.Cache-Control ~ "private") {
171+
# cache only successfully responses and 404s that are not marked as private
172+
if ((beresp.status != 200 && beresp.status != 404) || beresp.http.Cache-Control ~ "private") {
175173
set beresp.uncacheable = true;
176174
set beresp.ttl = 86400s;
177175
return (deliver);
@@ -182,22 +180,22 @@ sub vcl_backend_response {
182180
unset beresp.http.set-cookie;
183181
}
184182

185-
# If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
186-
if (beresp.ttl <= 0s ||
187-
beresp.http.Surrogate-control ~ "no-store" ||
188-
(!beresp.http.Surrogate-Control &&
189-
beresp.http.Cache-Control ~ "no-cache|no-store") ||
190-
beresp.http.Vary == "*") {
183+
# If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
184+
if (beresp.ttl <= 0s ||
185+
beresp.http.Surrogate-control ~ "no-store" ||
186+
(!beresp.http.Surrogate-Control &&
187+
beresp.http.Cache-Control ~ "no-cache|no-store") ||
188+
beresp.http.Vary == "*") {
191189
# Mark as Hit-For-Pass for the next 2 minutes
192190
set beresp.ttl = 120s;
193191
set beresp.uncacheable = true;
194192
}
195193

196-
# 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
197-
if (bereq.url ~ "/graphql" && bereq.http.X-Magento-Cache-Id && bereq.http.X-Magento-Cache-Id != beresp.http.X-Magento-Cache-Id) {
198-
set beresp.ttl = 0s;
199-
set beresp.uncacheable = true;
200-
}
194+
# 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
195+
if (bereq.url ~ "/graphql" && bereq.http.X-Magento-Cache-Id && bereq.http.X-Magento-Cache-Id != beresp.http.X-Magento-Cache-Id) {
196+
set beresp.ttl = 0s;
197+
set beresp.uncacheable = true;
198+
}
201199

202200
return (deliver);
203201
}

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,7 @@ sub vcl_backend_response {
173173
}
174174

175175
# cache only successfully responses and 404s that are not marked as private
176-
if (beresp.status != 200 &&
177-
beresp.status != 404 &&
178-
beresp.http.Cache-Control ~ "private") {
176+
if ((beresp.status != 200 && beresp.status != 404) || beresp.http.Cache-Control ~ "private") {
179177
set beresp.uncacheable = true;
180178
set beresp.ttl = 86400s;
181179
return (deliver);
@@ -186,22 +184,22 @@ sub vcl_backend_response {
186184
unset beresp.http.set-cookie;
187185
}
188186

189-
# If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
190-
if (beresp.ttl <= 0s ||
191-
beresp.http.Surrogate-control ~ "no-store" ||
192-
(!beresp.http.Surrogate-Control &&
193-
beresp.http.Cache-Control ~ "no-cache|no-store") ||
194-
beresp.http.Vary == "*") {
187+
# If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
188+
if (beresp.ttl <= 0s ||
189+
beresp.http.Surrogate-control ~ "no-store" ||
190+
(!beresp.http.Surrogate-Control &&
191+
beresp.http.Cache-Control ~ "no-cache|no-store") ||
192+
beresp.http.Vary == "*") {
195193
# Mark as Hit-For-Pass for the next 2 minutes
196194
set beresp.ttl = 120s;
197195
set beresp.uncacheable = true;
198-
}
196+
}
199197

200-
# 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
201-
if (bereq.url ~ "/graphql" && bereq.http.X-Magento-Cache-Id && bereq.http.X-Magento-Cache-Id != beresp.http.X-Magento-Cache-Id) {
202-
set beresp.ttl = 0s;
203-
set beresp.uncacheable = true;
204-
}
198+
# 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
199+
if (bereq.url ~ "/graphql" && bereq.http.X-Magento-Cache-Id && bereq.http.X-Magento-Cache-Id != beresp.http.X-Magento-Cache-Id) {
200+
set beresp.ttl = 0s;
201+
set beresp.uncacheable = true;
202+
}
205203

206204
return (deliver);
207205
}

0 commit comments

Comments
 (0)