Controlling origin requests
When a viewer request to CloudFront results in a cache miss (the requested object is not cached at the edge location), CloudFront sends a request to the origin to retrieve the object. This is called an origin request. The origin request always includes the following information from the viewer request:
-
The URL path (the path only, without URL query strings or the domain name)
-
The request body (if there is one)
-
The HTTP headers that CloudFront automatically includes in every origin request, including
Host
,User-Agent
, andX-Amz-Cf-Id
Other information from the viewer request, such as URL query strings, HTTP headers, and cookies, is not included in the origin request by default. But you might want to receive some of this other information at the origin, for example to collect data for analytics or telemetry. You can use an origin request policy to control the information that's included in an origin request.
Origin request policies are separate from cache policies, which control the cache key. This separation enables you to receive additional information at the origin and also maintain a good cache hit ratio (the proportion of viewer requests that result in a cache hit). You do this by separately controlling which information is included in origin requests (using the origin request policy) and which is included in the cache key (using the cache policy).
Although the two kinds of policies are separate, they are related. All URL query strings, HTTP headers, and cookies that you include in the cache key (using a cache policy) are automatically included in origin requests. Use the origin request policy to specify the information that you want to include in origin requests, but not include in the cache key. Just like a cache policy, you attach an origin request policy to one or more cache behaviors in a CloudFront distribution.
You can also use an origin request policy to add additional HTTP headers to an origin request that were not included in the viewer request. These additional headers are added by CloudFront before sending the origin request, with header values that are determined automatically based on the viewer request. For more information, see Adding CloudFront request headers.
Topics
Creating origin request policies
You can use an origin request policy to control the values (URL query strings, HTTP headers, and cookies) that are included in requests that CloudFront sends to your origin. You can create an origin request policy in the CloudFront console, with the Amazon Command Line Interface (Amazon CLI), or with the CloudFront API.
After you create an origin request policy, you attach it to one or more cache behaviors in a CloudFront distribution.
Origin request policies are not required. When a cache behavior does not have an origin request policy attached, the origin request includes all the values that are specified in the cache policy, but nothing more.
Note
To use an origin request policy, the cache behavior must also use a cache policy. You cannot use an origin request policy in a cache behavior without a cache policy.
Understanding origin request policies
CloudFront provides some predefined origin request policies, known as managed policies, for common use cases. You can use these managed policies, or you can create your own origin request policy that's specific to your needs. For more information about the managed policies, see Using the managed origin request policies.
An origin request policy contains the following settings, which are categorized into policy information and origin request settings.
Policy information
- Name
-
A name to identify the origin request policy. In the console, you use the name to attach the origin request policy to a cache behavior.
- Description
-
A comment to describe the origin request policy. This is optional.
Origin request settings
Origin request settings specify the values in viewer requests that are included in requests that CloudFront sends to the origin (known as origin requests). The values can include URL query strings, HTTP headers, and cookies. The values that you specify are included in origin requests, but are not included in the cache key. For information about controlling the cache key, see Controlling the cache key.
- Headers
-
The HTTP headers in viewer requests that CloudFront includes in origin requests. For headers, you can choose one of the following settings:
-
None – The HTTP headers in viewer requests are not included in origin requests.
-
All viewer headers – All HTTP headers in viewer requests are included in origin requests.
-
All viewer headers and the following CloudFront headers – All HTTP headers in viewer requests are included in origin requests. Additionally, you specify which of the CloudFront headers you want to add to origin requests. For more information about the CloudFront headers, see Adding CloudFront request headers.
-
Include the following headers – You specify which HTTP headers are included in origin requests.
Note
Do not specify a header that is already included in your Origin Custom Headers settings. For more information, see Configuring CloudFront to add custom headers to origin requests.
-
All viewer headers except – You specify which HTTP headers are not included in origin requests. All other HTTP headers in viewer requests, except for the ones specified, are included.
When you use the All viewer headers and the following CloudFront headers, Include the following headers, or All viewer headers except setting, you specify HTTP headers by the header name only. CloudFront includes the full header, including its value, in origin requests.
Note
When you use the All viewer headers except setting to remove the viewer's
Host
header, CloudFront adds a newHost
header with the origin's domain name to the origin request. -
- Cookies
-
The cookies in viewer requests that CloudFront includes in origin requests. For cookies, you can choose one of the following settings:
-
None – The cookies in viewer requests are not included in origin requests.
-
All – All cookies in viewer requests are included in origin requests.
-
Include the following cookies – You specify which cookies in viewer requests are included in origin requests.
-
All cookies except – You specify which cookies in viewer requests are not included in origin requests. All other cookies in viewer requests are included.
When you use the Include the following cookies or All cookies except setting, you specify cookies by their name only. CloudFront includes the full cookie, including its value, in origin requests.
-
- Query strings
-
The URL query strings in viewer requests that CloudFront includes in origin requests. For query strings, you can choose one of the following settings:
-
None – The query strings in viewer requests are not included in origin requests.
-
All – All query strings in viewer requests are included in origin requests.
-
Include the following query strings – You specify which query strings in viewer requests are included in origin requests.
-
All query strings except – You specify which query strings in viewer requests are not included in origin requests. All other query strings are included.
When you use the Include the following query strings or All query strings except setting, you specify query strings by their name only. CloudFront includes the full query string, including its value, in origin requests.
-