Skip to content

Commit 5ce1bfa

Browse files
committed
Merge remote-tracking branch '36524/fix-for-issue-36492' into 247beta3_jan
2 parents 7127de4 + 3b3396b commit 5ce1bfa

File tree

4 files changed

+133
-42
lines changed

4 files changed

+133
-42
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,22 +180,22 @@ sub vcl_backend_response {
180180
unset beresp.http.set-cookie;
181181
}
182182

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 == "*") {
189-
# Mark as Hit-For-Pass for the next 2 minutes
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 == "*") {
189+
# Mark as Hit-For-Pass for the next 2 minutes
190190
set beresp.ttl = 120s;
191191
set beresp.uncacheable = true;
192192
}
193193

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-
}
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+
}
199199

200200
return (deliver);
201201
}

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

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

156-
# cache only successfully responses and 404s
157-
if (beresp.status != 200 &&
158-
beresp.status != 404 &&
159-
beresp.http.Cache-Control ~ "private") {
156+
# cache only successfully responses and 404s that are not marked as private
157+
if ((beresp.status != 200 && beresp.status != 404) || beresp.http.Cache-Control ~ "private") {
160158
set beresp.uncacheable = true;
161159
set beresp.ttl = 86400s;
162160
return (deliver);
@@ -179,22 +177,22 @@ sub vcl_backend_response {
179177
unset beresp.http.set-cookie;
180178
}
181179

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

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

199197
return (deliver);
200198
}

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ sub vcl_backend_response {
158158
}
159159

160160
# cache only successfully responses and 404s that are not marked as private
161-
if (beresp.status != 200 &&
162-
beresp.status != 404 &&
163-
beresp.http.Cache-Control ~ "private") {
161+
if ((beresp.status != 200 && beresp.status != 404) || beresp.http.Cache-Control ~ "private") {
164162
set beresp.uncacheable = true;
165163
set beresp.ttl = 86400s;
166164
return (deliver);
@@ -183,22 +181,22 @@ sub vcl_backend_response {
183181
unset beresp.http.set-cookie;
184182
}
185183

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

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

203201
return (deliver);
204202
}

dev/tests/varnish/no-caching.vtc

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
varnishtest "X-Magento-Cache-Debug header"
2+
3+
server s1 {
4+
# first request will be the probe, handle it and be on our way
5+
rxreq
6+
expect req.url == "/health_check.php"
7+
txresp
8+
9+
# the probe expects the connection to close
10+
close
11+
accept
12+
13+
# the client will send 2 requests for each url, the server should only see one
14+
# of each in the cacheable case, and two otherwise
15+
16+
# 405, Cache-Control doesn't matter
17+
loop 2 {
18+
rxreq
19+
expect req.url == "/405uncacheable"
20+
txresp -status 405
21+
}
22+
23+
# 200, private Cache-Control
24+
loop 2 {
25+
rxreq
26+
expect req.url == "/200uncacheable"
27+
txresp -status 200 -hdr "cache-control: private"
28+
}
29+
30+
# 404, private Cache-Control
31+
loop 2 {
32+
rxreq
33+
expect req.url == "/404uncacheable"
34+
txresp -status 200 -hdr "cache-control: private"
35+
}
36+
37+
# 200, no Cache-Control
38+
rxreq
39+
expect req.url == "/200cacheable"
40+
txresp -status 200
41+
42+
# 404, but still no Cache-Control
43+
rxreq
44+
expect req.url == "/404cacheable"
45+
txresp -status 404
46+
} -start
47+
48+
# generate usable VCL pointing towards s1
49+
# mostly, we replace the place-holders, but we also jack up the probe
50+
# interval to avoid further interference
51+
shell {
52+
# testdir is automatically set to the directory containing the present vtc
53+
sed \
54+
-e 's@\.interval = 5s;@.interval = 5m; .initial = 10;@' \
55+
-e 's@/\* {{ host }} \*/@${s1_addr}@' \
56+
-e 's@/\* {{ port }} \*/@${s1_port}@' \
57+
-e 's@/\* {{ ssl_offloaded_header }} \*/@unused@' \
58+
-e 's@/\* {{ grace_period }} \*/@0@' \
59+
${testdir}/../../../app/code/Magento/PageCache/etc/varnish6.vcl > ${tmpdir}/output.vcl
60+
}
61+
62+
varnish v1 -arg "-f" -arg "${tmpdir}/output.vcl" -start
63+
64+
# make sure the probe request fired
65+
delay 1
66+
67+
client c1 {
68+
loop 2 {
69+
txreq -url /405uncacheable
70+
rxresp
71+
}
72+
73+
loop 2 {
74+
txreq -url /200uncacheable
75+
rxresp
76+
}
77+
78+
loop 2 {
79+
txreq -url /404uncacheable
80+
rxresp
81+
}
82+
83+
loop 2 {
84+
txreq -url /200cacheable
85+
rxresp
86+
}
87+
88+
loop 2 {
89+
txreq -url /404cacheable
90+
rxresp
91+
}
92+
} -run
93+
94+
# make sure s1 saw all the requests it was expecting
95+
server s1 -wait

0 commit comments

Comments
 (0)