If your origin uses special values (e.g., request headers) to select content for users or to otherwise direct requests to appropriate security domains, consider including those values in your cache key or Vary header. Doing so will prevent you from accidentally caching content across security domains and could prevent malicious attackers from poisoning your cache.
Redefining the cache keyBy default, Fastly uses the URL and the Host of a request (plus a special, internal Fastly variable for purging purposes) to create unique HTTP objects. Although Fastly allows you to explicitly set the cache key to define this more precisely, changing the default behavior risks the following:
To avoid these dangers, consider using the Vary header instead of following the instructions below.
Explicitly setting the cache keyYou can set the cache key explicitly (including attaching conditions by adding a request setting via the Settings page in the service configuration controls and including a comma-separated list of cache keys. The values of the cache keys listed are combined to make a single hash, and each unique hash is considered a unique object.
For example, if you don't want the query string to be part of the cache key, but you don't want to change req.url
so that the query string still ends up in your logs, you could use the following text for the hash keys:
req.url.path, req.http.host
In the web interface, the text would appear in the Cache keys field:
As a general rule, you should always have req.url
as one of your cache keys or as part of one.
Because purging works on individual hashes, additions to cache keys can complicate purging URLs. However, it can also be simplified.
For example, if you were to change your cache key to just req.url
and not the default req.url, req.http.host
, then purging http://foo.example.com/file.html
would also purge http://bar.example.com/file.html
. Keep in mind this is because they're actually the same object in the cache!
On the other hand, if you were to change your cache key to req.url, req.http.host, req.http.Fastly-SSL
, you would have to purge http://example.com/
and https://example.com/
individually.
In the latter case, if you were to use the Vary header instead of changing the cache key, you could still have different content on the two URLs, yet purge them with a single purge. In this case you would add a new Cache Header, use http.Vary
as the Destination, and use the following as the Source:
if(beresp.http.Vary, beresp.http.Vary ",", "") "Fastly-SSL"
Using a cookie as a cache key
You can use a cookie as a cache key or just check for the presence of a cookie set to a specific value by controlling its request conditions. Both methods are simple and shown in the steps below.
To use a cookie as a cache keyUsing a cookie as a cache key looks complicated but it's actually quite simple. Let's say your cookie is called "MyCookie" and it looks like mycookie=
.
Log in to the Fastly control panel.
From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.
Click Edit configuration and then select the option to clone the active version.
Click Content.
Click Create header.
Fill out the Create a header fields as follows:
Set MyCookie Header Default
.http.X-MyCookie
."0"
(with quotes).10
for new headers.Click Create. The new header appears in the Headers area of the Content page.
Click Create header again and create a second new header by filling out the fields as follows:
Set MyCookie Header from Cookie
.http.X-MyCookie
.req.http.cookie:mycookie
.10
, enter 20
.Click Create. The second header appears in the Headers area of the Content page.
Click Attach a condition next to the Set MyCookie Header from Cookie
header.
Click Create a new request condition.
Fill out the fields of the Create a new request condition page as follows:
Has MyCookie cookie
.req.http.cookie:mycookie
.Click Save and apply to. The Headers area now displays the condition that must be met in order for your header to begin being used.
Click Settings.
Click Create request setting.
In the Name field, enter Set Hash from Cookie
.
Click Advanced options at the bottom of the page.
In the Cache keys field, enter req.url, req.http.host, req.http.X-MyCookie
Click Create.
Click Attach a condition next to the new request.
From the Select a condition menu, select Has MyCookie cookie
. The Request settings area now displays the condition that must be met in order for your request to begin being used.
Click Activate to deploy your configuration changes.
An alternative way if you're just checking for the presence of the cookie set to some specific value (e.g., 1):
req.url, req.http.host, "Has mycookie"
.req.http.cookie:mycookie
.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4