Custom responses that don't hit origin servers

Fastly can send custom responses for certain requests that you don't want to hit your origin servers. For example, if you wanted to restrict caching to a URL subtree that contains images and scripts, you could configure Fastly to return a HTTP 404 Not Found response to requests for anything other than /Content/* or /Scripts/*. To illustrate how to implement this example, we'll show you how to create a response and corresponding request condition.

Creating the response

Follow these instructions to create a new response for your service:

  1. Log in to the Fastly application.
  2. Click the configure tab (wrench icon).

    the configure tab

  3. From the Service menu, select the appropriate service and then click the blue Configure button. The main controls for the selected service appear.

  4. Click Content from the section list on the left.

    the Content window

  5. In the Responses area at the bottom of the page, click the New button. The New Response window appears.

    the new response window

  6. Fill out the New Response fields as follows:

    • In the Name field, type a human-readable name for the response.
    • From the Status field, select an HTTP code to return to the client.
    • In the MIME Type field, type the MIME type of the response.
    • In the Content field, type the plaintext or HTML content to return to the client.
  7. Click the Create button to create the response.

Creating the request condition

Follow these instructions to attach a request condition to the response you just created:

  1. Click the gear icon next to the response that you just created and select Request Conditions.

    the request conditions menu selection in the responses area

    The New Condition window appears.

    the new condition window

  2. Fill out the New Condition fields as follows:

    • In the Name field, type a human-readable name for the condition.
    • In the Apply If field, type the request condition you want inserted into a VCL if statement. See the examples below for more information.
    • In the Priority field, type the priority for the request condition. Leave as default if this is the only request condition.
  3. Click the Create button to create the condition.

To activate the new response and request condition, deploy the version of the service you are editing.

Example request conditions

Respond only if URLs don't match a certain mask, in this case /Content/* or /Scripts/*:

  ! (req.url ~ "^/(Content|Scripts)/")

Respond only if URLs match /secret/* or are Microsoft Word or Excel documents (*.doc and *.xls file extensions):

  ! (req.url ~ "^/secret/" || req.url ~ "\.(xls|doc)$")

Ignore POST and PUT HTTP requests:

  req.request == "POST" || req.request == "PUT"

Deny spider or crawler with user-agent "annoying_robot":

  req.http.user-agent ~ "annoying_robot"

Prevent a specific IP from connecting, in this case the IP

  client.ip == ""

Use geographic variables to block traffic from a specific location, in this case China:

  geoip.country_code == "CN"

Match client.ip against a CIDR range, such as (this requires first creating an ACL object in VCL):

  client.ip ~ ipRangeObject
Back to Top