From 6b55736018efe760e0189b8ef3418ec5a0b69822 Mon Sep 17 00:00:00 2001 From: igovnow Date: Thu, 29 Aug 2024 13:54:16 +0000 Subject: [PATCH] Add varnish/default.vcl This VCL (Varnish Configuration Language) file defines caching and request handling rules for a Varnish cache server. Here's a description of its main components and functionality: Backend Definition: Sets the default backend to a service named "ghost" on port 2368. Request Handling (vcl_recv): Normalizes the host header by redirecting www subdomain requests. Excludes the Ghost admin panel and API from caching. Synthetic Responses (vcl_synth): Implements a custom redirect for www subdomain to the main domain. Backend Response Handling (vcl_backend_response): Sets a default cache time of 1 hour for all responses. Prevents caching of responses with Set-Cookie headers, varying wildcard, or private Cache-Control directives. Response Delivery (vcl_deliver): Adds a custom X-Cache header to indicate cache hits or misses. This configuration aims to improve website performance by caching Ghost blog content while ensuring that dynamic and private content (like the admin panel) is not cached. It also handles www subdomain redirection and provides cache status information in the response headers. --- varnish/default.vcl | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 varnish/default.vcl diff --git a/varnish/default.vcl b/varnish/default.vcl new file mode 100644 index 0000000..b2a12ad --- /dev/null +++ b/varnish/default.vcl @@ -0,0 +1,52 @@ +vcl 4.1; + +backend default { + .host = "ghost"; + .port = "2368"; +} + +sub vcl_recv { + # Normalize the Host header + if (req.http.host ~ "^www\.") { + return (synth(750, "")); + } + + # Don't cache the admin panel or API + if (req.url ~ "^/ghost/" || req.url ~ "^/ghost-api/") { + return (pass); + } +} + +sub vcl_synth { + if (resp.status == 750) { + set resp.status = 301; + set resp.http.Location = "https://speedyweedyops.org" + req.url; + return(deliver); + } +} + +sub vcl_backend_response { + # Cache everything for 1 hour by default + set beresp.ttl = 1h; + + # Don't cache responses with these headers + if (beresp.http.Set-Cookie || beresp.http.Vary == "*") { + set beresp.uncacheable = true; + return (deliver); + } + + # Don't cache responses for authenticated users + if (beresp.http.Cache-Control ~ "private") { + set beresp.uncacheable = true; + return (deliver); + } +} + +sub vcl_deliver { + # Add a custom header to indicate cache hit/miss + if (obj.hits > 0) { + set resp.http.X-Cache = "HIT"; + } else { + set resp.http.X-Cache = "MISS"; + } +} \ No newline at end of file