How to correctly set up Amazon Route 53, CloudFront with custom origin

Sam G picture Sam G · Feb 4, 2016 · Viewed 8k times · Source

I am currently using Amazon Route 53 for my DNS setup and a custom origin (web server outside of Amazon). I'd like to add CloudFront to the mix but I'm not too sure how to set it up?

I've seen Amazon's guide on setting up S3 with Route 53 and CloudFront, but I can't seem to locate any information on doing all this with a custom origin.

Should I be setting it up to work as follows?

  1. CloudFront pulls the data from the naked domain's IP address
  2. The 'www' is just an alias of the CloudFront distribution

I originally planned to host my content on S3 but decided against it because I'd like more control over the hosting (for example, having the ability to use an .htaccess file and ban hacker IPs, etc).

Thanks.

Answer

Sam G picture Sam G · Feb 5, 2016

OK so this is how I managed to solve it in the end:

Origin Server:

  • In cPanel (on my origin server) I created a sub-domain called origin.domain.com
  • origin.domain.com shares the same document root as domain.com
  • My .htaccess file redirects domain.com to www.domain.com:

Amazon CloudFront:

  • The "Alternate Domain Names (CNAMEs)" is set to www.domain.com

  • The origin is set to origin.domain.com

Amazon Route 53

  • The A record for domain.com is set to my origin server's IP address

  • The A record for www.domain.com is set to an alias for the CloudFront distribution

  • origin.domain.com is a CNAME for domain.com

Thanks to Matt Houser and Bruce P for their help, by the way. :)