VCL

Varnish Configuration Language#

  • Manual section: 7

DESCRIPTION#

The VCL language is a small domain-specific language designed to be used to describe request handling and document caching policies for Varnish Cache.

When a new configuration is loaded, the varnishd management process translates the VCL code to C and compiles it to a shared object which is then loaded into the server process.

This document focuses on the syntax of the VCL language. For a full description of syntax and semantics, with ample examples, please see the online documentation at https://www.varnish-cache.org/docs/ .

Starting with Varnish 4.0, each VCL file must start by declaring its version with vcl .; marker at the top of the file. See more about this under Versioning below.

Identifiers#

VCL language elements are generally written in ASCII.

Identifiers start with an ASCII alphabetic character and continue with any combination of alphabetic or numerical character, - or _.

To illustrate, e1xam_p-le is a valid identifier, 1try and a%remainder are not.

Character Sets#

While identifiers can only consist of this subset of ASCII, strings can contain any bytes except NUL (zero, 0), which marks the end of the string. The Varnish Configuration Language itself is not concerned with the character encoding of strings. VCL code handling strings in different character sets needs to track encodings itself. VMODs exist to help with such tasks (e.g. iconv).

To illustrate, "🥬" is a valid string which, when interpreted as UTF-8, happens to represent code point U+1F96C (Leafy Green) but 🥬 is not a valid identifier.

Character Sets in HTTP#

Not all valid strings in VCL are valid in HTTP elements other than the body (e.g. URLs and headers). For example, HTTP header field values can not contain control characters, see also the validate_headers feature.

Operators#

The following operators are available in VCL:

= Assignment operator.

+, -, *, /, % Basic math on numerical values.

+=, -=, *=, /= Assign and increment/decrement/multiply/divide operator.

For strings, += appends.

(, ) Evaluate separately.

==, !=, <, >, <=, >= Comparisons

~, !~ Match / non-match. Can either be used with regular expressions or ACLs.

! Negation.

&& / || Logical and/or.

On Equality#

In this section, “compare equal” means that the == operator returns true and, conversely, the != operator returns false.

The VCL types INT, DURATION, BYTES, REAL and TIME compare equal if their values compare equal numerically.

Notice that DURATION, REAL and TIME are represented as floating point numbers, and hence testing for (in)equality might be unreliable.

IP compares equal for equal address.

STRING and STRANDS compare equal if the represented strings are equal.

BOOL compares equal if the truth value is the same.

The VCL types BACKEND, ACL, PROBE, STEVEDORE, SUB and INSTANCE compare equal if they represent the same object. Different objects with the same properties will never compare equal.

Conditionals#

VCL has if and else statements. Nested logic can be implemented with the elseif statement (elsif/elif/else if are equivalent).

Note that there are no loops or iterators of any kind in VCL.

Variables#

VCL does most of the work by examining, set’ing and unset’ing variables:

if (req.url == "/mistyped_url.html") {
    set req.url = "/correct_url.html";
    unset req.http.cookie;
}

There are obvious limitations to what can be done, for instance it makes no sense to unset req.url; - a request must have some kind of URL to be valid, and likewise trying to manipulate a backend response when there is none (yet) makes no sense. The VCL compiler will detect such errors.

Variables have types. Most of them a STRINGS, and anything in VCL can be turned into a STRING, but some variables have types like DURATION, IP etc.

When setting a such variables, the right hand side of the equal sign must have the correct variables type, you cannot assign a STRING to a variable of type NUMBER, even if the string is "42".

Explicit conversion functions are available in VMOD std - Varnish Standard Module.

For the complete album of VCL variables see: VCL-Variables.

Strings#

Basic strings are enclosed in double quotes " …", and may not contain newlines. Long strings are enclosed in {" …"} or """ …""". They may contain any character including single double quotes ", newline and other control characters except for the NUL (0x00) character.

Booleans#

Booleans can be either true or false. In addition, in a boolean context some data types will evaluate to true or false depending on their value.

String types will evaluate to false if they are unset. This allows checks of the type if (req.http.opthdr) {} to test if a header exists, even if it is empty, whereas if (req.http.opthdr == "") {} does not distinguish if the header does not exist or if it is empty.

Backend types will evaluate to false if they don’t have a backend assigned; integer types will evaluate to false if their value is zero; duration types will evaluate to false if their value is equal or less than zero.

Time#

VCL has time. A duration can be added to a time to make another time. In string context they return a formatted string in RFC1123 format, e.g. Sun, 06 Nov 1994 08:49:37 GMT.

The keyword now returns a notion of the current time, which is kept consistent during VCL subroutine invocations, so during the execution of a VCL state subroutine (vcl_* {}), including all user-defined subroutines being called, now always returns the same value.

Durations#

Durations are defined by a number followed by a unit. The number can include a fractional part, e.g. 1.5s. The supported units are:

ms milliseconds

s seconds

m minutes

h hours

d days

w weeks

y years

In string context they return a string with their value rounded to 3 decimal places and excluding the unit, e.g. 1.500.

Integers#

Certain fields are integers, used as expected. In string context they return a string, e.g. 1234.

Real numbers#

VCL understands real numbers. In string context they return a string with their value rounded to 3 decimal places, e.g. 3.142.

Regular Expressions#

Varnish uses Perl-compatible regular expressions (PCRE). For a complete description please see the pcre(3) man page.

To send flags to the PCRE engine, such as to do case-insensitive matching, add the flag within parens following a question mark, like this:

# If host is NOT example dot com..
if (req.http.host !~ "(?i)example\.com$") {
    ...
}

Include statement#

To include a VCL file in another file use the include keyword:

include "foo.vcl";

The included file can be specified as follows:

  • If the path starts with ‘/’, it is an absolute path.
  • If the path starts with ‘./’, it is relative to the including VCL file.
  • Otherwise, it is a relative path searched in one of the vcl_path directories.

Optionally, the include keyword can take a +glob flag to include all files matching a glob pattern:

include +glob "/etc/varnish/example.org/*.vcl";

Note that the +glob option can only be used with absolute paths and relative paths starting with ‘./’, which means that +glob includes cannot be searched in vcl_path directories.

Import statement#

The import statement is used to load Varnish Modules (VMODs.)

Example:

import std;
sub vcl_recv {
    std.log("foo");
}

Comments#

Single lines of VCL can be commented out using // or #. Multi-line blocks can be commented out with /*block*/.

Example:

sub vcl_recv {
    // Single line of out-commented VCL.
    # Another way of commenting out a single line.
    /*
        Multi-line block of commented-out VCL.
    */
}