Install and configure Piwik behind Varnish reverse proxy

Written by
Date: 2013-01-13 14:14:00 00:00

Piwik is a good replacement for Google Analytics, and good options for you if you do not want to use cookies for your analytics solution. With it you can run analytics without cookies

If you install it behind a reverse proxy you can face some issues, it is supposed to automatically detect the reverse proxy, and configured itself to work with it. Here some guidelines if that simply does not happen.

Installing and configuring Piwik behind Varnish

Varnish is one of the most efficient reverse proxy servers available today, it makes your slow sites just fly, but it also creates some issues when you are running a complete dynamic site behind it. Piwik is 100% dynamic, so it is better to not cache it. Here what you need to do:


You need to edit the varnish vcl, which may be named default.vcl

Add these lines in vcl_recv

if ( == "") {
	set req.http.X-Forwarded-For = client.ip;

This will tell Varnish two things, first to send Piwik the IP of the customer in the X-Forward-For http header and to directly look at the back end server (Apache in my case, but Nginx will work just the same)

The first order will avoid Piwik from showing like all visits come from the Varnish' IP, usually, and show the real visitor's IP. The second will avoid Varnish to look for the reply in the Cache.

In the vcl_fetch add this:

if ( == "") {

This will tell Varnish not to cache any reply that comes from the back end when the requested url is in the I am assuming you are installing piwik in that domain, use the domain where you have installed Piwik instead of that.


You also need to edit the Piwik configuration file: config/config.ini.php which is inside your Piwik installation folder and add those lines, if the [General] section already exists, just add the last to lines to that section.

proxy_client_headers[] = "HTTP_X_FORWARDED_FOR"
proxy_host_headers[] = "HTTP_X_FORWARDED_FOR

You should be OK with that, and Piwik should be working behind a proxy, Varnish in this case.