I have been checking things up & down on Google & AWS Documentation but can't seem to nail this one.
I have an EC2 instance setup with a domain being served via a virtual host and I need to use a CDN for the contents on the site, mainly images ie website assets.
EC2 Details of the instance
- t2.micro
- LAMPP
- Virtual Host Set Up
- DNS: Route 53
- No ELB
- EBS
- SSL
Cloudfront Distribution Details
- Origin: example.com
- Delivery Method: Web
What I tried to do is use the domain name in the origin and the Cloudfront domain was now redirecting to the origin domain name along with fetching assets by https.
I haven't found any impact on the site speed, pinged from different geo locations thus I have come to doubt whether this was done correctly.
The part which I need to resolve is which of the below path is correct for setting up Cloudfront with EC2:
- Use EC2 instance DNS for 'Origin Domain Name' and add the directory name in the 'Origin Path' for the virtual host
- Only add the domain name in the 'Origin Domain Name'
Thanks for the help!
Edit
Attaching Route 53 Details
- A Record => Name: www.example.com, Value: IP ADDRESS & Routing Policy: Simple
- A Record => Name: example.com, Alias: Yes & Alias Target: www.example.com
Edit02
Updated AWS Setup
AWS Setup between EC2, Route 53 & CloudFront
Best Answer
In DNS, example.com needs to be an A record with Alias set to Yes, pointing to the CloudFront endpoint.
This means example.com no longer points to your origin server, so you have to use a different name as the origin host. One option is to use the EC2 public DNS name for the instance.
By default, CloudFront will set the origin domain name in the
Host:
header when sending the request to the instance. You can override this in Cache Behavior by whitelisting theHost
header, so that example.com is sent inside the request to the origin, even though the DNS entry used to actually find the origin differs.If
$ dig example.com
doesn't return a large number of IP addresses, then you aren't currently actually using CloudFront.HTTP responses that come back from CloudFront will also contain extra headers, including
X-Amz-Cf-id
,X-Cache
,Via
, and sometimesAge
.