2

I'm trying to cache a website that is on an EC2, it has an URL uncached.xyz.com, so i created an Cloud Front with the origin pointed to uncached.xyz.com and created a record xyz.com that points to the cloudfront distribution, named something like d111111abcdef8.cloudfront.net,

But all information returned by the server (like button urls) are not relative and include the request URL, meaning that if Cloudfront access the origin with uncached.xyz.com, the contents returned by the CDN (for any cname used) will contain the Origin url instead of the url used to access the CloudFront distribution, looking like an <a href="uncached.xyz.com/info"><a> where it should be <a href="xyz.com/info"><a>.

tl:dr

CloudFront should return:
<a href="xyz.com/info"><a> (which is the url accessed that points to the CDN distribution (the cached website))
But it returns:
<a href="uncached.xyz.com/info"><a> (which is the Origin URL that contains the data to be cached (the website))

Is there a way to "spoof" the Origin server in to thinking that the url being used to access it is the CloudFront distribution instead of the origin URL configured?

Simpler version of what i'm using.

exemple architecture

Ollegn
  • 73
  • 1
  • 7

1 Answers1

2

There are several ways to do that:

Set the headers X-Forwarded-Host & X-Forwarded-Proto within Cloudfront configuration for origin requests to pass the required information to the backend application. On the backend use the Headers, if present, for generating the content within the pages.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Host

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto

Another approach could be to setup a lambda@edge that processes the origin response, and replaces the content where needed.

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-updating-http-responses.html

hargut
  • 3,908
  • 7
  • 10
  • Thanks to your answer i also found some nice docs! https://aws.amazon.com/pt/premiumsupport/knowledge-center/configure-cloudfront-to-forward-headers/ – Ollegn Jan 25 '21 at 20:40
  • 1
    Your welcome. Directly whitelisting the header is also possible, it depends with what the backend can cope. Custom headers can as well be added within the origin configuration. https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/add-origin-custom-headers.html – hargut Jan 25 '21 at 20:47