About conditions

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).

the manage conditions page displaying a single, unattached request condition

You can apply a condition using the Conditions feature of the web interface or by using a VCL IF statement on a snippet or in custom VCL

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:

1IF
2 this condition happens
3THEN
4 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:

1IF
2 404 Not Found is what we have to tell the customer
3THEN
4 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:

1IF
2 beresp.status == 404
3THEN
4 respond with <html><body><h1>Can't find it?</h1><p>Contact support@example.com 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:

ConditionResponseLearn how
A web robot wants to crawl a particular area of your websiteProvide a customized robots.txt file defining which areas of your website should not be processed or scannedCreating and customizing a robots.txt file
Your server needs to return a 404 Not Found responseChange 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 areaHave Fastly redirect the page requests at the edge, without having to go back to your origin server for itRedirects

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 typeApplied when Fastly ...Works with which VCL variables
Requestprocesses a requestclient.*
server.*
req.*
Responseprocesses a response to a requestclient.*
server.*
req.*
resp.*
Cachereceives a response from your origin, just before that response is (potentially) cachedclient.*
server.*
req.*
beresp.*

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.

Was this guide helpful?

Do not use this form to send sensitive information. If you need assistance, contact support. This form is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.