Cain Manor

Your Guide To All Things Cain™

Setup logging for Varnish

So far, we’ve got Var­nish installed and have a cor­rect default.vcl. Now let’s make sure AWStats/JAW­Stats can read them.

In your default.vcl, make sure you have this stanza. Oth­er­wise, the Apache log files will only show traf­fic from your proxy server IP address. This allows the client.ip to be for­warded 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 cor­rectly. You need to add this line your httpd.conf to get the cor­rect log­ging for­mat. 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

Fur­ther down (or in your vir­tual server.conf file, if you break those out), we need to set our server to use that cus­tom log­ging. I have a domain (cainmanor.com) and a sub­do­main (photos.cainmanor.com), so I just pointed both vir­tual 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 cus­tom log (/var/log/httpd/cainmanor.com-access.log) and see your traf­fic com­ing from var­ied IP addresses.

Now we tell AWStats where to find the cus­tom 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 var­nish and apache, and you should be log­ging successfully.

Happy Com­put­ing!!

Comments are closed.