Created
November 9, 2021 16:04
-
-
Save VerosK/76caa54cdd70a7d084214d5a0d85176b to your computer and use it in GitHub Desktop.
Debug varnish cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vcl 4.0; | |
import directors; | |
import var; | |
import std; | |
backend main { | |
.host = "${VARNISH_BACKEND_HOST}"; | |
.port = "${VARNISH_BACKEND_PORT}"; | |
.first_byte_timeout = ${VARNISH_BACKEND_TIMEOUT}; | |
} | |
sub vcl_init { | |
} | |
sub vcl_recv { | |
if (req.method == "BAN") { | |
ban("req.url ~ ."); // ban everything | |
return(synth(200, "Ban added")); | |
} | |
var.set_real("req_start", std.time2real(now, 0)); | |
unset req.http.x-cache; | |
set req.http.x-cache-path = "none"; | |
set req.backend_hint = main; | |
# these are not handled | |
if (req.method != "GET" && | |
req.method != "HEAD" && | |
req.method != "PUT" && | |
req.method != "POST" && | |
req.method != "OPTIONS" && | |
req.method != "DELETE") { | |
return (synth(405)); /* not implemented */ | |
} | |
# work only with GET or HEAD | |
# | |
# all other should be passed to the backend servers | |
# | |
if (req.method != "GET" && req.method != "HEAD") { | |
set req.http.x-cache-path = "pass"; | |
return (pass); | |
} | |
# cache getRecordCover calls | |
if ( | |
req.url ~ "^/AJAX/JSON\?method=getRecordCover" || | |
req.url ~ "^/AJAX/JSON\?method=getObalkyKnihWithoutSolr" || | |
#req.url ~ "^/AJAX/JSON\?.*&method=getACSuggestions" || | |
# req.url ~ "^/AJAX/JSON\?.*&method=getSideFacets" || | |
req.url ~ "^/themes" || | |
req.url ~ "^/robots.txt" | |
) { | |
set req.http.x-cache-path = "hash-cacheable"; | |
return (hash); | |
} | |
set req.http.x-cache-path = "default-pass"; | |
return (pass); | |
} | |
sub vcl_hit { | |
var.set_string("Cache-Action", "hit"); | |
} | |
sub vcl_miss { | |
var.set_string("Cache-Action", "miss"); | |
} | |
sub vcl_pass { | |
var.set_string("Cache-Action", "pass"); | |
} | |
sub vcl_pipe { | |
var.set_string("Cache-Action", "pipe"); | |
} | |
sub vcl_synth { | |
var.set_string("Cache-Action", "synth"); | |
} | |
sub vcl_backend_fetch { | |
var.set_real("bereq_start", std.time2real(now, 0)); | |
} | |
sub vcl_backend_response { | |
# Happens after we have read the response headers from the backend. | |
# | |
# Here you clean the response headers, removing silly Set-Cookie headers | |
# and other mistakes your backend does. | |
var.set_real("generated_time", std.time2real(now, 1) - var.get_real("bereq_start")); | |
set beresp.http.X-Backend-Time = var.get_real("generated_time"); | |
if (beresp.status == 404) { | |
unset beresp.http.Expires; | |
set beresp.http.Cache-Control = "public, max-age=900"; | |
unset beresp.http.Set-Cookie; | |
set beresp.ttl = 60s; | |
set beresp.grace = 30s; | |
return (deliver); | |
} | |
if (beresp.status != 200) { | |
set beresp.uncacheable = true; | |
return (deliver); | |
} | |
if ( | |
bereq.url ~ "^/AJAX/JSON\?method=getRecordCover" || | |
bereq.url ~ "^/AJAX/JSON\?method=getObalkyKnihWithoutSolr" || | |
#bereq.url ~ "^/AJAX/JSON\?.*&method=getACSuggestions" || | |
#bereq.url ~ "^/AJAX/JSON\?.*&method=getSideFacets" || | |
bereq.url ~ "^/themes" || | |
bereq.url ~ "^/robots.txt" | |
) { | |
unset beresp.http.Cache-Control; | |
unset beresp.http.Pragma; | |
unset beresp.http.Expires; | |
unset beresp.http.Set-Cookie; | |
set beresp.http.Cache-Control = "public, max-age=900"; | |
set beresp.ttl = 3600s; | |
set beresp.grace = 90s; | |
return (deliver); | |
} | |
set beresp.uncacheable = true; | |
return (deliver); | |
} | |
sub vcl_deliver { | |
var.set_real("processing_time", std.time2real(now, 1) - var.get_real("req_start")); | |
set resp.http.X-Cache-Processing-Time = var.get_real("processing_time"); | |
if (obj.hits > 0) { | |
set resp.http.X-Cache-Hit = "HIT, count=" + obj.hits; | |
} | |
// set resp.http.x-cache-path = req.http.x-cache-path; | |
set resp.http.X-Cache-Action = var.get_string("Cache-Action"); | |
set resp.http.X-Dispatched-By = "${HOSTNAME}"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment