1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Using Varnish on a Cloud Server
This guide presents several examples of using Varnish on Infomaniak Managed Cloud Server.
⚠️ For additional help, contact a partner or submit a tender — also read the role of the host
Varnish Configuration
This is what a basic configuration file could look like with some common cases and different actions/rules in a single example:
vcl 4.0;
# This should not be modified
backend default {
.host = "127.0.0.80";
.port = "80";
}
# An ACL is an "Access control list", which defines a list of IPs, later to be reused in a rule, for example "Who can purge the cache"
acl purge {
"localhost";
"1.2.3.4"; # My Home IP
"42.42.42.0"/24; # My company's public IP range
! "42.42.42.7"; # Except some annoying colleague
}
# Happens when varnish first gets the request, before sending it to the backend
sub vcl_recv {
# Allow purging
if (req.method == "PURGE") {
# Not from an allowed IP? Then die with an error.
if (!client.ip ~ purge) { # purge is the ACL defined at the beginning
# 'synth' is a function that returns a page (without asking the backend) that will not get cached.
return (synth(405, "This IP is not allowed to send PURGE requests."));
}
# If you got to this stage (and didn't error out above), purge the cached result
return (purge);
}
# Allow purging all images, for example
if (req.method == "PURGEALL" && req.url == "/images") {
if (!client.ip ~ purge) {
return (synth(405, "This IP is not allowed to send PURGE requests."));
}
# The 'ban' function is a built-in that allows invalidating all objects in cache that match the given expression
ban("req.url ~ \.(jpg|png|gif|svg)$");
return (synth(200, "Purged images."));
}
# Pages with authorization are not cached
if (req.http.Authorization) {
# With the 'pass' action, the request will be directly passed from the backend without entering the cache
return (pass);
}
}
# Happens after we have read the response headers from the backend.
# Here you clean the response headers, define if the request must get cached, for how much time, etc.
sub vcl_backend_response {
# Cache all images for 1 day by default
if (beresp.http.content-type ~ "image") {
set beresp.ttl = 1d;
}
# If server sets an "uncacheable" header, do not store the response in cache
if (beresp.http.uncacheable) {
set beresp.uncacheable = true;
}
}
Purging from the CLI Interface
From here, the rules outlined in the above configuration work on all requests. So, if the configured site is "domain.xyz," you can simply use the CLI tool "curl" and do the following:
$ curl -X PURGE https://domain.xyz/
Error 200 Purged
Purged
Guru Meditation:
XID: 2
Varnish cache server
And there, the home page has been purged. Or to purge another URL, simply point the request to it:
$ curl -X PURGE https://domain.xyz/some_path/some_file.html
Error 200 Purged
Purged
Guru Meditation:
XID: 4
Varnish cache server
Or, as indicated in the VCL configuration, purge all images:
$ curl -X PURGEALL https://domain.xyz/images
Error 200 Purged images.
Purged images.
Guru Meditation:
XID: 32770
Varnish cache server
Purging from a CMS
It's a bit more challenging to illustrate this case because there are many ways to manage caching from a backend. In the above configuration example, a check on the "Uncacheable" header is added, which disables caching. With this option, any CMS could simply set this header on the response to disable caching for that request, for example.
From any PHP code and with the above configuration, you can simply send an HTTP request and use this snippet to perform a cache PURGE:
if ($curl = curl_init("http://127.0.0.1/some_url")) {
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "PURGE",
CURLOPT_HTTPHEADER => [
"Host: {$_SERVER['HTTP_HOST']}"
]
]);
curl_exec($curl);
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200) {
echo "Cache purged!";
}
curl_close($curl);
}
Learn More
Useful links regarding the Varnish Configuration Language (VCL) for controlling request handling, routing, caching, and several other aspects: