How to install vmod-xcounter for Varnish 6.0 LTS

About vmod-xcounter

Varnish 6.0.x branch is officially the LTS version. Many useful Varnish 6 modules (VMODs) are available for installation via our RPM repository.

vmod-xcounter is the new VMOD by Shohei Tanaka. It allows to create and use custom counters.

Install vmod-xcounter in CentOS / RedHat 7 for Varnish 6.0 LTS

The first step is enabling RPM repository with Varnish VMODs. This is a no-brainer with:

yum -y install https://extras.getpagespeed.com/release-el7-latest.rpm yum-utils
yum-config-manager --enable getpagespeed-extras-varnish60

If you have not installed Varnish already, do so now:

yum install varnish

And to get the vmod-xcounter installed, run:

yum install vmod-xcounter

Use vmod-xcounter

Load the VMOD by placing the following at the top of your main .vcl file:

import xcounter;

Now you can create counters with:

sub vcl_init {
new example_com = xcounter.vsc();
}

You can increment / otherwise adjust and work with counters virtually anywhere in VCL, e.g. to count visits to domain:

sub vcl_recv {
if(req.http.host == "example.com"){
example_com.incr(1);
}
}

You can then run varnishstat -f XCNT.* in order to view the counter. More documentation is available on the project's homepage.


Originally published at GetPageSpeed.