Varnish
Using Varnish as a caching layer in front of your application can improve your website or application’s performance in great ways.
Installation / Configuration
Varnish is fully configured through the Custom JSON Server Level Configuration.
Enable
To install and enable Varnish on your server, set varnish::ensure
to present.
Listening address and port
By default, Varnish will listen on the localhost interface on port 8022.
If Varnish should bind to other addresses or a different port, use the varnish::address
and varnish::address6
and corresponding varnish::port
and varnish::port6
parameters to specify them.
Backend
After evaluating caches, Varnish will forward requests that cannot be served from cache to the backend application.
By default, Varnish will use 127.0.0.1 as backend host and 8080 as backend port.
If Varnish has to use another backend, use the varnish::backend_host
and varnish::backend_port
parameter to specify them.
vcl_type
With varnish::vcl_type
, you choose a template which is used by Varnish as default VCL configuration.
By now, the following types are available:
default
: Varnish default configuration which does not very much but is perfectly suitable for your own, custom configuration troughvarnish::vcl_include
typo3
: Varnish configuration for thevarnish
TYPO3 extension (see GitLab)
vcl_include
Varnish uses HTTP headers to decide whether a request should be cached or not. See the chapter The role of HTTP Headers in the official Varnish documentation.
With varnish::vcl_include
, you can define a full path to an additional configuration file.
This file gets included into the Varnish default configuration.
Tip
Keep in mind to issue a puppet-agent
run after changing the local Varnish configuration.
Puppet will copy your local configuration file to a global location and ensure that Varnish is able to read it.
Memory Ratio
By default, a varnish::memory_ratio
of 2 is used, which means Varnish will take up to 50% of this servers total memory.
Daemon Options
If needed, you can submit further startup options that are passed on when starting the Varnish daemon.
For this, use varnish::daemon_options
.
Example configurations
Minimal example
Configuration through Custom JSON Server Level Configuration.
{
"varnish::ensure": "present"
}
Varnish uses the default configurations as described above.
Full example
Configuration through Custom JSON Server Level Configuration.
{
"varnish::ensure": "present",
"varnish::address": "192.168.1.1",
"varnish::port": "80",
"varnish::address6": "2001:db8::1",
"varnish::port6": "80",
"varnish::vcl_type": "default",
"varnish::vcl_include": "/home/user/cnf/varnish.vcl",
"varnish::memory_ratio": "4",
"varnish::daemon_options": "-p vcc_allow_inline_c=on",
"varnish::backend_host": "127.0.0.1",
"varnish::backend_port": "8080"
}
Monitoring
Varnish is monitored by Monit, a service which will restart varnish if varnish is not available.
To check whether Varnish is available, an HTTP request is sent with _
as host header.
You can test this manually as follows:
curl -I -H "Host: _" -A "Monit/5.27.1" http://127.0.0.1:8022/
curl -I -H "Host: _" -A "Monit/5.27.1" http://<address>:<port>/
Please ensure that this request is answered with an HTTP 200. Our standard varnish configuration includes the following snippet. You may need to add this to your own configuration.
if (req.http.host ~ "^_" && req.http.User-Agent ~ "(check_http|Monit)") {
return (synth(200, "Varnish up and running smoothly."));
}
Tools
You can run these tools by login with the devop user (see Generic Admin User).
varnishlog
: Display Varnish logsvarnishncsa
: Display Varnish logs in NCSA combined log formatvarnishhist
: Varnish request histogramvarnishstat
: Varnish Cache statisticsvarnishtop
: Varnish log entry rankingvarnish-reload
: Reloads the Varnish Daemonvarnish-restart
: Restarts the Varnish Daemon