Hear from Slack, the ACLU, TED, & more at our customer summit in San Francisco Register

About conditions

  Last updated May 04, 2017

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:

Condition basics

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:

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 "/api/". The ~ 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:

The client.ip, req.http.host, req.request, and 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/foo, or www.example.com/bar, or 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 foo, bar, or baz by using the following operators:

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