Knowledge base
1000 FAQs, 500 tutorials and instructional videos. Here, there are only solutions!
Using Varnish on Cloud Server
This guide presents several examples of using Varnish on Infomaniak Cloud Server.
Preamble
- Consult these additional resources on the Varnish Configuration Language (VCL) to master request processing, routing, and caching:
- If needed, local partners referenced by Infomaniak can handle these procedures: launch a free call for tenders; they take care of everything, freeing you from technical details — also discover the role of the host.
Varnish Configuration
Once installed, Varnish configuration is based on precise caching and purging rules. Make sure to restrict access to prevent unauthorized entities from emptying your cache.
Here is an example of a configuration file that groups the most frequent use cases:
vcl 4.0;
# Default backend configuration
backend default {
.host = "127.0.0.80"; # Backend IP address
.port = "80"; # Backend port
}
# Access Control List (ACL) for purge authorization
acl purge {
"localhost"; # Local access
"1.2.3.4"; # Trusted home IP
"42.42.42.0"/24; # Trusted company range
! "42.42.42.7"; # Specific IP exclusion (e.g., problematic user)
}
# Handle incoming requests
sub vcl_recv {
# Handle PURGE requests
if (req.method == "PURGE") {
# Check if client IP is authorized
if (!client.ip ~ purge) {
return (synth(405, "IP not authorized for PURGE requests."));
}
return (purge);
}
# Custom PURGEALL for image directory
if (req.method == "PURGEALL" && req.url == "/images") {
if (!client.ip ~ purge) {
return (synth(405, "IP not authorized for PURGEALL requests."));
}
# Invalidate all image-related objects in cache
ban("req.url ~ \.(jpg|png|gif|svg)$");
return (synth(200, "Images purged."));
}
# Bypass cache for authorized requests (e.g., admin panels)
if (req.http.Authorization) {
return (pass);
}
}
# Handle backend responses before caching
sub vcl_backend_response {
# Set TTL for images to 1 day
if (beresp.http.content-type ~ "image") {
set beresp.ttl = 1d;
}
# Respect backend's "uncacheable" instruction
if (beresp.http.uncacheable) {
set beresp.uncacheable = true;
}
}
Purge via the CLI interface
Once your rules are active, you can test the purge of your site (e.g., "domain.xyz") using the curl tool:
# Purge the homepage
$ curl -X PURGE https://domain.xyz/
# Expected Varnish response
<!DOCTYPE html>
<html>
<head>
<title>200 Purged</title>
</head>
<body>
<h1>Success 200: Purge completed</h1>
<p>The page has been successfully purged.</p>
<h3>Guru Meditation:</h3>
<p>XID: 2</p>
<hr>
<p>Varnish Cache Server</p>
</body>
</html>To purge a specific URL, simply modify the request path:
# Purge a specific file
$ curl -X PURGE https://domain.xyz/some_path/some_file.html
# Expected Varnish response
<!DOCTYPE html>
<html>
<head>
<title>200 Purged</title>
</head>
<body>
<h1>Success 200: Purge completed</h1>
<p>The file has been successfully purged.</p>
<h3>Guru Meditation:</h3>
<p>XID: 4</p>
<hr>
<p>Varnish Cache Server</p>
</body>
</html>Or to trigger the grouped purge of images defined in the VCL:
# Execute PURGEALL for images
$ curl -X PURGEALL https://domain.xyz/images
# Expected Varnish response
<!DOCTYPE html>
<html>
<head>
<title>200 Purged images</title>
</head>
<body>
<h1>Success 200: Images purged</h1>
<p>All images have been successfully purged.</p>
<h3>Guru Meditation:</h3>
<p>XID: 32770</p>
<hr>
<p>Varnish Cache Server</p>
</body>
</html>
Purge from a CMS (PHP)
Cache management can also be done dynamically via your backend. In the previous configuration, we added a control over the Uncacheable header. Your CMS can send this header to force Varnish not to store a response.
Here is how to send a programmatic purge request in PHP:
<?php
// Initialize cURL for a specific URL
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']}" // Match the target host
]
]);
curl_exec($curl);
// Check if the purge was successful (HTTP 200)
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200) {
echo "Cache purged!";
}
curl_close($curl);
}
?>Link to this FAQ:
Has this FAQ been helpful?