Using conditions

Last updated July 31, 2017

Conditions use the Varnish Control Language (VCL) to define when an configuration object should be applied while processing requests to a cache server. Once you understand some basics about conditions, use this guide to learn about how to create conditions using the Fastly web interface and when to use them.

Conditions on the Manage conditions page

The Manage conditions page provides an overview of all the conditions currently available to your service. You can see at a glance which conditions are mapped to configuration objects. It allows you to create new conditions and search for existing ones.

To view conditions on the Manage conditions page:

Log in to the Fastly web interface and click the Configure link.

From the service menu, select the appropriate service.

Click the Configuration button and then select Clone active. The Domains page appears.

Click the Conditions link. A list of all conditions for your service appears.

For example, this service has one request condition available:

The Example Request Condition shown above currently isn't applied to a configuration object (as indicated by "Not applied to anything"). If it was, it would instead appear similar to this:

Conditions attached directly to a configuration object

Configuration objects appear differently in the web interface when conditions are attached to them. For example, this request setting has no condition attached to it:

Once you click the Attach a condition link to create a new condition or attach an existing condition, however, the web interface changes how the configuration object appears:

By default, configuration objects hide the majority of details for any attached conditions. You can unhide those details by clicking the Show details link. When expanded, the details vary depending on the type of condition.

Parts of a properly configured condition

Conditions require only a few parameters, making them appear deceptively simple. Specifically, they require:

a Type parameter that classifies the condition being added. If added via the Manage conditions page, the type can always be manually selected. If added via the Attach a condition link on a configuration object, the type is automatically applied whenever possible.

a Name parameter that serves human-readable identifier of the condition.

an Apply if statement containing the logical expression to execute in VCL to determine if condition resolves as True or False.

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:

This operator ...

Does this ...

( )

groups 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

An example of adding conditions

The scenario: You want to add a new origin server that handles a specific portion of your API requests. Some requests to this API must be cached differently than other requests to your API, so you want to set special headers for specific types of requests. Specifically, you don't want your new origin server to cache PUT, POST, or DELETE requests because they're special for this particular API and send back extra, time dependent, meta-information in each response. And finally, you want to track the effectiveness of doing this. To accomplish all of this using conditions via the Fastly web interface, you would:

Create a new origin server

To create a new origin server that will handle the special API traffic, follow the instructions for connecting to origins. You'll add specific details about your API server when you fill out the Create a host fields:

In the Name field, type a name for your API server (for example, Special API Set Header).

In the Address field, type the IP address (or hostname) of the API server.

Create a request condition

Once you've created a new origin server to handle the special API traffic, tell the cache how to route requests to this origin server by creating a request condition.

In the Hosts area, click the Attach a condition link next to the name of the origin server you just created. The Add a condition to window appears.

You can either select an available condition or you can click the Create a new request condition button. The Create a new request condition window appears.

Fill out the Create a new request condition fields as follows:

In the Name field, type a descriptive name for the new condition (for example Special API Request).

In the Apply if field, type the appropriate request condition that will be applied (for example, req.url ~ "^/special/" could address all requests related to the special API server).

Click the Save and apply to button to create the new condition for the host.

Create a cache settings object

Requests are now are being properly routed to the new origin server. Next, create a cache settings object to ensure the origin doesn't cache any responses from PUT, POST, or DELETE requests. They're special for this particular API and send back extra, time dependent, meta-information in each response.

In the Name field, type a descriptive name for the new cache settings.

Leave the TTL (seconds) field set to its default value.

From the Action menu, select Pass (do not cache).

Leave the Stale TTL (seconds) field set to its default value.

Click the Create button.

Create and apply a condition to the cache settings object

Create a new condition that specifies when the cache settings object should be applied.

In the Cache Settings area, click the Attach a condition link next to the name of the cache setting you just created. The Add a condition to window appears.

Click Create a new cache condition button. The Create a new cache condition window appears.

Fill out the Create a new cache condition fields as follows:

In the Name field, type a descriptive name for the new condition (for example, Special API Set Header).

In the Apply if field, type the appropriate request condition that will be applied (for example, req.request ~ "PUT|POST|DELETE" && beresp.status == 200).

Click the Save and apply to button to create the new condition for the cache setting.

Create a new header

To make sure you can track the effectiveness the new API, create a new header so you can use it to gather information about the special API requests as they happen.

Click the Content link. The Content page appears.

In the Headers area, click the Create header button to create a new header. The Create a header page appears.

Fill out the Create a header fields as follows:

In the Name field, type a descriptive name for the new header (for example, Special API Set Header).

From the Type menu, select Response and from the Action menu, select Set.

In the Destination field, type the name of the header that will be affected by the action (for example, http.super).

In the Source field, type a description of the source where the content for this header comes from (for example, "Thanks for asking!").

Leave the Ignore if set and Priority fields set to their default settings.

Click Create.

Create a request condition for the new header

Once the header is created, create an associated condition to ensure this header is only set on that special type of request.

In the Headers area, click the Attach a condition link next to the name of the new header you just created. The Create a new request condition window appears.

Fill out the Create a new request condition fields as follows:

In the Name field, type a descriptive name for the new condition (for example, Special API Response Condition).

In the Apply if field, type the appropriate request condition that will be applied (for example, req.url ~ "^/special" && resp.status == 200).

Click the Save and apply to button to create the new condition for the header.

Check your work

Before activating the configuration, review the generated VCL to see how Fastly converted the objects and conditions into actual VCL. For the example shown above, the VCL for the request condition appears as:

As you become more familiar with the VCL syntax and programming, look at the generated VCL to see if the configuration is doing what you think it is doing (most VCL is pretty simple once you know what the variables are referring to).

Was this guide helpful?

Yes
No

Tell us what worked and what we could do better.

Do not use this form to send sensitive information. If you need assistance, contact support@fastly.com.