15

I want to get Varnish to log requests. I found the command varnishlog -a -w /var/log/varnish.log, but it does not log anything.

Then I found that Varnish does not write to log by default. However I am unable to find configuration options for logging.

Muneeb Nasir
  • 2,414
  • 4
  • 31
  • 54
  • 1
    Note also when using varnishlog -w with version < 3.0.3, you have to restart it after every varnish restart. – Doomsday Oct 03 '12 at 13:11

5 Answers5

24

If you want to log HTTP requests in NCSA Common Log Format you need to use varnishncsa. On CentOS/RedHat, the Varnish RPM package includes a varnishncsa init script that you can use to start logging. By default it logs to logfile="/var/log/varnish/varnishncsa.log".

Additionally if you wish to serve several different hosts through a single Varnish install, you'll want to include the host name in the log as well. This can be accomplished with the following setting in /etc/sysconfig/varnishncsa

DAEMON_OPTS="$DAEMON_OPTS -F '%{Host}i %h %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-agent}i\"'"

Please note that the method described in the link by Anshu only logs the requests that are passed through by Varnish to the backend servers. Cache hit requests will never be recorded (before Varnish 5.0 - see below). Therefore the HTTPD logs gathered this way cannot be used for statistical analysis.

Update: As @VikrantPogula mentioned, as of Varnish 5.0 all client requests are logged - including cache hits. This is the default behavior, and can be switched on explicitly using the -c switch.

Ketola
  • 2,767
  • 18
  • 21
  • 20
    To actually enable the logging on Ubuntu: edit `/etc/default/varnishncsa` uncomment `# VARNISHNCSA_ENABLED=1` run `sudo service varnishncsa restart` – George May 02 '13 at 14:15
  • I was not able to use the -F option in `/etc/default/varnishncsa` (Ubuntu 14.04), probably because of the shell handling quotes in strange ways. When I tried to restart the service via `sudo service varnishncsa restart`, the usage options for varnish were displayed but the service wasn't started. Looks like an unresolved bug to me, see: http://giantdorks.org/alain/workaround-for-broken-varnishncsa-logging-due-to-shell-mishandling-of-spaces-in-log_format-variables/ and https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=657449 – Hirnhamster Jan 10 '16 at 15:55
6

Metrics in Varnish are a common area that people struggle with.

It's relatively easy to get some one off request metrics but overall tracking over time generally involves using varnishncsa. Here is a guide on Varnish installation that includes metric setup

mattnthat
  • 309
  • 4
  • 3
3

In response to Ketola's answer, you can have varnishncsa log even Cache hit requests, by passing the -c flag. This will allow you to use HTTPD logs to analyse stats.

Example : varnishncsa -c -a -w ~/varnish.log will write cache hit requests to the file in default NCSA format

2

The files /etc/default/varnish* are deprecated in systems with systemd. just run these commands:

systemctl enable varnishncsa.service
systemctl start varnishncsa.service

you can also modify the daemon parameters in /lib/systemd/system/varnishncsa.service

Mehdi Hamidi
  • 99
  • 1
  • 8
1

Since i landed here and the suggested fixed did not help:

My varnishncsa.log was empty and the process was running.

It turned out, that i HAD TO specify the log format (any format).

As this insight in itself did not fix my init script, i fixed it using this strategy.

scones
  • 3,317
  • 23
  • 34