Search code examples
amazon-cloudfront

Cloudfront with custom origin and identical domain


Let's say a website with the domain www.example.com is hosted on a LAMP server of a webhoster, which is not Amazon. The domain is managed by Route53.

Is it possible to somehow keep all settings on the LAMP webserver and still use www.example.com as the domain for Cloudfront? Like:

Client -> www.example.com -> Cloudfront Edge Server -> Custom origin available over www.example.com on LAMP webserver of third party webhoster

Basically, I want to use www.example.com for both the Cloudfront Edge Server and the LAMP server.

Best regards


Solution

  • You cannot do anything like /etc/hosts on CloudFront -- it always uses public DNS to resolve the origin.

    However, you can still do what you are trying to do -- but you just need to understand why this solution is indeed what you want, because it will seem like it is not (as you indicated in comments).

    In Route 53, create a new A record for a new hostname for the origin server, such as origin.example.com. You do not configure this value anywhere on your origin server at all. Your origin server still believes it is www.example.com.

    In Route 53, create an alias A record www.example.com pointing to origin.example.com.

    At this point, your site works exactly as you expect and require. The hostname "origin.example.com" is in the resolution path, but this information is invisible and unknown to the origin.

    In CloudFront, create a distribution, setting the origin domain name to origin.example.com and the Alternate Domain Name for the distribution to www.example.com.

    In the settings for each Cache Behavior, ensure that the Host header is whitelisted for forwarding to the origin.

    Change Route 53's alias for www.example.com to point to your CloudFront distribution.

    When requests arrive at CloudFront, the request retains the Host: www.example.com header. CloudFront uses DNS to find the IP address for origin.example.com however, it only uses this information to make the connection to the origin. The incoming request is still addressed to www.example.com. If the origin has an SSL certificate for www.example.com, CloudFront will accept it as valid, because you configured the Host header for whitelisting, and it matches the cert.

    In this configuration, accessing the CloudFront distribution with the assigned dzczcexample.cloudfront.net hostname in the browser's address bar will not work, because CloudFront will send that hostname to the origin, but once you point the Route 53 alias for www.example.com to the assigned cloudfront.net domain name, requests will be processed correctly.