Integrating Varnish Cache with a PHP application

Posted by on Jul 6, 2013 in Environment, Linux, PHP Programming | No Comments

Varnish Cache is a web application accelerator. It stands in front of a web server and can cache any type of data. It stores data in memory and can speed up your application by a factor of 300-1000x depending on your architecture.

It’s not my first post about Varnish Cache (Boost WordPress performance) but this time I’m going to show a generic example which can work with any type of PHP application.

You can install Varnish Cache via good old “apt-get” although I prefer to compile it from sources. The reason for that is I usually use it with memcached module which requires Varnish Cache source code.

Varnish requires libpcre.

Install the software.

If you didn’t use any –prefix= the software should be installed under /usr/local.

Config file should be in /usr/local/etc/varnish/default.vcl but it’s not that important.

Now it’s a time to create a very simple PHP script and save it as index.php.

You might be wondering what is the ESI tag. It stands for Edge Side Includes and it’s a very cool feature.

A web page usually consists of multiple blocks. Some of them like layout change almost never while other might be fully dynamic. Varnish Cache allows to break down a page into such a blocks and cache them with a different expire time. Depends on your needs you can setup Varnish to pull those blocks from different web servers (for example you can have a dedicated host for a real time data).

Going back to our example Varnish will replace “” with a content from “/time.php“. Everything inside the

tag will be removed from the page.

Lets create the time.php script.

It couldn’t be simpler.

Right now you should have 2 pages:

Now it’s the time to create the varnish configuration file.

If you are new to Varnish Cache this might look little bit overwhelming but I assure you there is no magic here. This is the default configuration which is well explained in the manual. What’s interesting from our example’s point of view is inside vcl_fetch.

For the “/” request we turn the ESI processing on and we cache content from this location for 120 seconds. Content returned from “/time.php” will be stored only for 5 seconds.

Lets run varnish and give it a go.

One thing to notice is “-d” flag at the end of the above line. That will run Varnish Cache in debug mode so you have to type “start” to run it.

No open new tab in your web browser and visit

You should see something like that:

Hello World
Cache from: Sat, 06 Jul 13 22:20:47 +0100
Cache from: Sat, 06 Jul 13 22:20:47 +0100

Interesting thing happen when you refresh the page. First two line should stay the same for 2 minutes while the last one should change every 5 seconds. Isn’t that great?

This is not everything. There are cases when you have to invalidate cache without waiting for it to expire.

Varnish 3.x allows to ban cached data Modify the default.vcl file.

Obviously in the production environment you need additional condition to allow calling “/clearcache” only from certain IP addresses.

Stop Varnish server (ctrl + c) and start it again (don’t forget to type “start”).

Now if you go to cache for “/” will be invalidated. You can see all active bans in you server console by typing ban.list.

Varnish will add bans only if there is a cached content (for that rule).

The last thing is to call the clearcache URL from PHP. After all we don’t want to manually refresh that page.

Lets create another script and call it clearcache.php.

Now you can visit to give it a go.

If you need to troubleshoot your VCL script put

in the first line and echo data with

Debug data will be pushed to the Varnish Cache log and to read it run:

Thank you for getting to the end of this post. Varnish Cache is a great peace of software and it’s worth knowing it. It’s little bit techie and programming VCL script could be easier but it will make your application fly.

Leave a Reply