Conditions control how requests are processed. You can use them to add logic to any basic configuration object in a service and have them control if and when that object is applied. Conditions require minimal programming. They allow you to wrap configuration objects attached to your service in a VCL IF statement.
Before you start using conditions
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.
A simple condition example
The simplest way to explain how Fastly handles conditions is this IF statement:
1 2 3 4 IF this condition happens THEN 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:
1 2 3 4 IF 404 Not Found is what we have to tell the customer THEN 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:
1 2 3 4 IF beresp.status == 404 THEN 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.
Ideas for using conditions
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||Generating HTTP redirects at the edge|
Types of conditions and when you can use them
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.
|Condition type||Applied when Fastly …||Works with which VCL variables|
|Request||processes a request||
|Response||processes a response to a request||
|Cache||receives a response from your origin, just before that response is (potentially) cached||
Where to go for more information
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.