Skip to content

configure the Cloud Front to set the origin request header

0

I am trying to migrate our CDN service, here are the details: Origin domain: origin.example.com Viewer domain: bps-sim.example2.com How can I configure the Cloud Front to set the origin request header to origin domain: origin.example.com? Because the service origin.example.com only accept this domain for service. Additionally, I don't want to use lambda function.

asked a year ago555 views

2 Answers
0

Hello.

What is your current CloudFront Origin Request Policy setting?
If the header setting in the origin request policy is "All viewer headers", all headers in the viewer request are sent to the origin, and the host header will be that of CloudFront.
Try changing it to "All viewer headers except" or using "Include the following headers" to forward only the headers you need to your origin.
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/origin-request-understand-origin-request-policy.html

By the way, the previous settings will be used until the settings are reflected in the edge location, so it is possible that you are accessing an edge location where the settings have not been reflected.
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/HowToUpdateDistribution.html

In the CloudFront console, you can see the CloudFront distributions that are associated with your AWS account, view the settings for a distribution, and update most settings. Be aware that settings changes that you make won't take effect until the distribution has propagated to the AWS edge locations.

According to this AWS blog, CloudFront settings are reflected in about 5 minutes, but it is possible that it may take a while for the settings to be reflected temporarily.
https://aws.amazon.com/jp/blogs/networking-and-content-delivery/slashing-cloudfront-change-propagation-times-in-2020-recent-changes-and-looking-forward/

EXPERT

answered a year ago

  • I already configured the policy AllViewerExceptHostHeader, but still. When I checked the logs from the origin service, the host domain is still viewer domain: bps-sim.example2.com.

  • Could you please share the contents of the AWS CLI "get-distribution" and "get-origin-request-policy" commands? Just to be sure, we need to check whether an origin request policy is associated with CloudFront. https://docs.aws.amazon.com/cli/latest/reference/cloudfront/get-distribution.html https://docs.aws.amazon.com/cli/latest/reference/cloudfront/get-origin-request-policy.html

  • ❯ aws cloudfront get-distribution --id E1RWOYRPJL7CC1 | jq '.Distribution.DistributionConfig.DefaultCacheBehavior.OriginRequestPolicyId' "b689b0a8-53d0-40ab-baf2-68738e2966ac"

    ❯ aws cloudfront get-origin-request-policy --id b689b0a8-53d0-40ab-baf2-68738e2966ac { "ETag": "E23ZP02F085DFQ", "OriginRequestPolicy": { "Id": "b689b0a8-53d0-40ab-baf2-68738e2966ac", "LastModifiedTime": "1970-01-01T00:00:00+00:00", "OriginRequestPolicyConfig": { "Comment": "Policy to forward all parameters in viewer requests except for the Host header", "Name": "Managed-AllViewerExceptHostHeader", "HeadersConfig": { "HeaderBehavior": "allExcept", "Headers": { "Quantity": 1, "Items": [ "host" ] } }, "CookiesConfig": { "CookieBehavior": "all" }, "QueryStringsConfig": { "QueryStringBehavior": "all" } } } }

-1

To configure CloudFront to set the origin request header to your origin domain (origin.example.com) when the viewer is accessing your content through bps-sim.example2.com, you can use CloudFront's origin request policies without needing Lambda functions.

Since your origin server (origin.example.com) only accepts requests with its own domain in the Host header, you'll need to ensure CloudFront sends the correct Host header in the origin request.

Here's how to configure this:

  1. When creating or updating your CloudFront distribution, set your origin domain to origin.example.com.

  2. For the cache behavior that handles requests to bps-sim.example2.com, apply the "AllViewerExceptHostHeader" managed origin request policy. This policy includes all headers, cookies, and query strings from the viewer request except for the Host header.

  3. When you use this managed policy to remove the viewer's Host header, CloudFront will automatically add a new Host header with the origin's domain name (origin.example.com) to the origin request.

This approach ensures that when viewers access your content through bps-sim.example2.com, CloudFront will forward requests to your origin with the Host header set to origin.example.com, which is what your origin server requires. This solution doesn't require any Lambda functions or custom code.
Sources
Use managed origin request policies - Amazon CloudFront
How do I make CloudFront request headers available in my edge compute functions? | AWS re:Post

answered a year ago

  • I already configured the policy AllViewerExceptHostHeader, but still. When I checked the logs from the origin service, the host domain is still viewer domain: bps-sim.example2.com.

  • I'm seeing the same issue. Using a Cache Policy of UseOriginCacheControlHeaders-QueryStrings with an Origin Request policy of AllViewerExceptHostHeader, I am still seeing the Cloudfront Distribution Domain name being sent to my backend server as the Host name. This is causing an issue since my backend server is configured as a virtual host.

    Please advise.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.