How can my s3 bucket host multiple websites in different folders using nginx?

I have an s3+Cloudfront solution on Amazon. I would like to host different websites in different folders inside the bucket and access them in one of this two way: - a subdomain -> mywebsite1.mydomain.com or - path -> www.mydomain.com/mywebsite1

I read that a proxy based on nginx could solve my problems. Is it true? Is it possible to get nginx on s3?

Answers 1

  • You can do this with Nginx (or HAProxy) running in EC2 and acting as a reverse proxy in front of the buckets, yes, but if you are not already familiar with how to configure it, it may be simpler to just use CloudFront... a second time.

    The solution here is to create a separate distribution each web site subdomain.

    Assuming the bucket is named example.com and it is in the us-west-2 region, verify that you already enabled web site hosting on the bucket and then find the web site endpoint for the bucket, which in this case would be example.com.s3-website-us-west-2.amazomaws.com.

    For subdomain1.example.com, the content would be under, let's say, subdomain1/, in the example.com bucket. So, you create a new CloudFront distribution for this subdomain, configuring the CloudFront distro with subdomain1.example.com as the alternate domain name. For the origin server, use the bucket website endpoint hostname, mentioned above (no path -- just the hostname). Then, configure the Origin Path to be /subdomain1. Note there is a leading slash but not a trailing slash.

    Now, when this, distribution sees a request for (e.g.) /images/cat.jpg it will send it to S3... but before it does that, it will prepend the Origin Path onto the request, and ask the bucket for /subdomain1/images/cat.jpg.

    Point the DNS for subdomain1 to the new CloudFront distribution, and you should have what you need -- a subdomain whose content lives under a path in a bucket.

    Repeat for each subdomain. This step is easily automated with one of the SDKs or the CLI.

    You may eventually need to request an increase in the number of CloudFront distributions your account is allowed to create, since the default is 200 but this is a simple process as long as you are able to describe your use case.

    Of course, it occurs to me that your reason for wanting to do this might be related to the 100 bucket limit per account, but that limit is no longer a fixed limit. AWS will now allow you to request that this limit be increased as well.


Related Articles