12 October 2026

Joomla and Varnish Cache: The Session Cookie Problem and How to Reduce TTFB to a Few Milliseconds

Joomla assigns a session cookie even to anonymous visitors, and Varnish, to be safe, doesn't cache anything. Here's how to fix this without breaking logins, forms, and restricted areas.

Joomla session cookie blocks Varnish cache

Anyone who runs Varnish on a Joomla site expects response times to plummet. Often, the opposite happens: the server works as before, the database processes the same queries, and Varnish logs show almost nothing but MISSES and PASSES. The hasty conclusion is that "Varnish doesn't work with Joomla." In reality, Varnish works perfectly: it's Joomla, in its standard configuration, that prevents it from doing its job.

In this article I explain why this happens, how to solve it with a custom VCL configuration and what are the two traps that break the site if the cache is activated without criteria: logged in users and form security tokens.

The problem: a session cookie even for those who haven't logged in

Varnish, to be on the safe side, doesn't cache requests that arrive with a cookie or responses that set one. This is a sensible choice: a cookie can identify a user, and a personal page should never be served to someone else.

Joomla, in its default configuration, starts a session for each visitor, even those who have never logged in, and sends the corresponding session cookie right from the first page. From that moment on, every request from that visitor carries the cookie. The result is that Varnish treats every anonymous visitor as a potentially personal user and sends everything to PHP. Caching is present, but it is almost never used.

There is a second detail that completes the picture: Joomla responds with headers that prohibit caching (Cache-Control: no-cache and an expiration date in the past). Even if you removed the cookie, Varnish would receive a response stating that it could not be retained.

Joomla request paths with and without session cookies in Varnish
With the session cookie every request ends up in PHP; removing it from anonymous users makes it a HIT.

How much is cache really worth?

To give an idea of ​​the scale, I externally measured our site, which is a WordPress site running Varnish, on the same page and over the same connection. Without caching, the page loads in 0,63-0,69 seconds; served by Varnish, it loads in 0,10-0,12 seconds, of which 0,07 are just the connection and TLS negotiation time from my workstation. Net of the network, that's 30-50 milliseconds versus over half a second of processing time.

On Joomla, the ratio is the same, often even more pronounced on sites with many modules and extensions. But the profit depends entirely on one thing: how many requests become hits. With the session cookie enabled for everyone, that percentage is close to zero.

Response time with and without Varnish cache, real-world measurement
Real-world measurement: over half a second without cache, a few tens of milliseconds with Varnish

The solution: three rules to follow

The correct strategy can be summed up in three rules:

  1. anonymous visitors receive pages from the cache, and for them Varnish ignores the session cookie;
  2. Authenticated users never go through the cache, because they see personal pages;
  3. the pages with forms, the administrative area, the login, the cart and the checkout are always excluded.

The problem is distinguishing an anonymous user from a logged-in user, since both have a session cookie with an indistinguishable name. Joomla doesn't provide a proxy indicator, so we create one ourselves: a small system plugin that sets a dedicated cookie upon login, for example: ms_loggato=1, and deletes it when you log out. Joomla 4 and 5 provide the right events (onUserAfterLogin e onUserLogout) and the plugin is written in a few lines. The plugin itself can send Cache-Control: private to authenticated users, so no intermediate cache stores their pages.

The VCL configuration

Here is a base for Varnish 6 or 7, to be adapted to the individual site:

vcl 4.1;

sub vcl_recv {
    # Solo GET e HEAD possono finire in cache
    if (req.method != "GET" && req.method != "HEAD") {
        return (pass);
    }

    # Area amministrativa, utenti, form e percorsi dinamici
    if (req.url ~ "^/administrator" ||
        req.url ~ "option=com_users" ||
        req.url ~ "[?&]task=" ||
        req.url ~ "^/(login|accedi|registrazione|carrello|checkout)") {
        return (pass);
    }

    # Utente autenticato o con "ricordami": niente cache
    if (req.http.Cookie ~ "ms_loggato=1" ||
        req.http.Cookie ~ "joomla_remember_me_") {
        return (pass);
    }

    # Parametri di tracciamento: non cambiano la pagina
    if (req.url ~ "[?&](utm_[a-z]+|gclid|fbclid|msclkid)=") {
        set req.url = regsuball(req.url, "(utm_[a-z]+|gclid|fbclid|msclkid)=[^&]*&?", "");
        set req.url = regsub(req.url, "[?&]+$", "");
    }

    # Visitatore anonimo: il cookie di sessione non serve
    unset req.http.Cookie;
    return (hash);
}

