Varnish Configuration Language (VCL)·续4

VCL objects and variables#

VCL provides a set of variables that can be set, unset, retrieved or modified. These variables are part of the various VCL objects.

You can group the variables as follows:

  • Connection -related variables. Part of the local, server, remote and client objects
  • Request -related variables. Part of the req and req_top objects
  • Backend request -related variables. Part of the bereq object
  • Backend response -related variables. Part of the beresp object
  • Cache object -related variables. Part of the obj object
  • Response -related variables. Part of the resp object
  • Session -related variables. Part of the sess object
  • Storage -related variables. Part of the storage object

The full list of VCL objects and variables can be found in VCL variable documentation.

Connection variables#

There are four connection -related objects available in VCL, and their meaning depends on your topology:

  • client: the client that sends the HTTP request
  • server: the server that receives the HTTP request
  • remote: the remote end of the TCP connection in Varnish
  • local: the local end of the TCP connection in Varnish

Local variables#

The local object has two interesting variables:

  • local.endpoint
  • local.socket

Both of these variables are only available using the vcl 4.1 syntax.

local.endpoint contains the socket address for the -a socket.

If -a http=:80 was set in varnishd, the local.endpoint value would be :80.

Whereas local.endpoint takes the socket value , local.socket will take the socket name. If we take the example where -a http=:80 is set, the value for local.socket will be http.

Identities#

Both the client and the server object have an identity variable that identifies them.

client.identity identifies the client, and its default value is the same value as client.ip. This is a string value, so you can assign what you like.

Naturally, the server.identity variable will identify your server. If you specified -i as a runtime parameter in varnishd, this will be the value. If you run multiple Varnish instances on a single machine, this is quite convenient.

But by default, server.identity contains the hostname of your server, which is the same value as server.hostname.

Request variables#

The req object allows you to inspect and manipulate incoming HTTP requests.

The most common variable is req.url, which contains the request URL. But there’s also req.method to get the request method. And every request header is accessible through req.http.*.

Imagine the following HTTP request :

GET / HTTP/1.1
Host: localhost
User-Agent: curl
Accept: */*

Varnish will populate the following request variables :

  • req.method will be GET.
  • req.url will be /.
  • req.proto will be HTTP/1.1.
  • req.http.host will be localhost.
  • req.http.user-agent will be curl.
  • req.http.accept will be */*.
  • req.can_gzip will be false because the request didn’t contain an Accept-Encoding: gzip header.

Top-level requests and Edge Side Includes#

Edge Side Includes (ESI) are markup tags that are used to assemble content on the edge, using a source attribute that refers to an HTTP endpoint.

Here’s an ESI example:

<esi:include src="/header" />

ESI tags are parsed by Varnish and the HTTP endpoint in the source attribute is called by an internal subrequest.

At the request level, there is a req.esi_level variable to check the level of depth and a req.esi variable to toggle ESI support.

When you’re in an ESI subrequest, there is also some context available about the top-level request that initiated the subrequest. The req_top object provides that context.

  • req_top.url returns the URL of the parent request.
  • req_top.http.* contains the request headers of the parent request.
  • req_top.method returns the request method of the parent request.
  • req_top.proto returns the protocol of the parent request.

If req_top is used in a non-ESI context, their values will be identical to the req object.

If you want to know whether or not the top-level request was requesting the homepage, you could use the following VCL code:

vcl 4.1;
import std;

sub vcl_recv {
    if(req.esi_level > 0 && req_top.url == "/") {
        std.log("ESI call for the homepage");
    }
}

This example will log ESI call for the homepage to Varnish’s Shared Memory Log using the std.log() function.

Backend request variables#

Backend requests are the requests that Varnish sends to the origin server when an object cannot be served from cache. The bereq object contains the necessary backend request information and is built from the req object.

In terms of scope, the req object is accessible in client-side subroutines , whereas the bereq object is only accessible in backend subroutines.

Although both objects are quite similar, they are not identical. The backend requests do not contain the per-hop fields such as the Connection header and the Range header.

Here are some bereq variables:

  • bereq.url is the backend request URL.
  • bereq.method is the backend request method.
  • bereq.http.* contains the backend request headers.
  • bereq.proto is the backend request protocol that was used.

On the one hand, bereq provides a copy of the client request information in a backend context. But on the other hand, because the backend request was initiated by Varnish , we have a lot more information in bereq:

bereq.connect_timeout, bereq.first_byte_timeout, and bereq.between_bytes_timeout contain the timeouts that are applied to the backend.

bereq.is_bgfetch is a boolean that indicates whether or not the backend request is made in the background. When this variable is true, this means the client hit an object in grace , and a new copy is fetched in the background.

