Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 241
Sam G Avatar asked Feb 04 '16 18:02

Sam G


People also ask

What is custom origin in CloudFront?

A custom origin is an HTTP server, for example, a web server. The HTTP server can be an Amazon EC2 instance or an HTTP server that you host somewhere else. An Amazon S3 origin configured as a website endpoint is also considered a custom origin.


2 Answers

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. :)

like image 103
Sam G Avatar answered Oct 12 '22 23:10

Sam G


Assuming your have your origin at origin.domain.com, create your CloudFront distribution, using http://origin.domain.com as the origin.

Create your www.domain.com DNS entry in Route 53, making it an "Alias" for the CloudFront distribution.

One possible alternative to .htaccess is to use WAF on your CloudFront distribution. This way, you can permit/deny based on IP address.

like image 38
Matt Houser Avatar answered Oct 13 '22 00:10

Matt Houser