Upgrading to Varnish 6.0·续1

ACLs#

As before, ACLs can only specify ranges of IP addresses, and matches against ACLs can only be run against IP-valued elements.

This means that if a *.ip variable whose value is 0.0.0.0 due to the use of UDS is matched against an ACL, the match can only succeed if the ACL includes 0.0.0.0. If you currently have a security requirement that depends on IP addresses not matching an ACL unless they belong to a specified range, then that will continue to work with a UDS listener (since you almost certainly have not included 0.0.0.0 in that range).

Recall again that client.ip and server.ip are set by the PROXY protocol. So if you have a UDS listener configured to use PROXY and are using an ACL to match against one of those two variables, the matches will continue working against the “real” IPs sent via PROXY.

You can of course define an ACL to match in the UDS case, by including 0.0.0.0:

# matches local.ip and remote.ip when the listener is UDS
acl uds {
      "0.0.0.0";
}

But such an ACL cannot distinguish different UDS listeners, if you have more than one. For that, you can achieve a similar effect by inspecting local.socket and/or local.endpoint, as discussed above.

client.identity and the hash and shard directors#

As before, client.identity defaults to client.ip; that is, if its value has not been explicitly set in VCL, then it returns the same value as client.ip when it is read.

A common use of client.identity is to configure the hash and shard directors (see VMOD directors - Varnish Directors Module). This is a way to achieve “client-sticky” distribution of requests to backends – requests from the same clients are always sent to the same backends.

Such a configuration will almost certainly not do what you want if:

  • The listener is set to a UDS address.
  • PROXY is not used to set client.ip.
  • client.identity is not set to a distinct value before it is used to configure the director.

Since client.identity defaults to client.ip, which is always 0.0.0.0 under these conditions, the result will be that the director sends all requests to just one backend, and no requests to any other backend.

To avoid that result, change one of the conditions listed above – use PROXY to set distinct values for client.ip, or set client.identity to distinct values before it is used.

server.ip and default hashing for the cache#

The default algorithm for computing a hash value for the cache (the implementation of vcl_hash in builtin.vcl) mixes req.url and the Host header (req.http.Host) into the hash data. If there is no Host header, then server.ip is used instead. Considering the Host header or server.ip is a way of achieving a kind of “virtual hosting” – if your site receives requests with different Host headers or at distinct server addresses, then requests for the same URL will not hit the same cached response, if the requests are different in those other respects.

If you have UDS listeners and are not using PROXY to set distinct values of server.ip, then requests without a Host header will have the same value of server.ip == 0.0.0.0 mixed into the hash. In that case, requests with the same URL will result in the same hash value, and hit the same cached responses.

That doesn’t matter, of course, if you don’t need the “virtual hosting” effect – you only have one listener, you never receive different host headers, or you never receive the same URL for what should lead to distinct responses.

But if you need to avoid that result, then you can make one or more of these changes:

  • Use the PROXY protocol to set distinct server.ip values.
  • Write your own implementation of vcl_hash, for example to mix local.socket or local.endpoint into the hash.
  • Set req.http.Host to a distinct value if it is absent before vcl_hash is entered.

X-Forwarded-For#

Varnish automatically appends the value of client.ip to the X-Forwarded-For request header that is passed on to backends, or it creates the header with that value if it is not already present in the client request.

If the client request is received over a UDS listener and the PROXY protocol is not used, then 0.0.0.0 will be added to X-Forwarded-For. If you prefer, you can change that in VCL:

sub vcl_backend_fetch {
      # Assuming that server.identity has been set to an IP
      # address with the -i command-line argument.
      set bereq.http.X-Forwarded-For
          = regsub(bereq.http-X-Forwarded-For, "0.0.0.0$", server.identity);
      # ...
}

Again, this is probably not a concern if client.ip is set via the PROXY protocol.

UDS backends and the Host header#

By default, Varnish forwards the Host header from a client request to the backend. If there is no Host header in the client request, and the .host_header field was set in the backend declaration, then that value is used for the backend Host header. For backends declared with the .host field (with a domain name or IP address), then if there is neither a client Host header nor a .host_header declaration, the value of .host is set as the Host header of the backend request.

If the backend was declared with .path for a socket path, then the backend Host header is set to 0.0.0.0 under those conditions.

