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.
This commit is contained in:
parent
ca25328ef4
commit
6b55736018
52
varnish/default.vcl
Normal file
52
varnish/default.vcl
Normal file
@ -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";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user