logfileForVirtualhost='virtualhostA.myDomain.tld.log'; logfileForEverythingElse='varnishncsa.log'; cd '/var/log/varnish'; randomLineNumber=$(shuf -i 1-$(wc -l "$logfileForVirtualhost" | cut -d' ' -f1) -n 1); regEx=$(awk -v lineNumber="$randomLineNumber" 'BEGIN{OFS=".*"} NR==lineNumber {print $1,$4,$7,$9,$11}' "$logfileForVirtualhost" | tr -d '[]'); grep -E "$regEx" "$logfileForVirtualhost" "$logfileForEverythingElse" | column -s ' ' -twhich shows that every entry of virtualhostA.myDomain.tld.log can be found in varnishncsa.log.
Flag | Usage |
---|---|
-a | append to file specified with -w instead of overwriting it (the default) |
-D | Daemonize |
-F formatString | Write the output log in the Format specified by formatString |
-P pidFile | write the process's PID into pidFile |
-q myQuery | Run the specified VSL query myQuery to extract logs |
-w outputFile | write output to outputFile |
# Note: If systemd is installed, this file is obsolete and ignored. You will # need to copy /lib/systemd/system/varnishncsa.service to /etc/systemd/system/ # and edit that file.
-rw-r--r-- 1 varnishlog varnishlog 0 Sep 9 02:16 /var/log/varnish/varnishncsa.log empty file
● varnishncsa.service - Varnish Cache HTTP accelerator NCSA logging daemon Loaded: loaded (/etc/systemd/system/varnishncsa.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Wed 2018-01-10 22:53:40 CET; 7 months 28 days ago
● varnishncsa.service - Varnish Cache HTTP accelerator NCSA logging daemon Loaded: loaded (/etc/systemd/system/varnishncsa.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2018-09-09 18:56:47 CEST; 1s ago Process: 32169 ExecStart=/usr/bin/varnishncsa -a -w /var/log/varnish/varnishncsa.log -D -P /run/varnishncsa/varnishncsa.pid (code=exited, Main PID: 32170 (varnishncsa) CGroup: /system.slice/varnishncsa.service └─32170 /usr/bin/varnishncsa -a -w /var/log/varnish/varnishncsa.log -D -P /run/varnishncsa/varnishncsa.pid
This article is NOT about changing the default port Varnish is listening on. It is about a change in Varnish behavior after upgrading Debian on a server where Varnish was already running fine and listening on port 80.
For more information about the initial setup of Varnish, and about changing its default port, please read my Varnish setup article.
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name tcp 0 0 0.0.0.0:6081 0.0.0.0:* LISTEN 0 833790406 5785/varnishd tcp 0 0 127.0.0.1:6082 0.0.0.0:* LISTEN 0 833790425 5785/varnishdHas /etc/default/varnish been altered during the upgrade ? Let's check this with :
DAEMON_OPTS="-a :80 \Nope, no change. So what ?
The root cause is that Debian Jessie doesn't use init.d as the init system anymore, but systemd instead. And /etc/default/varnish is only read by the /etc/init.d/varnish script, not by the systemd init script (/lib/systemd/system/varnish.service).
[Unit] Description=Varnish Cache, a high-performance HTTP accelerator [Service] Type=forking LimitNOFILE=131072 LimitMEMLOCK=82000 ExecStartPre=/usr/sbin/varnishd -C -f /etc/varnish/default.vcl ExecStart=/usr/sbin/varnishd -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m ExecReload=/usr/share/varnish/reload-vcl [Install] WantedBy=multi-user.target
The good ol' /etc/init.d/varnish restart has no effect here (varnishd still listening on 6081). SysVinit vs systemd styles, I guess...
● varnish.service - Varnish Cache, a high-performance HTTP accelerator Loaded: loaded (/lib/systemd/system/varnish.service; linked) Active: active (running) since Tue 2016-01-05 21:17:24 CET; 4s ago Process: 7835 ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m (code=exited, status=0/SUCCESS) Process: 7824 ExecStartPre=/usr/sbin/varnishd -C -f /etc/varnish/default.vcl (code=exited, status=0/SUCCESS) Main PID: 7846 (varnishd) CGroup: /system.slice/varnish.service ├─7846 /usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m └─7848 /usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256mYou can see there not only the port it's listening to, but also the configuration file read at startup.
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 834168483 7846/varnishd tcp 0 0 127.0.0.1:6082 0.0.0.0:* LISTEN 0 834168504 7846/varnishd
DAEMON_OPTS="-a :6081 \ -T localhost:6082 \ -b localhost:8080 \ -u varnish \ -g varnish \ -S /etc/varnish/secret \ -s file,/var/lib/varnish/$INSTANCE/varnish_storage.bin,1G"
DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m"
deb https://repo.varnish-cache.org/debian/ wheezy varnish-4.0
' >> /etc/apt/sources.list.d/varnish-cache.listActive Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name tcp 0 0 0.0.0.0:6081 0.0.0.0:* LISTEN 0 148492344 8519/varnishd tcp 0 0 127.0.0.1:6082 0.0.0.0:* LISTEN 0 148492320 8495/varnishd tcp6 0 0 :::6081 :::* LISTEN 0 148492345 8519/varnishd tcp6 0 0 ::1:6082 :::* LISTEN 0 148492319 8495/varnishd
State Recv-Q Send-Q Local Address:Port Peer Address:Port (user/PID/?) LISTEN 0 0 *:6081 *:* users:(("varnishd",8519,7)) LISTEN 0 0 127.0.0.1:6082 *:* users:(("varnishd",8495,6)) ... LISTEN 0 0 :::6081 :::* users:(("varnishd",8519,8)) LISTEN 0 0 ::1:6082 :::* users:(("varnishd",8495,5))
What happens if we query those ports ?
HTTP request sent, awaiting response... HTTP/1.1 503 Backend fetch failed Date: Mon, 20 Oct 2014 13:53:36 GMT Server: Varnish Content-Type: text/html; charset=utf-8 Retry-After: 5 X-Varnish: 32770 Age: 0 Via: 1.1 varnish-v4 Content-Length: 282 Connection: keep-alive 2014-10-20 15:53:36 ERROR 503: Backend fetch failed.I did get a response from Varnish, but it sounds like "Halp, I'm not configured !"
Connecting to ipAddressOfMyServer:6082... failed: Connection refused.Knocking on the wrong door ? (This is for administration, actually. We'll see this later.)
This procedure applies to a live webserver running Lighttpd, which is why all restarts occur and the end of the configurations.
## Alternative 2, Configuration with VCL # # Listen on port 6081, administration on localhost:6082, and forward to # one content server selected by the vcl file, based on the request. # Use a 256MB memory based cache. # DAEMON_OPTS="-a :6081 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m"Update
-a :6081
to -a :80
.
# Default backend definition. Set this to point to your content server. backend default { .host = "127.0.0.1"; .port = "8080"; }
server.port = 80
into server.port = 8080
Message from VCC-compiler:
(details of error encountered and line)
#
Running VCC-compiler failed, exited with 2
VCL compilation failed
HTTP request sent, awaiting response... HTTP/1.1 200 OK Cache-Control: public, max-age=600 Expires: Mon, 20 Oct 2014 15:10:29 GMT X-Powered-By: PHP/5.5.17-1~dotdeb.1 Vary: Accept-Encoding Content-type: text/html Date: Mon, 20 Oct 2014 15:00:30 GMT Server: lighttpd/1.4.31 X-Varnish: 32872 Age: 0 Via: 1.1 varnish-v4 Transfer-Encoding: chunked Connection: keep-alive Accept-Ranges: bytesLooks good !
req
is still available. And there's also the backend response object beresp
, containing HTTP headers from the backend. (details)req
is available.(details)pass
can be called in both vcl_recv and vcl_fetch
.