Last updated August 25, 2016
Use conditions to specify how to edit and activate configurations. This allows you to add logic to any basic configuration object in a service. When you assign a condition to an object in your service, you are wrapping the object in a VCL if-statement. If the condition is met, then the object is used when processing an end-user request.
Before you begin
Because of their complexity and power, be sure you're comfortable with the following before you begin using conditions:
- Know how to edit and activate configurations using the Fastly web interface. If you are not familiar with basic editing using the web interface, see our documentation to learn more before moving forward.
- Understand basic C-style logical expression syntax (e.g., basic logic, operators such as &&, and precedence). If you do not have experience with these types of expressions, refer to a basic programming guide (such as the Tizag Perl tutorial) that deals with "IF" style expressions in either C or Perl. (These languages are not directly applicable to the tutorial because VCL is neither C nor Perl. However, the syntax is similar.)
Conditions are saved in your service like any other configuration object, but they can only be accessed, assigned, and manipulated via the Attach a condition link next to that object. The link launches an Add a condition window that allows you to select an existing condition or create a new one and then apply it in multiple places across your configuration.
Properly configured conditions require only a few parameters, making them appear deceptively simple:
- a Type - The type of condition being added, usually automatically selected by the system. For example, if you create a cache header, you can only apply a cache condition. Note that with a response setting, you can select either a request condition (based on what the user is requesting) or a cache condition (based on what the backend is returning).
- a Name - A human-readable identifier for the condition.
- an Apply if statement - The logical expression to execute in VCL to determine if condition resolves as True or False.
- the Priority - A number used to determine the order in which multiple conditions execute. Lower numbers execute first. Defaults to 10.
Most problems occur in the Apply if parameter because it uses actual VCL code to specify when a condition should be applied.
Working with conditions
A basic condition used by Fastly might look like this:
req.url ~ "^/api/"
This condition tells the cache server to pay attention if a request it receives begins with the string
~ operator performs a Perl-style regular expression match with the
"^/api/" regex and returns true if
req.url matches. You might use this condition when you want to specifically route certain requests for your service. In this example, you might be routing requests to an API backend that exists on your website.
Matching conditions on logical expressions
Conditions can perform matches on more complicated logical expressions. For example:
client.ip == "127.0.0.1": The client requesting a resource on your service has the IP
req.http.host ~ "example.com": The host header of the incoming request is
req.request ~ "POST|PUT" && req.url ~ "^/api/articles/": The request is a POST or a PUT and the URL begins with
req.url conditions shown above all represent configuration variables in VCL.
Operators for complex conditions
You could also get creative and create a more complex condition used by Fastly that might look like this:
req.http.host = "www.example.com" && (req.url !~ "^/foo" || req.url !~ "^/bar/" || req.url !~ "^/baz/")
This condition tells the cache server that the URL should equal
www.example.com and the url cannot match
www.example.com/baz. You might use this type of condition when you have multiple variables or options and want to fine-tune your results. In this example, you are indicating that you don't want URLs that contain
baz by using the following operators:
( )groups your expressions and restricts alteration to part of the regex
| |performs an alternation where each variable is checked until it finds a variable that is true
!~excludes any urls that include the specified variables
Where to go for more information
The Varnish Cache documentation on variables provides a complete list of variables you can use to craft conditions. Keep in mind, however, some of the variables Varnish allows may not be available or may have no meaning in the context of Fastly services. For more information, see our Guide to VCL.Back to Top