LOG IN SIGN UP
Documentation

Local variables

  Last updated October 29, 2018

Fastly VCL supports variables for storing temporary values during request processing.

Declaring a variable

Variables must be declared before they are used, usually at the beginning of a function before any statements. They can only be used in the same function where they are declared. Fastly VCL does not provide block scope. Declarations apply to an entire function's scope even if a variable is declared within a block.

Variables start with var. and their names consist of characters in the set [A-Za-z0-9._-]. (: is explicitly disallowed.) The declaration syntax is:

declare local var.<name> <type>;

Variable types

Variables can be of the following types:

Declared variables are initialized to the zero value of the type:

Usage

Boolean variables

Boolean assignments support boolean variables on the right-hand side as well as BOOL-returning functions, conditional expressions, and the true and false constants.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
declare local var.boolean BOOL;

# BOOL assignment with RHS variable
set var.boolean = true;
set req.esi = var.boolean;
set resp.http.Bool = if(req.esi, "y", "n");

# BOOL assignment with RHS function
set var.boolean = http_status_matches(resp.status, "200,304");

# BOOL assigment with RHS conditional
set var.boolean = (req.url == "/");

# non-NULL-ness check, like 'if (req.http.Foo) { ... }'
set var.boolean = (req.http.Foo);

Numeric variables

Numeric assignment and comparison support numeric variables (anything except STRING or BOOL) on the right-hand side, including conversion in both directions between FLOAT and INTEGER types, rounding to the nearest integer in the FLOAT to INTEGER case.

Invalid conditions or domain errors like division by 0 will set fastly.error.

1
2
3
4
5
6
7
8
9
10
11
declare local var.integer INTEGER;
declare local var.float FLOAT;

# Numeric assignment with RHS variable and
# implicit string conversion for header
set var.integer = req.bytes_read;
set var.integer -= req.body_bytes_read;
set resp.http.VarInteger = var.integer;

# Numeric comparison with RHS variable
set resp.http.VarIntegerOK = if(req.header_bytes_read == var.integer, "y", "n");

String variables

String assignments support string concatenation on the right-hand side.

1
2
3
4
declare local var.restarted STRING;

# String concatenation on RHS
set var.restarted = "Request " if(req.restarts > 0, "has", "has not") " restarted.";

IP address variables

IP address variables represent individual IP addresses.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
acl office_ip_ranges {
  "192.0.2.0"/24;                              # internal office
  "198.51.100.4";                              # remote VPN office
  "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff";    # ipv6 address remote
}

declare local var.ip1 IP;
set var.ip1 = "192.0.2.0";

if (var.ip1 ~ office_ip_ranges) {
  ...
}

declare local var.ip2 IP;
set var.ip2 = "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff";

Time variables

Time variables support both relative and absolute times.

1
2
3
4
5
6
7
8
9
10
11
declare local var.time TIME;
declare local var.rtime RTIME;

set req.grace = 72s;
set var.rtime = req.grace;
set resp.http.VarRTime = var.rtime;

set var.time = std.time("Fri, 10 Jun 2016 00:02:12 GMT", now);
set var.time -= var.rtime;
# implicit string conversion for header
set resp.http.VarTime = var.time;

Back to Top