sub vcl_backend_response {
    if (bereq.uncacheable) {
        return (deliver);
    }
    # Joomla invia un cookie di sessione e "no-cache" anche agli ospiti
    unset beresp.http.Set-Cookie;
    unset beresp.http.Cache-Control;
    unset beresp.http.Expires;
    unset beresp.http.Pragma;
    set beresp.ttl = 10m;
    set beresp.grace = 6h;
    return (deliver);
}

Some notes on these rules. Excluded paths should be adapted to the site's actual menu aliases: if the login page is called /area-riservata, is the one that should be excluded. The cookie joomla_remember_me_ It is used to not "forget" users who have chosen to stay connected: without that rule, Varnish would remove the cookie and Joomla would not be able to recognize them. grace six hours allows Varnish to serve a newly expired copy while preparing a new one, preventing a traffic spike from hitting PHP all at once just as the cache expires.

The Security Token Trap

Joomla protects forms with a CSRF token: a code inserted into the HTML that must match the session of the person submitting the form. If a page containing a form is cached, all visitors receive the same token, which doesn't match any of their sessions. The result is the classic invalid token error when submitting.

The most common case is the login form displayed on all pages , perhaps in the sidebar or header. With caching enabled, it becomes unusable. There are two clean solutions: show the login form only on the login page, which is excluded from the cache, or replace it with a simple link to that page. The same goes for contact forms and any forms placed on public pages: either they live on excluded pages or are loaded separately.

Is Joomla Cache Still Needed?

Joomla has its own caching system: the system cache (conservative or progressive) and the "System – Page Cache" plugin, which stores entire pages for guest visitors. With Varnish in place, Joomla's page cache becomes largely redundant: Varnish responds before PHP even starts, so the entire pages stored by Joomla are rarely requested.

The conservative module cache , however, remains useful: it speeds up page generation when Varnish needs to query PHP, i.e., on MISS pages and for authenticated users. This is the combination we recommend: Varnish for entire pages, Joomla's conservative cache for fragments.

Refresh cache when publishing

Joomla doesn't have native integration with Varnish to clear modified pages. With a TTL of ten minutes, the problem is limited: a modified article appears updated within a few minutes. When immediate updates are needed, the same system plugin can intercept the saving of content (event onContentAfterSave) and ask Varnish to invalidate the article's page, its category, and its home page, with a request authorized only by the server itself. This way, you can extend the TTL and further increase the HIT rate.

How to check if it works

Three checks are enough:

  • from anonymous visitor, reloading a page twice, the second response must come from Varnish (the header Age becomes greater than zero);
  • from logged in user, pages must show your own name and menu, never someone else's;
  • the forms (login, contacts, registration) must work on the first submission, without token errors.

On a production site it is then worth looking at the ratio between HIT and MISS for a few days with varnishstat: This is the number that tells you whether the configuration is actually easing the load on the server.

Conclusions

Varnish and Joomla work very well together, as long as you understand their conflict: the session cookie assigned to everyone. By removing it from anonymous users, recognizing authenticated users with a dedicated cookie, and keeping forms and restricted areas out of the cache, you can go from an unused cache to response times of a few tens of milliseconds for the vast majority of visits.

If your Joomla site is slow or you've already tried Varnish without success, we can configure cache, plugins, and exclusions tailored to your site on our Joomla hosting , and with our Varnish Cache support service, we can also work on existing infrastructures.

Do you have doubts? Don't know where to start? Contact us!

We have all the answers to your questions to help you make the right choice.

Chat with us

Chat directly with our presales support.

0256569681

Contact us by phone during office hours 9:30 - 19:30

Contact us online

Open a request directly in the contact area.

