A machine-readable guide to this site for AI agents and language models is available at/llms.txt(https://dev.acquia.com/llms.txt on the production site).
Varnish Cache is a caching reverse proxy installed in front of all Cloud Platform load balancing infrastructure. To improve application performance, Varnish caches connections from anonymous users and serves responses from memory. This reduces the need to forward requests to the Apache web infrastructure. For more details, visit Introduction to Varnish.
Varnish caches responses to anonymous user requests. It also caches static assets such as images, JavaScript, and CSS for both anonymous and authenticated users.
When you use ISR or similar cache revalidation on a site served by multiple load-balanced caching nodes, a single PURGE request sent to the public hostname may not sufficiently clear the cache. Each cache node maintains its own version of the cached content, and a purge request may only affect one node. To ensure immediate content updates, perform cache purging across all load balancers behind the hostname. Use correct host routing and secure handling when HTTPS is enabled. This method ensures all cache nodes clear their content, which prevents stale pages and ensures all users see the latest version without inconsistencies.
The customer can adapt the following code snippet to clear the varnish cache for specific endpoints.
How this implementation works:
POST /api/clear-varnish-cache.
const dns = require("dns").promises;
const https = require("https");
const axios = require("axios");
/**
* Purges a specific path from all cache nodes behind a hostname.
*
* @param {string} site_name - The Acquia site name (for example, "myappdev" or "myappprod").
* @param {string} host - The hostname to purge (for example, "myappdev2.com" or "myappdev2.prod.acquia-sites.com").
* @param {string} [protocol="https"] - The protocol to use, typically "http" or "https".
* @param {string} [path="/"] - The path to purge (for example, "/blog/my-post").
* @returns {Promise<Array<{ip: string, status?: number, error?: string}>>} Per-node purge results.
* @throws {Error} Thrown when the hostname does not resolve to any IPv4 or IPv6 addresses.
*
* @example
* await purgeHost("myappdev2", "www.example.com", "https", "/blog/page");
*/
async function purgeHost(site_name, host, protocol = "https", path = "/") {
const ips = [];
// Resolve IPv4 addresses
const ACQUIA_DEFAULT_FQDN = `${site_name}.prod.acquia-sites.com`
try {
ips.push(...await dns.resolve4(ACQUIA_DEFAULT_FQDN));
} catch { }
// Resolve IPv6 addresses
try {
ips.push(...await dns.resolve6(ACQUIA_DEFAULT_FQDN));
} catch { }
if (!ips.length) {
throw new Error(`No IPs found for ${ACQUIA_DEFAULT_FQDN}`);
}
const results = await Promise.all(
ips.map(async (ip) => {
const targetHost = ip.includes(":") ? `[${ip}]` : ip;
const url = `${protocol}://${targetHost}${path}`;
const config = {
method: "PURGE",
url,
timeout: 5000,
headers: {
'X-Acquia-Purge': site_name,
'User-Agent': 'Cache-Purge-Agent',
'Host': host,
},
validateStatus: () => true,
};
if (protocol === "https") {
config.httpsAgent = new https.Agent({
servername: host,
rejectUnauthorized: false,
});
}
try {
const res = await axios(config);
return { ip, status: res.status };
} catch (err) {
return { ip, error: err.message };
}
})
);
console.log(results)
return results;
}On Front End Hosting – Advanced, the Varnish cache layer can be bypassed automatically based on certain cookie patterns:
SESS or SSESSPERSISTENT_LOGIN_NO_CACHEAuthenticated users who send any of these cookies bypass Varnish and connect directly to the application. This provides access to draft or unpublished content for logged-in users, while maintaining strong caching for anonymous users.
If this content did not answer your questions, try searching or contacting our support team for further assistance.
If this content did not answer your questions, try searching or contacting our support team for further assistance.