VCL·续
Backends and health probes#
Please see VCL-backends and VCL-probe
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 an ACL entry specifies a host name which Varnish is unable to resolve, it will match any address it is compared to. Consequently, if it is preceded by a negation mark, it will reject any address it is compared to, which may not be what you intended. If the entry is enclosed in parentheses, however, it will simply be ignored if the host name cannot be resolved.
To match an IP address against an ACL, simply use the match operator:
if (client.ip ~ localnetwork) {
return (pipe);
}
ACLs have feature flags which can be set or cleared for each ACL individually:
-
+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; }
However, if the name resolves to both IPv4 and IPv6 you will still get an error.
- -fold - Do not fold ACL supernets and adjacent networks.
With this parameter set to on (default), ACLs are optimized in that subnets contained in other entries are skipped (e.g. if 1.2.3.0/24 is part of the ACL, an entry for 1.2.3.128/25 will not be added) and adjacent entries get folded (e.g. if both 1.2.3.0/25 and 1.2.3.128/25 are added, they will be folded to 1.2.3.0/24).
Skip and fold operations on ACL entries are output as warnings during VCL compilation as entries from the VCL are processed in order unless the -report sub-flag is also given (see below).
Logging under the VCL_acl tag can change with this parameter enabled: Matches on skipped subnet entries are now logged as matches on the respective supernet entry. Matches on folded entries are logged with a shorter netmask which might not be contained in the original ACL as defined in VCL. Such log entries are marked by fixed: folded.
Negated ACL entries are never folded.
Exactly one sub-flag is supported following fold in parenthesis:
- +fold(+report) - Fold with reporting (default)
Report about folding as described above
- +fold(-report) - Fold without reporting
Enable folding, but do not output folding-related warnings during VCL compilation
The report sub-option is only valid with +fold.
VCL objects#
A VCL object can be instantiated with the new keyword:
sub vcl_init {
new b = directors.round_robin();
b.add_backend(node1);
}
The object name (b in the example) needs to be a valid Identifier.
This is only available in vcl_init.
Subroutines#
A subroutine is used to group code for legibility or reusability:
sub pipe_if_local {
if (client.ip ~ localnetwork) {
return (pipe);
}
}
Subroutines in VCL do not take arguments, nor do they return values. The built in subroutines all have names beginning with vcl_, which is reserved.
To call a subroutine, use the call keyword followed by the subroutine’s name:
sub vcl_recv {
call pipe_if_local;
}
Symbol references#
Some VCL symbols are subject to reference counting, and would trigger a vcl compiler error if they were declared but not used. One way to turn these errors into warnings is to add -p vcc_feature=-err_unref to your varnishd command line. This will disable the error globally for all symbols in every VCL that is compiled.
If you only want to disable the error for some specific symbols, the unused keyword can be used to mark such symbols as intentionally unused:
backend b1 { .host = "localhost"; }
probe p1 { }
acl acl1 { "localhost"; }
sub sb {}
unused b1;
unused p1;
unused acl1;
unused sb;
Return statements#
The ongoing vcl_* subroutine execution ends when a return( __) statement is made.
The __specifies how execution should proceed. The context defines which actions are available.
It is possible to exit a subroutine that is not part of the built-in ones using a simple return statement without specifying an action. It exits the subroutine without transitioning to a different state:
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.
The built-in VCL distributed with Varnish will be implicitly concatenated when the VCL is compiled.
Functions#
The following built-in functions are available:
ban(STRING)#
Deprecated. See BOOL ban(STRING).
The
ban()function is identical to BOOL ban(STRING), 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 invcl_hash.
synthetic(STRING)#
Prepare a synthetic response body containing the STRING. Available in
vcl_synthandvcl_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.
For converting or casting VCL values between data types use the functions available in the std VMOD.
Versioning#
Multiple versions of the VCL syntax can coexist within certain constraints.
The VCL syntax version at the start of VCL file specified with -f sets the hard limit that cannot be exceeded anywhere, and it selects the appropriate version of the builtin VCL.
That means that you can never include vcl 9.1; from vcl 8.7;, but the opposite may be possible, to the extent the compiler supports it.
Files pulled in via include do not need to have a vcl X.Y; but it may be a good idea to do it anyway, to not have surprises in the future. The syntax version set in an included file only applies to that file and any files it includes - unless these set their own VCL syntax version.
The version of Varnish this file belongs to supports syntax 4.0 and 4.1.
EXAMPLES#
For examples, please see the online documentation.
SEE ALSO#
- varnishd
- VCL-backends
- VCL-probe
- VCL-steps
- VCL-Variables
- VMOD directors - Varnish Directors Module
- VMOD std - Varnish Standard Module
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
