table.lookup

STRINGtable.lookupIDidSTRINGkeySTRINGdefault

Available in all subroutines.

Looks up the key key in the table ID. When the key is present, its associated value will be returned. When the key is absent, a not set string value is returned.

When a third STRING argument is provided, the lookup function behaves as it would normally, except when a key is absent, the default value is returned instead.

Examples

table geoip_lang {
"US": "en-US",
"FR": "fr-FR",
"NL": "nl-NL",
}
if (!req.http.Accept-Language) {
set req.http.Accept-Language = table.lookup(geoip_lang, geoip.country_code, "en-US");
}
table extension {
"xhtml5": "xhtml",
"html5": "html",
"htm": "html",
"aif": "aiff",
"tif": "tiff",
"jpg": "jpeg",
"mpg": "mpeg",
}
sub vcl_recv {
# normalizing URL file extensions
declare local var.ext STRING;
# lowercase for both the table key and for the default value
set var.ext = std.tolower(req.url.ext);
set var.ext = table.lookup(extension, var.ext, var.ext);
}

Try it out

table.lookup is used in the following solution recipes, which show real world use cases. Click RUN to provision a Fastly service, execute the code on Fastly, and see how the function behaves.

HTTP Basic Auth

Store username/password list in an edge dictionary, authorize user at the edge, reject requests that don't have correct credentials.

Dictionary based IP block list

Block a list of IP addresses from accessing your service and include an expiry time.

Redirect old URLs at the edge

Use a dictionary of URL mappings to serve your redirects at lightning speed.

User contributed notes

We welcome comments that add use cases, ideas, tips, and caveats. All comments will be moderated before publication. To post support questions, visit our support center and we'll find you the help you need.