DISCLAIMER, Legal Notes and Copyright. RedHat, Inc. holds the rights to Red Hat®, RHEL®, RedHat Linux®, and CentOS®; AlmaLinux™ is a trademark of the AlmaLinux OS Foundation; Rocky Linux® is a registered trademark of the Rocky Linux Foundation; SUSE® is a registered trademark of SUSE LLC; Canonical Ltd. holds the rights to Ubuntu®; Software in the Public Interest, Inc. holds the rights to Debian®; Linus Torvalds holds the rights to Linux®; FreeBSD® is a registered trademark of The FreeBSD Foundation; NetBSD® is a registered trademark of The NetBSD Foundation; OpenBSD® is a registered trademark of Theo de Raadt; Oracle Corporation holds the rights to Oracle®, MySQL®, MyRocks®, VirtualBox®, and ZFS®; Percona® is a registered trademark of Percona LLC; MariaDB® is a registered trademark of MariaDB Corporation Ab; PostgreSQL® is a registered trademark of PostgreSQL Global Development Group; SQLite® is a registered trademark of Hipp, Wyrick & Company, Inc.; KeyDB® is a registered trademark of EQ Alpha Technology Ltd.; Typesense® is a registered trademark of Typesense Inc.; REDIS® is a registered trademark of Redis Labs Ltd; F5 Networks, Inc. owns the rights to NGINX® and NGINX Plus®; Varnish® is a registered trademark of Varnish Software AB; HAProxy® is a registered trademark of HAProxy Technologies LLC; Traefik® is a registered trademark of Traefik Labs; Envoy® is a registered trademark of CNCF; Adobe Inc. owns the rights to Magento®; PrestaShop® is a registered trademark of PrestaShop SA; Shopware® is a registered trademark of Shopware AG; OpenCart® is a registered trademark of OpenCart Limited; Automattic Inc. holds the rights to WordPress®, WooCommerce®, and JetPack®; Open Source Matters, Inc. owns the rights to Joomla®; Dries Buytaert owns the rights to Drupal®; Shopify® is a registered trademark of Shopify Inc.; BigCommerce® is a registered trademark of BigCommerce Pty. Ltd.; TYPO3® is a registered trademark of the TYPO3 Association; Ghost® is a registered trademark of the Ghost Foundation; Amazon Web Services, Inc. owns the rights to AWS® and Amazon SES®; Google LLC owns the rights to Google Cloud™, Chrome™, and Google Kubernetes Engine™; Alibaba Cloud® is a registered trademark of Alibaba Group Holding Limited; DigitalOcean® is a registered trademark of DigitalOcean, LLC; Linode® is a registered trademark of Linode, LLC; Vultr® is a registered trademark of The Constant Company, LLC; Akamai® is a registered trademark of Akamai Technologies, Inc.; Fastly® is a registered trademark of Fastly, Inc.; Let's Encrypt® is a registered trademark of the Internet Security Research Group; Microsoft Corporation owns the rights to Microsoft®, Azure®, Windows®, Office®, and Internet Explorer®; Mozilla Foundation owns the rights to Firefox®; Apache® is a registered trademark of The Apache Software Foundation; Apache Tomcat® is a registered trademark of The Apache Software Foundation; PHP® is a registered trademark of the PHP Group; Docker® is a registered trademark of Docker, Inc.; Kubernetes® is a registered trademark of The Linux Foundation; OpenShift® is a registered trademark of Red Hat, Inc.; Podman® is a registered trademark of Red Hat, Inc.; Proxmox® is a registered trademark of Proxmox Server Solutions GmbH; VMware® is a registered trademark of Broadcom Inc.; CloudFlare® is a registered trademark of Cloudflare, Inc.; NETSCOUT® is a registered trademark of NETSCOUT Systems Inc.; ElasticSearch®, LogStash®, and Kibana® are registered trademarks of Elastic NV; Grafana® is a registered trademark of Grafana Labs; Prometheus® is a registered trademark of The Linux Foundation; Zabbix® is a registered trademark of Zabbix LLC; Datadog® is a registered trademark of Datadog, Inc.; Ceph® is a registered trademark of Red Hat, Inc.; MinIO® is a registered trademark of MinIO, Inc.; Mailgun® is a registered trademark of Mailgun Technologies, Inc.; SendGrid® is a registered trademark of Twilio Inc.; Postmark® is a registered trademark of ActiveCampaign, LLC; cPanel®, LLC owns the rights to cPanel®; Plesk® is a registered trademark of Plesk International GmbH; Hetzner® is a registered trademark of Hetzner Online GmbH; OVHcloud® is a registered trademark of OVH Groupe SAS; Contabo® is a registered trademark of Contabo GmbH; IONOS® is a registered trademark of IONOS SE; International Business Machines Corporation owns the rights to IBM® and IBM Cloud®; Terraform® is a registered trademark of HashiCorp, Inc.; Ansible® is a registered trademark of Red Hat, Inc.; cURL® is a registered trademark of Daniel Stenberg; Facebook®, Inc. owns the rights to Facebook®, Messenger® and Instagram®. This site is not affiliated with, sponsored by, or otherwise associated with any of the above-mentioned entities and does not represent any of these entities in any way. All rights to the brands and product names mentioned are the property of their respective copyright holders. All other trademarks mentioned are the property of their respective registrants. MANAGED SERVER® is a European registered trademark of MANAGED SERVER SRL, with registered office in Via Flavio Gioia, 6, 62012 Civitanova Marche (MC), Italy and operational headquarters in Via Enzo Ferrari, 9, 62012 Civitanova Marche (MC), Italy.

JUST A MOMENT !

Have you ever wondered if your hosting sucks?

Find out now if your hosting provider is hurting you with a slow website worthy of 1990! Instant results.

Close the CTA
Back to top