bereq.backend contains the backend that Varnish will attempt to fetch from. When it is used in a string context, we just get the backend name.

Backend response variables#

Where there’s a backend request , there is also a backend response.

The beresp object contains all the relevant information regarding the backend response.

  • beresp.proto contains the protocol that was used for the backend response.
  • beresp.status is the HTTP status code that was returned by the origin.
  • beresp.reason is the HTTP status message that was returned by the origin.
  • beresp.body contains the response body , which can be modified for synthetic responses.
  • beresp.http.* contains all response headers.

There are also a bunch of backend response variables that are related to the Varnish Fetch Processors (VFP). These are booleans that allow you to toggle certain features:

  • beresp.do_esi can be used to enable ESI parsing.
  • beresp.do_stream can be used to disable HTTP streaming.
  • beresp.do_gzip can be used to explicitly compress non-gzip content.
  • beresp.do_gunzip can be used to explicitly uncompress gzip content and store the plain text version in cache.
  • beresp.ttl contains the objects remaining time to live (TTL) in seconds.
  • beresp.age (read-only) contains the age of an object in seconds.
  • beresp.grace is used to set the grace period of an object.
  • beresp.keep is used to keep expired and out of grace objects around for conditional requests.

These variables return a duration type. beresp.age is read-only, but all the others can be set in the vcl_backend_response or vcl_backend_error subroutines.

Other backend response variables#

  • beresp.was_304 indicates whether or not our conditional fetch got an HTTP 304 response before being turned into an HTTP 200.
  • beresp.uncacheable is inherited from bereq.uncacheable and is used to flag objects as uncacheable. This results in a hit-for-miss, or a hit-for-pass object being created.
  • beresp.backend returns the backend that was used.

beresp.backend returns a backend object. You can then use beresp.backend.name and beresp.backend.ip to get the name and IP address of the backend that was used.

Object variables#

The term object refers to what is stored in cache. It’s read-only and is exposed in VCL via the obj object.

Here are some obj variables:

  • obj.proto contains the HTTP protocol version that was used.
  • obj.status stores the HTTP status code that was used in the response.
  • obj.reason contains the HTTP reason phrase from the response.
  • obj.hits is a hit counter. If the counter is 0 by the time vcl_deliver is reached, we’re dealing with a cache miss.
  • obj.http.* contains all HTTP headers that originated from the HTTP response.
  • obj.ttl is the object’s remaining time to live in seconds.
  • obj.age is the objects age in seconds.
  • obj.grace is the grace period of the object in seconds.
  • obj.keep is the amount of time an expired and out of grace object will be kept in cache for conditional requests.
  • obj.uncacheable determines whether or not the cached object is uncacheable.

Even non-cacheable objects are kept in cache and are marked uncacheable. By default these objects perform hit-for-miss logic, but can also be configured to perform hit-for-pass logic. Uncacheable objects are a lot smaller in size and don’t contain all the typical response information.

Response variables#

The resp object contains the necessary information about the response that is going to be returned to the client.

In case of a cache hit the resp object is populated from the obj object. For a cache miss or if the cache was bypassed, the resp object is populated by the beresp object.

When a synthetic response is created, the resp object is populated from synth.

Here are some response variables as an example:

  • resp.proto contains the protocol that was used to generate the HTTP response.
  • resp.status is the HTTP status code for the response.
  • resp.reason is the HTTP reason phrase for the response.
  • resp.http.* contains the HTTP response headers for the response.
  • resp.is_streaming indicates whether or not the response is being streamed while being fetched from the backend.
  • resp.body can be used to produce a synthetic response body in vcl_synth.

Storage variables#

In varnishd you can specify one or more storage backends , or stevedores as we call them. The -s runtime parameter is used to indicate where objects will be stored.

Here’s a typical example:

varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256M

This Varnish instance will store its objects in memory and will allocate a maximum amount of 256 MB.

In VCL you can use the storage object to retrieve the free space and the used space of stevedore.

In this case you’d use storage.s0.free_space to get the free space, and storage.s0.used_space to get the used space.

When a stevedore is unnamed, Varnish uses the s%d naming scheme. In our case the stevedore is named s0.

Let’s throw in an example of a named stevedore :

varnishd -a :80 -f /etc/varnish/default.vcl -s memory=malloc,256M

To get the free space and used space , you’ll have to use storage.memory.free_space and storage.memory.used_space.

The built-in VCL#

The built-in VCL is a collection of VCL code that will be executed by default, even if the code is not specified in your VCL file. The built-in VCL is defined in the various VCL subroutines and is called by the Varnish Finite State Machine.

It is possible to bypass the built-in VCL by issuing a return statement in the corresponding subroutine.

To learn more about Varnish’s built-in VCL, take a look at the corresponding tutorials.

The built-in VCL →