To re-state that:

  • If the backend was declared with .path to connect to a Unix domain socket, …
  • and .host_header was not set in the backend declaration, …
  • and there is no Host header in the client request, …
  • then the Host header in the backend request is set to 0.0.0.0.

If you want to avoid that, set a .host_header value for the backend, or set a value for the Host header in VCL.

VMOD std#

INT port(IP ip) always returns 0 when applied to a *.ip variable whose value is set to 0.0.0.0 because the listener is UDS. VOID set_ip_tos(INT tos) is silently ignored when the listener is UDS.

The shard director#

The alg argument of the shard director’s .reconfigure() and .key() methods has been removed. The choice of hash algorithms was experimental, and we have settled on SHA256 as providing the best dispersal.

If you have been using other choices of alg for .reconfigure(), then after upgrading and removing alg, the sharding of requests to backends will change once and only once.

If you have been using other values of alg for .key() and need to preserve the previous behavior, see the change log for advice on how to do so.

With the resolve=LAZY argument of the .backend() method, the shard director will now defer the selection of a backend to when a backend connection is actually made, which is how all other bundled directors work as well.

In vcl_init, resolve=LAZY is default and enables layering the shard director below other directors – you can now use something like mydirector.add_backend(myshard.backend()) to set the shard director as a backend for another director.

Use of resolve=LAZY on the client side is limited to using the default or associated parameters.

The shard director now provides a shard_param object that serves as a store for a set of parameters for the director’s .backend() method. This makes it possible to re-use a set of parameter values without having to restate them in every .backend() call. The .backend() method has an argument param whose value, if it is used, must be returned from the shard_param.use() method.

Because of these changes, support for positional arguments of the shard director .backend() method had to be removed. In other words, all parameters to the shard director .backend() method now need to be named.

See VMOD directors - Varnish Directors Module for details.

Restarts#

Restarts now leave all of the properties of the client request unchanged (all of the req.* variables, including the headers), except for req.restarts and req.xid, which change by design.

If you need to reset the client request headers to their original state (before changes in VCL), call VOID rollback(HTTP h).

return(restart) can now be called from vcl_recv{}.

New VMODs#

VMOD unix#

VMOD unix - Utilities for Unix domain sockets provides functions to determine the credentials of the peer process (user and group of the process owner) that connected to Varnish over a listener at a Unix domain socket. You can use this, for example, to impose tighter restrictions on who can access certain resources:

import unix;

sub vcl_recv {
      # Return "403 Forbidden" if the connected peer is
      # not running as the user "trusteduser".
      if (unix.user() != "trusteduser") {
              return( synth(403) );
      }

This is not available on every platform. As always, check the documentation and test the code before you attempt something like this in production.

VMOD proxy#

VMOD proxy - Varnish Module to extract TLV attributes from PROXYv2 provides functions to extract TLV attributes that may be optionally sent over a PROXYv2 connection to a Varnish listener. Most of these are properties of the peer component’s TLS connection:

import proxy;

# Get the authority attribute -- corresponds to the SNI of a TLS
# connection.
set req.http.Authority = proxy.authority();

Not all implementations send TLV attributes, and those that do don’t necessarily support all of them; test your code to see what works in your configuration.

See the PROXY v2 specification for more information about TLV attributes.

Packaging changes#

Supported platforms#

Official Varnish packages went through major changes for this release, and target Debian 9, Ubuntu 16.04 LTS and (Red Hat) Enterprise Linux 7. Ubuntu 14.04 LTS will likely reach its end of life before Varnish 6 and the venerable Enterprise Linux 6 is getting too old and forced time-consuming workarounds so for these reasons we dropped community support for those platforms.

Services#

As a result we ended up with systemd-only platforms for the official packages. The old services are still available as we archived them in the pkg-varnish-cache source tree. This was the occasion to remove differences between Red Hat and Debian derivatives since there’s no more reasons to have them diverge: we initially inherited packaging support from downstream package maintainers, and they deserve many thanks for that.

The biggest change that resulted in unifying our systemd setup across all official packages is that the varnish.params file available on Red Hat derivatives is gone. We noticed that using an environment file to hide the fact that varnishd is configured via command line arguments misled some people into thinking that what was proposed in the file was your only set of configuration options. For example, you can specify multiple listen addresses using multiple -a options but you only get variables for one address and a catch-all variable DAEMON_OPTS for anything not fitting in the template. In addition using an environment file pollutes the process’s environment.