In a few of my posts I’ve talked about the benefits of page cache systems like Varnish. Today we’ll demonstrate how to install it! Before continuing, be aware that this guide assumes you’re using Ubuntu on your server.
Why use Varnish?
Firstly, let’s talk about why page cache is fantastic. For dynamic page generation languages like PHP, the amount of server processing power it takes to build a page compared to serving a static file (like HTML) is substantially more. Since the page has to be rebuilt with each new user to request it, the server does a lot of redundant work. But this also allows for more customization to your users since you can tell the server to build the page differently based on different conditions (geolocation, referrer, device, campaign, etc).
That being said, using persistent page cache is an easy way to get the best of both worlds: cache holds onto a static copy of the page that was generated for a period of time, and then the page can be built as new whenever the cache expires. In short, page cache allows your pages to load in a few milliseconds rather than 1+ full seconds.
Installing Varnish
To install Varnish on a system using Ubuntu, you’ll use the package installer. While logged into your server (as a non-root user), run the following:
sudo apt install varnish
Be sure the Varnish service is stopped while you configure it! You can stop the Varnish service like this:
sudo systemctl stop varnish
Now it’s time to configure the Varnish settings. Make a copy of the default configuration file like so:
cd /etc/varnish sudo cp default.vcl mycustom.vcl
Make sure Varnish is configured for the right port (we want port 80 by default) and the right file (our mycustom.vcl file):
sudo nano /etc/default/varnish
DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/mycustom.vcl \ -S /etc/varnish/secret \ -s malloc,256m"
Configuring Varnish
The top of your mycustom.vcl file should read like this by default:
backend default { .host = "127.0.0.1"; .port = "8080"; }
This line defines the “backend,” or which port to which Varnish should pass uncached requests. Now we want to configure the web server to listen on the right port. Nginx will listen on port 8080 by default, but if you’re using Apache you may need to modify the port in your /etc/apache2/ports.conf file and /etc/apache2/sites-enabled/000-default.conf to reference port 8080.
From here you can begin to customize your configuration! You can tell Varnish what requests to add X-Group headers for, which pages to strip out cookies on, how and when to purge the cache, and more. You probably only want to cache GET and HEAD methods for requests, as POST requests should always be uncached. Here’s a basic rule that says to add a header saying not to cache anything that’s not GET and HEAD:
sub vcl_recv { if (req.request != "GET" && req.request != "HEAD") { set req.http.X-Pass-Method = req.request; return (pass); } }
And here’s an excerpt which says not to cache anything with the path “wp-admin” (a common need for sites with WordPress):
sub vcl_recv { if (req.http.host == "mysite.com" && req.url ~ "^/wp-admin") { return (pass); } }
There’s a ton of other fun custom configurations you can add. To research the available options and experiment with them, check out the book from Varnish.
Once you’ve added in your customizations, be sure to start Varnish:
sudo systemctl start varnish
Now what?
Now you have Varnish installed and configured! Your site will cache pages and purge the cache based on the settings you’ve configured in the mycustom.vcl file. Using cache and caching heavily will heavily benefit your site performance. And, it’ll help your site scale to support more traffic at a time. Enjoy!
Have more questions about Varnish? Confused about how cache works? Any cool cache rules you use in your own environment? Let me know in the comments or contact me.
Leave a Reply