Last updated 2023-10-03
Conditions are a configuration mechanism that you can add to your service to control how particular requests are processed. Like conditions in general programming, Fastly conditions use IF-statements to evaluate requests using the logic you specify and formulate a response based on whether the request meets the condition (TRUE) or does not meet the condition (FALSE).
Be sure you understand the construction of basic logical expressions before you start using conditions. Specifically, you should understand basic C-style logical expression syntax (e.g., basic logic, operators such as && and precedence) when working with conditions. A basic programming guide that deals with IF style expressions in either the C or Perl language (the Tizag Perl tutorial is a good one to start with). Even though they aren't directly applicable to our condition examples, the syntax of these languages is similar to VCL.
The simplest way to explain how Fastly handles conditions is this IF statement:
1IF2 this condition happens3THEN4 respond this way
A practical example can demonstrate this. The vast majority of the time, your site processes requests for information as usual, but every so often customers mistype a search term or simply can't find what they're looking for and you're forced to display a 404 Not Found error. You've realized that when that happens, the standard 404 Not Found errors on your website aren't as helpful as they could be. To fix this, any time your server can't find what a customer is looking for (a condition), you want to display a customized 404 message instructing customers to contact your support team for help (a response).
In plain English, the IF statement might look like this:
1IF2 404 Not Found is what we have to tell the customer3THEN4 respond with the special Contact Support page
The IF line in the example above is the condition you've set. The THEN line describes what will happen if that condition is met.
If you were to replace the English in the example above with VCL variables and a little bit of HTML, it might look like this instead:
1IF2 beresp.status == 4043THEN4 respond with <html><body><h1>Can't find it?</h1><p>Contact firstname.lastname@example.org for help.</p></body></html>
Interested in doing this? We have step-by-step instructions for creating error pages with custom responses.
Need some more ideas for when you could use conditions? Explore these:
|A web robot wants to crawl a particular area of your website
|Provide a customized robots.txt file defining which areas of your website should not be processed or scanned
|Creating and customizing a robots.txt file
|Your server needs to return a 404 Not Found response
|Change the default caching time for only 404 responses from 3600 seconds (60 minutes) to 120 seconds (2 minutes)
|Overriding caching defaults based on a backend response
|Users request a popular page on your site but it's been moved to a different area
|Have Fastly redirect the page requests at the edge, without having to go back to your origin server for it
We group conditions into three types:
- request conditions
- response conditions
- cache conditions
A condition's type dictates which configuration objects it can be applied to during a specific stage of the caching process. In addition, each stage of caching works with a different set of VCL variables that can be used to create conditions.
|Applied when Fastly ...
|Works with which VCL variables
|processes a request
|processes a response to a request
|receives a response from your origin, just before that response is (potentially) cached
The Varnish Cache documentation 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.