Upgrading to Varnish 6.0
Unix domain sockets as listen addresses#
The varnishd -a command-line argument now has this form, where the address may be a Unix domain socket, identified as such when it begins with / (see varnishd OPTIONS):
-a [name=][address][:port][,PROTO][,user=<user>][,group=<group>][,mode=<mode>]
For example:
varnishd -a /path/to/listen.sock,PROXY,user=vcache,group=varnish,mode=660
That means that an absolute path must always be specified for the socket file. The socket file is created when Varnish starts, and any file that may exist at that path is unlinked first. You can use the optional user, group and mode sub-arguments to set permissions of the new socket file; use names for user and group (not numeric IDs), and a 3-digit octal number for mode. This is done by the management process, so creating the socket file and setting permissions are done with the privileges of the management process owner.
There are some platform-specific restrictions on the use of UDSen to which you will have to conform. Here are some things we know of, but this list is by no means authoritative or exhaustive; always consult your platform documentation (usually in man unix):
- There is a maximum permitted length of the path for a socket file, considerably shorter than the maximum for the file system; usually a bit over 100 bytes.
- On FreeBSD and other BSD-derived systems, the permissions of the socket file do not restrict which processes can connect to the socket.
- On Linux, a process connecting to the socket must have write permissions on the socket file.
On any system, a process connecting to the socket must be able to access the socket file. So you can reliably restrict access by restricting permissions on the directory containing the socket (but that must be done outside of the Varnish configuration).
When UDS listeners are in use, VCL >= 4.1 will be required for all VCL programs loaded by Varnish. If you attempt to load a VCL source with vcl 4.0;, the load will fail with a message that the version is not supported.
If you continue using only IP addresses in your -a arguments, you won’t have to change them, and you can continue using VCL 4.0.
Unix domain sockets as backend addresses#
A backend declaration may now have the .path field to specify a Unix domain socket to which Varnish connects:
backend my_uds_backend {
.path = "/path/to/backend.sock";
}
One of the fields .host or .path must be specified for a backend (but not both).
The value of .path must be an absolute path (beginning with /), and the file at that path must exist and be accessible to Varnish at VCL load time; and it must be a socket.
The platform-specific restrictions on UDSen mentioned above apply of course to backends as well; but in this case your deployment of the peer component listening at the socket file must fulfill those conditions, otherwise Varnish may not be able to connect to the backend.
The path of a socket file may also be specified in the varnishd -b command-line option (see varnishd OPTIONS):
$ varnishd -b /path/to/backend.sock
The value of -b must fulfill the same conditions as the .path field in a backend declaration.
Backends with the .path specification require VCL 4.1, as do paths with the -b argument. If you don’t use UDS backends, you can continue using VCL 4.0.
varnishd parameters#
The cli_buffer parameter, which was deprecated as of Varnish 5.2, is now retired.
max_restarts now works more correctly – it is the number of return(restart) calls permitted per request. (It had been one less than the number of permitted restarts.)
The parameters tcp_keepalive_intvl, tcp_keepalive_probes and tcp_keepalive_time are silently ignored for listen addresses that are Unix domain sockets. The parameters accept_filter and tcp_fastopen (which your platform may or may not support in the first place) almost certainly have no effect on a UDS. It is not an error to use any of these parameters with a UDS; you may get error messages in the log for accept_filter or tcp_fastopen (with the VSL tag Error in raw grouping), but they are harmless.
workspace_thread is now used for IO buffers during the delivery of the client response. This space had previously been taken from workspace_client. If you need to reduce memory footprint, consider reducing workspace_client by the amount in workspace_thread.
Added ref_param_esi_iovs. tl;dr: Don’t touch it, unless advised to do so by someone familiar with the innards of Varnish.
Changes to VCL#
VCL 4.0 and 4.1#
The first line of code in a VCL program may now be either vcl 4.0; or vcl 4.1;, establishing the version of the language for that instance of VCL. Varnish 6.0 supports both versions.
The VCL version mainly affects which variables may be used in your VCL program, or in some cases, whether the variable is writable or read-only. Only VCL 4.1 is permitted when Unix domain sockets are in use.
For details, see local, server, remote and client, and the notes in the present document.
VCL variables#
local.socket and local.endpoint#
These read-only variables are available as of VCL 4.1, and provide information about the listener address over which the current client request was received.
local.socket is the name provided in the -a command-line argument for the current listener, which defaults to a0, a1 and so on (see varnishd OPTIONS).
local.endpoint is the value of the address[:port] or path field provided as the -a value for the current listener, exactly as given on the command line. For example:
# When varnishd is invoked with these -a arguments ...
$ varnishd -a foo=12.34.56.78:4711 -a bar=/path/to/listen.sock
# ... then in VCL, for requests received over the first listener:
local.socket == "foo"
local.endpoint == "12.34.56.78:4711"
# ... and for requests received over the second listener:
local.socket == "bar"
local.endpoint == "/path/to/listen.sock"
# With this invocation ...
$ varnishd -a :80 -a 87.65.43.21
# ... then for requests received over the first listener:
local.socket == "a0"
local.endpoint == ":80"
# ... and for the second listener
local.socket == "a1"
local.endpoint == "87.65.43.21"
So if you have more than one listener and need to tell them apart in VCL, for example a listener for “regular” client traffic and another one for “admin” requests that you must restrict to internal systems, these two variables can help you do so.
local.socket and local.endpoint are available on both the client and backend sides. But the values on the backend side are not necessarily the same as they were on the side of the client request that initiated the backend request. This is because of the separation of client and backend threads – a backend thread may be re-used that was initiated by a client request over another listener, and local.socket and local.endpoint on that thread retain the values for the original listener.
So if, in your backend VCL code, you need to be sure about the listener that was used on the client side of the same transaction, assign local.socket and/or local.endpoint to a client request header, and retrieve the value from a backend request header:
sub vcl_miss {
set req.http.X-Listener = local.socket;
}
sub vcl_backend_fetch {
if (bereq.http.X-Listener == "a0") {
# ...
}
}
sess.xid#
This is the unique ID assigned by Varnish to the current session, which stands for the “conversation” with a single client connection that comprises one or more request/response transactions. It is the same XID shown in the log for session transactions (with -g session grouping). sess.xid is read-only and is available as of VCL 4.1.
Variable changes in VCL 4.0 and 4.1#
The *.proto variables (req.proto, resp.proto, bereq.proto and beresp.proto) are read-only as of VCL 4.1, but are still writable in VCL 4.0.
req.esi is available in VCL 4.0, but no longer in 4.1. In its place, resp.do_esi has been introduced in VCL 4.1. Set resp.do_esi to false in vcl_deliver if you want to selectively disable ESI processing for a client response (even though beresp.do_esi was true during fetch).
beresp.backend.ip and beresp.storage_hint are discontinued as of VCL 4.1, but are still available in 4.0. Note that beresp.storage_hint has been deprecated since Varnish 5.1; you should use beresp.storage instead.
Client-side variable access#
req.storage, req.hash_ignore_busy and req.hash_always_miss are now accessible from all of the client side subroutines (previously only in vcl_recv{}).
Unix domain sockets and VCL#
We have made an effort to adapt the support of Unix domain sockets in VCL so that you may not have to change anything in your VCL deployment at all, other than changing the version to 4.1.
The short story is that where VCL requires an IP value, the value is 0.0.0.0:0 for a connection that was addressed as a UDS – the “any IPv4” address with port 0. So your use of IP-valued elements in VCL will continue to work and may not have to change, but there are some consequences that you should consider, covered in the following.
As we shall see, for a variety of reasons you get the best results if the component forwarding to Varnish via UDS uses the PROXY protocol, which sets client.ip and server.ip to the addresses sent in the PROXY header.
If you don’t use UDSen, then nothing about VCL changes with respect to network addressing. UDS support requires version 4.1, so if you are keeping your VCL level at 4.0 (and hence are staying with IP addresses), then none of the following is of concern.
client.ip, server.ip, local.ip and remote.ip#
These variables have the value 0.0.0.0 for a connection that was addressed as a UDS. If you are using the PROXY protocol, then client.ip and server.ip have the “real” IP address values sent via PROXY, but local.ip and remote.ip are always 0.0.0.0 for a UDS listener.
If you have more than one UDS listener (more than one -a command-line argument specifying a socket path), then you may not be able to use the *.ip variables to tell them apart, especially since local.ip will be 0.0.0.0 for all of them. If you need to distinguish such addresses in VCL, you can use local.socket, which is the name given for the -a argument (a0, a1 etc. by default), or local.endpoint, which in the case of UDS is the path given in the -a argument. You can, for example, use string operations such as regex matching on local.endpoint to determine properties of the path address:
# admin requests allowed only on the listener whose path ends in
# "admin.sock"
if (req.url ~ "^/admin") {
if (local.endpoint !~ "admin.sock$") {
# wrong listener, respond with "403 Forbidden"
return( synth(403) );
}
else {
# process the admin request ...
}
}
# superadmin requests only allowed on the "superadmin.sock" listener
if (req.url ~ "^/superadmin") {
if (local.endpoint !~ "superadmin.sock$") {
return( synth(403) );
}
else {
# superadmin request ...
}
}
