Download the PHP package studioespresso/craft-varnish-purger without Composer

On this page you can find all versions of the php package studioespresso/craft-varnish-purger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package craft-varnish-purger

Varnish Purger for Craft CMS

Screenshot

Cache your Craft site in Varnish, and purge exactly the pages that changed when content is saved.

Every page Varnish caches is labelled with what it shows: the entries, assets and categories it outputs, the sections it lists, the Matrix fields it renders. When an editor saves something, the plugin tells Varnish to drop every page with a matching label. Edit an event and only the event page and the listings that show it are purged; the rest of the site stays cached.

The labels are Craft's own element cache tags, the ones behind {% cache %}, so the plugin follows Craft's rules for what a save affects. It uses Varnish's built-in bans, so no Varnish modules are needed.

[!IMPORTANT] This plugin only works together with a VCL tuned for it, and that VCL depends on the site and its hosting. The plugin tags pages and sends BAN requests; Varnish has to cache those tagged pages and turn the BANs into bans. The example VCLs are a starting point, not a drop-in config. Expect to adapt them to things like:

  • Your Varnish version: std.ban() needs Varnish 6.6+; on 6.0, use example-varnish-6.0.vcl.
  • How requests reach Varnish: behind a proxy, load balancer or CDN, the client IP that acl purge should check may arrive in a header set by that proxy instead of client.ip, and the Craft server may reach Varnish by yet another route.
  • Managed hosting: hosts often run their own VCL template, a bypass switch, or a separate purge endpoint that handles BAN/PURGE itself and never reaches your VCL. Find out how to load your own VCL and where it receives requests.
  • The site itself: backend address, what must never be cached (login areas, carts, per-visitor content), cookies, and the hostnames it's served under.

Verify the result on the actual setup: php craft varnish/check must answer Ban added, pages must show X-Cache: HIT on a repeat request, and saving an entry must turn its page into a MISS. Test before pointing live traffic at Varnish.

Requirements

Installation

Then set up Varnish and tell the plugin where it lives.

1. Set up Varnish

Start from example-varnish-6.0.vcl on Varnish 6.0 (check with varnishd -V, varnishadm banner, or the Via response header). The 6.0 version uses ban() instead of std.ban(), so a ban Varnish can't parse isn't reported back to Craft; otherwise they're identical. Either one:

Adapt these before going live:

In example.vcl Change to
backend default the host and port of your web server
acl purge the IP addresses of your Craft server(s) only
# unset resp.http.X-Cache-Tags; and # unset resp.http.X-Cache-Site; uncomment them, so visitors don't see the tags
admin in the (admin\|actions\|index\.php) pass rule your cpTrigger, if you changed it
the novarnish block remove it: it's for ddev only

Also set Craft's trustedHosts config setting to the IP addresses of Varnish (and any proxy in front of Craft). By default Craft trusts forwarded headers from any client. The example VCL drops the risky ones, but a request that reaches Craft without passing through Varnish still gets them trusted.

2. Tell Craft where Varnish lives

Go to Settings → Plugins → Varnish and add each Varnish server: its host and port.

Or configure the servers in config/varnish.php. This overrides the CP setting and locks the table. Use Craft's per-environment format to give each environment (CRAFT_ENVIRONMENT) its own servers:

Two more options, for hosting setups (config file only):

If no servers are configured, pages are still tagged and cached but nothing is ever purged (logged at info level, so visible with devMode on).

Pages with a lot of tags (long listings showing hundreds of elements) are not tagged, and so not cached, once the X-Cache-Tags header would exceed maxTagsHeaderLength (4000 bytes by default). A warning is logged. A header over Varnish's http_resp_hdr_len (8 KB by default), or over the header buffer of a proxy in between, would fail the whole response. Raise the setting together with those limits if needed.

3. Check that it works

After deploying, check that every configured server accepts bans from this server:

It sends a harmless test ban (a tag no page carries) and exits with an error code if any server fails, so it can run in a deploy script. When Varnish refuses a ban, the answer names the IP it saw: that's the address to add to acl purge.

Then check caching itself:

Request a page twice and look at the headers:

Save one of the entries on that page in the CP, and the next request is a MISS again. Every ban is logged per server with Varnish's answer: failures always (Varnish ban failed (http://… → 403 Forbidden: BAN from 10.0.0.5 …)), successes with devMode on (Varnish banned: … (http://… → 200 Ban added)).

ESI (Edge Side Includes)

Render part of a cached page in its own request, so it can change on every page view while the page around it stays cached:

example.vcl handles both sides: it sets Surrogate-Capability on requests (so Craft knows it can output ESI tags), and processes responses that send Surrogate-Control: content="ESI/1.0".

Purging everything

Use Utilities → Caches → Varnish cache, or from the command line, which shows each server's answer:

php craft clear-caches/varnish purges everything too. All of these respect the hostnames setting.

How pages are tagged

While the page template renders, Craft records:

Tags are shortened to keep the header small:

Tag Means the page… Purged when…
12 outputs element 12 element 12 is saved, deleted, restored or moved
e:s:3 lists entries from section 3 any entry in section 3 changes, including new ones
e:t:4, e:f:6 queries entry type 4, or the Matrix field 6 an entry of that type, or in that field, changes
e:any runs an entry query with no scope any entry changes
e:search runs an entry search Craft finishes updating the search index for an entry
all is cached everything is purged

The element type comes first: e entry, c category, a asset, u user, t tag, g global set, b content block, ad address, p Commerce product, v Commerce variant. Then the scope: s section, t entry type, f field, o owner, g group, v volume, pt product type.

Some details:

Purge requests

When content changes, the plugin collects the tags Craft invalidates and, at the end of the request (or after each queue job, for queue workers), sends one request to each Varnish server:

X-Cache-Site-Ban is only sent when the change was limited to one site, and X-Cache-Hosts-Ban only when hostnames is configured. The VCL stores the hostname each page was cached under (X-Cache-Host, hidden from visitors) to match it.

Varnish then drops every cached page carrying one of those tags, whatever its URL. Requests go to all servers in parallel. An unreachable server is logged and skipped, and doesn't slow down saving by more than a few seconds.

Known limitations

Development

The tests use Codeception with Craft's test module, and need an empty testing database. The suite wipes and reinstalls it on every run.

tests/_craft/config/db.php sets the connection, and can be overridden with DB_SERVER, DB_USER and DB_PASSWORD. tests/_bootstrap.php clears any CRAFT_DB_* environment variables first, because hosts like ddev set those and they would otherwise point the suite at your project's database. It refuses to run unless the database resolves to testing.

In ddev:

CI runs ECS, PHPStan and the tests on every push and pull request (.github/workflows/ci.yml).

AI agents working with this plugin (installing it, writing a VCL, debugging a hosting setup) should also read AGENTS.md: it has the background, edge cases and hosting pitfalls in full.


All versions of craft-varnish-purger with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
craftcms/cms Version ^5.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package studioespresso/craft-varnish-purger contains the following files

Loading the files please wait ...