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,remoteandclientobjects - Request -related variables. Part of the
reqandreq_topobjects - Backend request -related variables. Part of the
bereqobject - Backend response -related variables. Part of the
berespobject - Cache object -related variables. Part of the
objobject - Response -related variables. Part of the
respobject - Session -related variables. Part of the
sessobject - Storage -related variables. Part of the
storageobject
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 requestserver: the server that receives the HTTP requestremote: the remote end of the TCP connection in Varnishlocal: the local end of the TCP connection in Varnish
Local variables#
The local object has two interesting variables:
local.endpointlocal.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.methodwill beGET.req.urlwill be/.req.protowill beHTTP/1.1.req.http.hostwill belocalhost.req.http.user-agentwill becurl.req.http.acceptwill be*/*.req.can_gzipwill befalsebecause the request didn’t contain anAccept-Encoding: gzipheader.
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.urlreturns the URL of the parent request.req_top.http.*contains the request headers of the parent request.req_top.methodreturns the request method of the parent request.req_top.protoreturns 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.urlis the backend request URL.bereq.methodis the backend request method.bereq.http.*contains the backend request headers.bereq.protois 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.protocontains the protocol that was used for the backend response.beresp.statusis the HTTP status code that was returned by the origin.beresp.reasonis the HTTP status message that was returned by the origin.beresp.bodycontains the response body , which can be modified for synthetic responses.beresp.http.*contains all response headers.
VFP-related backend response variables#
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_esican be used to enable ESI parsing.beresp.do_streamcan be used to disable HTTP streaming.beresp.do_gzipcan be used to explicitly compress non-gzip content.beresp.do_gunzipcan be used to explicitly uncompress gzip content and store the plain text version in cache.
Timing-related backend response variables#
beresp.ttlcontains the objects remaining time to live (TTL) in seconds.beresp.age(read-only) contains the age of an object in seconds.beresp.graceis used to set the grace period of an object.beresp.keepis 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_304indicates whether or not our conditional fetch got an HTTP 304 response before being turned into an HTTP 200.beresp.uncacheableis inherited frombereq.uncacheableand is used to flag objects as uncacheable. This results in ahit-for-miss, or ahit-for-passobject being created.beresp.backendreturns 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.protocontains the HTTP protocol version that was used.obj.statusstores the HTTP status code that was used in the response.obj.reasoncontains the HTTP reason phrase from the response.obj.hitsis a hit counter. If the counter is0by the timevcl_deliveris reached, we’re dealing with a cache miss.obj.http.*contains all HTTP headers that originated from the HTTP response.obj.ttlis the object’s remaining time to live in seconds.obj.ageis the objects age in seconds.obj.graceis the grace period of the object in seconds.obj.keepis the amount of time an expired and out of grace object will be kept in cache for conditional requests.obj.uncacheabledetermines 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.protocontains the protocol that was used to generate the HTTP response.resp.statusis the HTTP status code for the response.resp.reasonis the HTTP reason phrase for the response.resp.http.*contains the HTTP response headers for the response.resp.is_streamingindicates whether or not the response is being streamed while being fetched from the backend.resp.bodycan be used to produce a synthetic response body invcl_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.
