How do I configure CloudFront to forward the host header to the origin?

2 minute read
2

The origin that's configured on my Amazon CloudFront distribution uses virtual hosting. Because of this, my distribution must forward the host header to my origin server. I want to configure my distribution to forward the host header.

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.

Important: For Amazon Simple Storage Service (Amazon S3) origins, caches based on the host header isn't supported. For more information, see Selecting the headers to base caching on.

If you create a custom policy for API Gateway origins without custom domain, then don't forward the HOST header. Otherwise, you get a 403 Error.

Resolution

Create a cache policy and an origin request policy

  1. Follow the steps to create a cache policy using the CloudFront console.
  2. Under Cache key settings, for Headers, choose Include the following headers. From the Add header dropdown list, choose Host.
  3. Complete all other settings of the cache policy based on the requirements of the behavior that you attach the policy to. Then, choose Create.
  4. After you create the cache policy, follow the steps to attach the policies to the relevant behavior of your CloudFront distribution.

Edit the settings of an existing behavior

  1. Open the CloudFront console. Then, choose your distribution.
  2. Choose the Behaviors tab, and then choose the path to forward the host header to.
  3. Choose Edit.
  4. Under Cache key and origin requests, confirm that Legacy cache settings is selected. If it's not selected, then follow the steps in the preceding section to create a cache policy. If Legacy cache settings is selected, then complete the following values:
    For Headers, choose Include the following headers.
    From the Add header dropdown list, choose Host.
  5. Choose Save Changes.

Related information

Caching content based on request headers

Working with policies

AWS OFFICIAL
AWS OFFICIALUpdated 8 months ago
4 Comments

Is it possible to forward the header under a different name? For example "host" -> "x-forwarded-host"?

replied 5 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 5 months ago

I have the same question, is it possible to forward the header under a different name? For example "host" -> "x-forwarded-host"?

If you updated the Knowledge Center, can you post a link to the information here so there's a breadcrumb trail for future pioneers? Thanks.

profile picture
johnny
replied a month ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied a month ago