Skip to content

[vcl] Better debug #28944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/code/Magento/PageCache/etc/varnish6.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ sub vcl_backend_response {
}

sub vcl_deliver {
if (resp.http.x-varnish ~ " ") {
if (obj.uncacheable) {
set resp.http.X-Magento-Cache-Debug = "UNCACHEABLE";
} else if (obj.hits) {
set resp.http.X-Magento-Cache-Debug = "HIT";
set resp.http.Grace = req.http.grace;
} else {
Expand Down
11 changes: 11 additions & 0 deletions dev/tests/varnish/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Files in this directory are Varnish Test Cases (VTC) and check the behavior of the VCL file shipped by `magento2`.

Varnish needs to be installed, but then the test scenarios can be run individually or all at once:

``` shell
varnishtest *.vtc
```

Documentation:
- varnishtest itself: https://varnish-cache.org/docs/trunk/reference/varnishtest.html
- VTC syntax: https://varnish-cache.org/docs/trunk/reference/vtc.html
54 changes: 54 additions & 0 deletions dev/tests/varnish/debug.vtc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
varnishtest "X-Magento-Cache-Debug header"

server s1 {
# first request will be the probe, handle it and be on our way
rxreq
expect req.url == "/health_check.php"
txresp

# the probe expects the connection to close
close
accept

rxreq
txresp -hdr "answer-to: POST"

rxreq
txresp -hdr "answer-to: GET"
} -start

# generate usable VCL pointing towards s1
# mostly, we replace the place-holders, but we also jack up the probe
# interval to avoid further interference
shell {
# testdir is automatically set to the directory containing the present vtc
sed \
-e 's@\.interval = 5s;@.interval = 5m; .initial = 10;@' \
-e 's@/\* {{ host }} \*/@${s1_addr}@' \
-e 's@/\* {{ port }} \*/@${s1_port}@' \
-e 's@/\* {{ ssl_offloaded_header }} \*/@unused@' \
-e 's@/\* {{ grace_period }} \*/@0@' \
${testdir}/../../../app/code/Magento/PageCache/etc/varnish6.vcl > ${tmpdir}/output.vcl
}

varnish v1 -arg "-f" -arg "${tmpdir}/output.vcl" -start

# make surethe probe request fired
delay 1

client c1 {
txreq -method "POST"
rxresp
expect resp.http.answer-to == "POST"
expect resp.http.X-Magento-Cache-Debug == "UNCACHEABLE"

txreq
rxresp
expect resp.http.answer-to == "GET"
expect resp.http.X-Magento-Cache-Debug == "MISS"

txreq
rxresp
expect resp.http.answer-to == "GET"
expect resp.http.X-Magento-Cache-Debug == "HIT"
} -run