1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Using Varnish on Cloud Server
This guide presents several examples of using Varnish on Cloud Server Infomaniak.
â ïž For additional help contact a partner or launch a free tender â also discover the role of the host.
Varnish Configuration
After installation, configuring Varnish includes important rules for caching and purging. Be careful not to accidentally allow unwanted IP addresses.
Here is what a basic configuration file might look like with a few common cases and different actions/rules in one example:
vcl 4.0;
# Configuration du backend par défaut
backend default {
.host = "127.0.0.80"; # Adresse IP du backend
.port = "80"; # Port du backend
}
# Définition d'une liste de contrÎle d'accÚs (ACL) pour les IPs autorisées à purger le cache
acl purge {
"localhost"; # IP locale
"1.2.3.4"; # IP de votre domicile
"42.42.42.0"/24; # Plage d'IP publique de votre entreprise
! "42.42.42.7"; # Exclusion d'une IP spĂ©cifique (ex : un collĂšgue gĂȘnant)
}
# Traitement des requĂȘtes Ă leur rĂ©ception par Varnish
sub vcl_recv {
# Autoriser les requĂȘtes de purge
if (req.method == "PURGE") {
# Vérification si l'IP du client est autorisée à purger
if (!client.ip ~ purge) { # 'purge' fait référence à l'ACL définie plus haut
# Retourne une page d'erreur si l'IP n'est pas autorisée
return (synth(405, "Cette IP n'est pas autorisĂ©e Ă envoyer des requĂȘtes PURGE."));
}
# Si l'IP est autorisĂ©e, purger le cache pour cette requĂȘte
return (purge);
}
# Autoriser la purge de toutes les images via une requĂȘte PURGEALL
if (req.method == "PURGEALL" && req.url == "/images") {
if (!client.ip ~ purge) {
return (synth(405, "Cette IP n'est pas autorisĂ©e Ă envoyer des requĂȘtes PURGE."));
}
# Invalider tous les objets en cache correspondant Ă des images
ban("req.url ~ \.(jpg|png|gif|svg)$");
return (synth(200, "Images purgées."));
}
# Ne pas mettre en cache les pages avec une autorisation (header Authorization)
if (req.http.Authorization) {
# Passer la requĂȘte directement au backend sans la mettre en cache
return (pass);
}
}
# Traitement de la réponse du backend avant de la renvoyer au client
sub vcl_backend_response {
# Mise en cache des images pour une durée de 1 jour
if (beresp.http.content-type ~ "image") {
set beresp.ttl = 1d;
}
# Si le backend indique que la rĂ©ponse ne doit pas ĂȘtre mise en cache, respecter cette consigne
if (beresp.http.uncacheable) {
set beresp.uncacheable = true;
}
}
Purge from the CLI interface
From there, the rules stated in the configuration above apply to all requests, so if the configured site is "domain.xyz", you can simply use the CLI tool "curl
" and do the following:
# Envoyer une requĂȘte PURGE pour purger la page d'accueil de "domain.xyz"
$ curl -X PURGE https://domain.xyz/
# Réponse renvoyée par le serveur Varnish
<!DOCTYPE html>
<html>
<head>
<title>200 Purged</title>
</head>
<body>
<h1>Erreur 200 : Purge effectuée</h1>
<p>La page a été purgée avec succÚs.</p>
<h3>Guru Meditation:</h3>
<p>XID: 2</p>
<hr>
<p>Serveur de cache Varnish</p>
</body>
</html>
And there, the homepage has been purged. Or to purge another URL, simply point the request to the latter:
# Envoyer une requĂȘte PURGE pour purger un fichier spĂ©cifique Ă "domain.xyz"
$ curl -X PURGE https://domain.xyz/some_path/some_file.html
# Réponse renvoyée par le serveur Varnish
<!DOCTYPE html>
<html>
<head>
<title>200 Purged</title>
</head>
<body>
<h1>Erreur 200 : Purge effectuée</h1>
<p>Le fichier a été purgé avec succÚs.</p>
<h3>Guru Meditation:</h3>
<p>XID: 4</p>
<hr>
<p>Serveur de cache Varnish</p>
</body>
</html>
Or, as indicated in the VCL configuration, purge all images:
# Envoyer une requĂȘte PURGEALL pour purger toutes les images dans "domain.xyz"
$ curl -X PURGEALL https://domain.xyz/images
# Réponse renvoyée par le serveur Varnish
<!DOCTYPE html>
<html>
<head>
<title>200 Purged images</title>
</head>
<body>
<h1>Erreur 200 : Images purgées</h1>
<p>Toutes les images ont été purgées avec succÚs.</p>
<h3>Guru Meditation:</h3>
<p>XID: 32770</p>
<hr>
<p>Serveur de cache Varnish</p>
</body>
</html>
Purge from a CMS
It is a bit more difficult to illustrate this case because there are many ways to manage caching from a backend. In the configuration example above, a control on the header "Uncacheable
" is added, which disables caching. With this option, any CMS could simply set this header on the response to disable caching for this request, for example.
From any PHP code and with the configuration above, you can simply send an HTTP request and use this snippet to perform a PURGE of the cache:
<?php
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) to control request processing, routing, caching and several other aspects: