LOG IN SIGN UP
Documentation

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 an 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 web interface and click the Configure link.
  2. From the service menu, select the appropriate service.
  3. Click the Edit configuration button and then select Clone active. The service version page appears.
  4. Click the Content tab. The Content page appears.

    the Content tab on the Configuration page

  5. Click the Create response button. The Create a new synthetic response page appears.

    the create a new synthetic response window

  6. Fill out the Create a new synthetic response fields as follows:

    • From the Status menu, select an HTTP code to return to the client. For example, 404 Not Found.
    • In the MIME Type field, type the MIME type of the response. For example, text/html.
    • In the Response field, type the plaintext or HTML content to return to the client. For example Page not found.
    • In the Description field, type a human-readable name for the response. For example Return Not Found.
  7. Click the Create button to create the response. The new response appears in the Responses area of the Content tab.

    an example synthetic response to return when a page isn't found

Creating the request condition

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

  1. Click the Attach a condition link next to the response that you just created. The Add a condition window appears.

    an example add a condition window

  2. Click the Create a new condition button. The Create a new condition window appears.

    the request conditions menu selection in the responses area

  3. Fill out the Create a new condition fields as follows:

    • From the Type menu, select Request.
    • In the Name field, type a human-readable name for the condition. For example, Return Not Found.
    • In the Apply if field, type the request condition you want inserted into a VCL if statement. For example, ! ( req.url ~ "^/(Content|Scripts)/" ). See below for more examples of request conditions.
  4. Click the Save and apply to button. The Responses area now displays the condition that must be met in order for your response to begin being used.

    responses condition return not found that will trigger the display of page not found

  5. Click the Activate button to deploy your configuration changes.

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 a spider or crawler using user-agent "annoying_robot":

  req.http.user-agent ~ "annoying_robot"

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

  client.ip == "225.0.0.1"

Use geographic variables to block traffic from a specific location (e.g., China):

  geoip.country_code == "CN"

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

  client.ip ~ ipRangeObject

Back to Top