Setup logging for Varnish

So far, we’ve got Varnish installed and have a correct default.vcl. Now let’s make sure AWStats/JAWStats can read them.

In your default.vcl, make sure you have this stanza. Otherwise, the Apache log files will only show traffic from your proxy server IP address. This allows the client.ip to be forwarded to your log file.

sub vcl_recv {
  # Add a unique header containing the client address
  remove req.http.X-Forwarded-For;
  set    req.http.X-Forwarded-For = client.ip;
}


Now we need to setup Apache to log correctly. You need to add this line your httpd.conf to get the correct logging format. Notice the http.X-Forwarded-For we just set.

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" varnishcombined

Further down (or in your virtual server.conf file, if you break those out), we need to set our server to use that custom logging. I have a domain (cainmanor.com) and a subdomain (photos.cainmanor.com), so I just pointed both virtual host to the same log file.

<VirtualHost *:8080>
        DocumentRoot /wordpress
        ServerName cainmanor.com
        CustomLog /var/log/httpd/cainmanor.com-access.log varnishcombined
</VirtualHost>

Once you’ve done this, you should be able to look in your custom log (/var/log/httpd/cainmanor.com-access.log) and see your traffic coming from varied IP addresses.

Now we tell AWStats where to find the custom log ( /etc/awstat/awstats.cainmanor.com.conf ) we created.

#LogFile="/var/log/httpd/access_log" <- OLD
LogFile="/var/log/httpd/cainmanor.com-access.log"

Now all you need to do is restart varnish and apache, and you should be logging successfully.

Happy Computing!!