NAME
VCL - Varnish Configuration LanguageDESCRIPTION
The VCL language is a small domain-specific language designed to be used to describe request handling and document caching policies for Varnish Cache.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.
Conditionals
VCL has if and else statements. Nested logic can be implemented with the elseif statement (elsif/elif/else if are equivalent).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; }
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.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.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
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.# 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";
include +glob "example.org/*.vcl";
Import statement
The import statement is used to load Varnish Modules (VMODs.)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*/.sub vcl_recv { // Single line of out-commented VCL. # Another way of commenting out a single line. /* Multi-line block of commented-out VCL. */ }
Backends and health probes
Please see vcl-backend(7) and vcl-probe(7)Access Control List (ACL)
An Access Control List (ACL) declaration creates and initialises a named access control list which can later be used to match client addresses:acl localnetwork { "localhost"; # myself "192.0.2.0"/24; # and everyone on the local network ! "192.0.2.23"; # except for the dial-in router }
if (client.ip ~ localnetwork) { return (pipe); }
- •
- +log - Emit a Acl record in VSL to tell if a match was found or not.
- •
- +table - Implement the ACL with a table instead of compiled code. This runs a little bit slower, but compiles large ACLs much faster.
- •
- -pedantic - Allow masks to cover non-zero host-bits. This allows the following to work:
acl foo -pedantic +log { "firewall.example.com" / 24; }
VCL objects
A VCL object can be instantiated with the new keyword:sub vcl_init { new b = directors.round_robin() b.add_backend(node1); }
Subroutines
A subroutine is used to group code for legibility or reusability:sub pipe_if_local { if (client.ip ~ localnetwork) { return (pipe); } }
sub vcl_recv { call pipe_if_local; }
Return statements
The ongoing vcl_* subroutine execution ends when a return(<action> ) statement is made.sub filter_cookies { if (!req.http.cookie) { return; } # complex cookie filtering }
Multiple subroutines
If multiple subroutines with the name of one of the built-in ones are defined, they are concatenated in the order in which they appear in the source.Functions
The following built-in functions are available:ban(STRING)
Deprecated. See std.ban().
The ban() function is identical to std.ban(), but does not provide
error reporting.
hash_data(input)
Adds an input to the hash input. In the
built-in VCL hash_data() is called on the host and URL of the request.
Available in vcl_hash.
synthetic(STRING)
Prepare a synthetic response body containing
the STRING. Available in vcl_synth and vcl_backend_error.
Identical to set resp.body / set beresp.body.
regsub(str, regex, sub)
Returns a copy of str with the first
occurrence of the regular expression regex replaced with sub.
Within sub, \0 (which can also be spelled \&) is
replaced with the entire matched string, and \n is replaced with
the contents of subgroup n in the matched string.
regsuball(str, regex, sub)
As regsub(), but this replaces all
occurrences.
VERSIONING
Multiple versions of the VCL syntax can coexist within certain constraints.EXAMPLES
For examples, please see the online documentation.SEE ALSO
HISTORY
VCL was developed by Poul-Henning Kamp in cooperation with Verdens Gang AS, Redpill Linpro and Varnish Software. This manual page is written by Per Buer, Poul-Henning Kamp, Martin Blix Grydeland, Kristian Lyngstøl, Lasse Karstensen and others.COPYRIGHT
This document is licensed under the same license as Varnish itself. See LICENSE for details.- •
- Copyright (c) 2006 Verdens Gang AS
- •
- Copyright (c) 2006-2015 Varnish Software AS