Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve static/media contents (only) from s3 and others from EC2?

I want to serve my static/media contents ( images for now) from S3 but my url should be as it is now. For example:

http://example.com/resources/img/me.jpg is one of my current image url which is now being served from the native server (EBS storage came with EC2). Now I want to store this me.jpg file in a bucket of s3 and then if someone types the above url it is brought from there.

I know this is possible by creating a bucket name as example.com and www.example.com and then configuring Route 53 CNAME (Alias) for s3-website-region.amazonaws.com

But what will happen when I will type http://example.com/blog/cool-stuff-blog ? This not intended for a static/media content! It needs to execute server side code (Python/PHP).

But with the above configuration of my current working domain what's gonna happen? Won't it try to supply some content from S3 and fail?

How can I get it done? Please don't say have a subdomain like static.example.com and serve all static file requests to that. For me that's unacceptable now. I want to use the exact same domain name.

like image 369
edam Avatar asked Sep 02 '15 18:09

edam


2 Answers

Create a CloudFront distribution.

Configure 2 "origin" servers, one being your main server, the other as your S3 bucket.

Configure the default * path pattern to route to your main server, and configure /resources/* to point to your bucket (or whatever paths you need, you can configure more than one).

CloudFront matches each request against the path patterns, and will forward each request to the appropriate back-end, based on the first matching pattern.

If you don't want to use CloudFront's caching capability, you can disable it by configuring the option to "forward all request headers to the origin."

Add your domain name to the list of alternate domain names associated your distribution.

Select the CloudFront pricing tier that is appropriate for your workload and budget -- you can potentially keep costs a little lower by disabling more expensive CloudFront edge locations if that isn't where your audience is.

After testing, point your domain name to the CloudFront distribution, with an Alias A-Record in Route 53.

You can also add additional "origins" if you want to serve content from different paths out of multiple buckets or back-end server clusters, through a single hostname.

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html

like image 63
Michael - sqlbot Avatar answered Oct 07 '22 01:10

Michael - sqlbot


Personally, I think updating the urls and going with a subdomain is the better approach (just for the record), but you should be able to use rewrite rules in your web-server config to accomplish what you want.

http://www.bennadel.com/blog/2172-redirecting-static-requests-to-amazon-s3-using-iis-mod-rewrite-or-apache-mod-rewrite.htm

like image 22
E.J. Brennan Avatar answered Oct 07 '22 01:10

E.J. Brennan