The origin that's configured on my Amazon CloudFront distribution uses virtual hosting. I want to configure my distribution to forward the host header to my origin server.
Short description
To configure your distribution to forward the host header to the origin, take one of the following actions:
- Create a cache policy and an origin request policy.
- Edit the settings of an existing behavior in the distribution.
If you create a custom policy for Amazon API Gateway origins without a custom domain, then don't forward the host header. Otherwise, you get a "403 error".
Important: For Amazon Simple Storage Service (Amazon S3) origins, CloudFront doesn't support caches based on the host header. For more information, see Select the headers to base caching on.
Resolution
Create a cache policy and an origin request policy
Complete the following steps:
- Open the CloudFront console.
- In the navigation pane, choose Policies.
- In the Custom policies section, choose Create cache policy.
- Under Cache key settings, for Headers, choose Include the following headers.
- From the Add header dropdown list, choose Host.
- Complete all other settings of the cache policy based on your requirements.
- Choose Create.
- Attach the policies to the relevant behavior of your CloudFront distribution.
Edit the settings of an existing behavior in the distribution
Complete the following steps:
- Open the CloudFront console.
- Select your distribution.
- Choose the Behaviors tab, and then select the path to forward the host header to.
- Choose Edit.
- Confirm that Cache key and origin requests is set to Legacy cache settings.
Note: If Legacy cache settings isn't selected, then complete the steps in the preceding section to create a cache policy.
- Under Legacy cache settings, for Headers, choose Include the following headers.
- From the Add header menu, choose Host.
- Choose Save Changes.
(Optional) Forward the header under a different name
To forward the host header value under a custom or different header name, use a CloudFront function or AWS Lambda@Edge function.
To forward the header under a different name, complete the following steps:
- Check the incoming host header.
Note: For CloudFront functions, check the viewer request event. For Lambda@Edge, check the view or origin request event.
- To store the host header value from a client request to the header, use the Lambda function handler command in the viewer request.
Note: For example, if the incoming host header is host, then add a new header that has the same value. See the following code example:
function handler(event) { const request = event.request;
const headers = request.headers;
const host = request.headers.host.value;
headers['x-forwarded-host'] = { "value": host }
return request
}
Note: This example function activates a viewer request event.
- To forward the custom header to the origin, configure the cache or host request policy to allowlist the X-Forwarded-Host header. For more information, see Request and response behavior for custom origins.
Related information
Cache content based on request headers
Control the cache key with a policy