Question

I have a website, e.g. mywebsite.com, I use Amazon S3 as storage for it. What I need to do is to make urls to my content, placed on S3, look like native website urls.

E.g.

http://s3-eu-west-1.amazonaws.com/img-bucket/flowers/flower.jpg

should looks like

http://www.mywebsite.com/flowers/flower.jpg

or at least like

http://www.mywebsite.com/img-bucket/flowers/flower.jpg

Can I achieve this in some way without using CloudFront and alias img-bucket to www.mywebsite.com?

Additional problem I see, is that I have thumbs for images, which stored in "folder" inside same bucket /img-bucket/thumbs/, but thumbs should be accessible by another urls, like

http://thumbs.mywebsite.com/flowers/flower.jpg

Is it possible to achieve this without too much changes? because now I have a feeling that at least I need to use CloudFront for original images, and split original images and their thumbs into 2 different buckets.

Was it helpful?

Solution

Through CNAME

Amazon recommends to achieve that, is through a CNAME in your domain to have something like http://images.mywebsite.com/flowers/flower.jpg.

To have this, you have to create a bucket called just like the host (images.mywebsite.com), and add a CNAME record in your domain that points to your bucket.

The CNAME record should be something like this:

images.mywebsite.com CNAME                 images.mywebsite.com.s3.amazonaws.com.

More information can be found on the official documentation of Amazon S3.

Proxied frontend web server

However, if the previous option still doesn't fit on what you need, you can go with the second option, which is tweaking the frontend web server to act as proxy when specifics url are being accessed.

If you are using Nginx, you can use HttpProxyModule to do this, and you have to add this configuration to your nginx.conf file:

location ~* ^/images/(.*) {
  set $s3_bucket        'your_bucket.s3.amazonaws.com';
  set $url_full         '$1';

  proxy_http_version     1.1;
  proxy_set_header       Host $s3_bucket;
  proxy_set_header       Authorization '';
  proxy_hide_header      x-amz-id-2;
  proxy_hide_header      x-amz-request-id;
  proxy_hide_header      Set-Cookie;
  proxy_ignore_headers   "Set-Cookie";
  proxy_buffering        off;
  proxy_intercept_errors on;

  resolver               172.16.0.23 valid=300s;
  resolver_timeout       10s;
  proxy_pass             http://$s3_bucket/$url_full;
}

With this, you can retrieve your S3 file using an url like: http://www.mywebsite.com/images/flowers/flower.jpg

The downside of this option, is that the webserver will retrieve the file by his own to deliver it to the user, causing some slowness.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top