Logging

Running Varnish in front of your web server will mess up your normal Apache log files as the majority of traffic will be served from the cache. Varnish normally writes its log information to memory, but includes the varnishncsa program which displays the data in the Apache/NCSA combined format and can write that data to a file.

varnishncsa Startup
With Varnish installed from source, daemontools is an easy way to run the logging daemon. If you haven't already, install daemontools.

Create a directory for the varnishncsa service:

mkdir -m 1755 /var/service/varnishncsa
cd /var/service/varnishncsa

Create the run script and make it executable:

echo '#!/bin/sh' > run
echo 'exec 2>&1' >> run
echo 'exec varnishncsa' >> run
chmod 755 run

Our log script comes from John Simpson's:

mkdir -m 755 log
cd log
wget http://qmail.jms1.net/scripts/service-any-log-run
mv service-any-log-run run
chmod 755 run

In order to show the proper IP address from clients, you'll need to adjust the Varnish vcl file to set custom header with the client's IP:

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;
}

Then adjust your webserver's log format to use this header:

  • Lighttpd (/service/lighttpd/root/lighttpd.conf:

    accesslog.format = "%{X-Forwarded-For}i %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i""
  • Apache (/usr/local/apache2/etc/httpd.conf):
    LogFormat "%{X-Forwarded-For}i %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" varnishcombined

    and in your virtual host config:

    CustomLog /path/to/access/log varnishcombined

Edit the varnishncsa line in /var/service/varnishncsa/run to write to your website access log:

exec varnishncsa -a -w /var/websites/www.example.com/logs/access.log

For VirtualHosts:

exec varnishncsa -m "RxHeader:^Host: example.com$" -a -w /var/websites/logs/example.com-access.log

Add the service to daemontools by creating the symbolic link in /service

ln -s /var/service/varnishncsa /service/varnishncsa

Confirm that the service is running:

svstat /service/varnishncsa /service/varnishncsa/log

Resources
log the user's IP, not the proxy's, in Lighttpd access log
Save the split log for every host
http://cainmanor.com/tech/setup-logging-for-varnish
http://constantshift.com/page/6/

Recent Updates

  • 1 year 12 months ago
  • 1 year 12 months ago
  • 1 year 12 months ago
    php 8.x
  • 1 year 12 months ago
    10.6.7
  • 2 years 22 hours ago
    Drop Centos 5/6 stuff