5

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.

edam
  • 910
  • 10
  • 29

2 Answers2

4

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

Michael - sqlbot
  • 169,571
  • 25
  • 353
  • 427
  • Thanks for the answer. But there are advanced redirect rules in the bucket configuration set too. Can't I use it to suffice what (only for redirection) CloudFront is doing? – edam Sep 03 '15 at 03:53
  • No, not if I understand your question correctly. Redirect rules in S3 will send the browser to a resource at a different hostname -- it won't pass-through proxy the requests somewhere else. For a given hostname, all requests go to one place. – Michael - sqlbot Sep 03 '15 at 09:57
  • ... If the specific resource needed is elsewhere, that "one place" has to either fetch it on behalf of the browser (Cloudfront, or another reverse proxy, including config in Apache, Nginx, or HAProxy) or redirect the browser it. If the page requested by the browser (in the address bar) is redirected, the address bar hostname will change, when the browser re-fetches in response to the redirect. – Michael - sqlbot Sep 03 '15 at 09:58
  • Okay, I am not gonna use CloudFront, at least not now. SO what solution remains for me? Can I register a subdomain `static.example.com` and configure it against the **bucket name:** `static.example.com` as a static website? It seems the easiest way for now right? I don't even have to configure a virtualhost in this way (or do I have to?) . – edam Sep 03 '15 at 10:48
  • If you want static content from S3 and you're going to link to it, that will work, unless your site uses https, because vanity/virtual domain + SSL on S3 requires the SSL component be provided by (guess who?) CloudFront. – Michael - sqlbot Sep 03 '15 at 11:44
  • Or, if your server is Apache, you can always do some variant of `RewriteCond %{REQUEST_URI}` followed by `RewriteRule ^/resources/(.*[png|gif|jpe?g])$ http://example-bucket.s3.amazonaws.com/resources/$1 [P]` ... this would cause your apache to fetch `/resources/*` from S3 on behalf of the browser (like CloudFront does) for files with the extensions `png` `gif` `jpg` `jpeg`. – Michael - sqlbot Sep 03 '15 at 12:10
2

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

E.J. Brennan
  • 45,870
  • 7
  • 88
  • 116
  • If I configure an S3 bucket example.com (which is my original domain) to serve static content from it then any request is made should go there first, right? If any redirection has to be written, to be written in the bucket redirect rules, right? Why are you saying I should configure my Apache server when it won't even receive the request at first hand? – edam Sep 03 '15 at 03:58