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
/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:
- Log in to the Fastly application.
Click the configure tab (wrench icon).
From the Service menu, select the appropriate service and then click the blue Configure button. The main controls for the selected service appear.
Click Content from the section list on the left.
In the Responses area at the bottom of the page, click the New button. The New Response window appears.
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.
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:
Click the gear icon next to the response that you just created and select Request Conditions.
The New Condition window appears.
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.
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
! (req.url ~ "^/(Content|Scripts)/")
Respond only if URLs match
/secret/* or are Microsoft Word or Excel documents (
*.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
req.http.user-agent ~ "annoying_robot"
Prevent a specific IP from connecting, in this case the IP
client.ip == "18.104.22.168"
Use geographic variables to block traffic from a specific location, in this case China:
geoip.country_code == "CN"
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