I am working on an application where user can create his own html templates and publish them to the web.Now when the user clicks publish I want to create host his website on a subdomain with a name he selects.(EX: he names the site apple,I create a subdomain apple.ABC.com).
In the application a single user can create multiple websites/templates.Now,I want to store a single users websites in a single bucket.If a user has two templates Ex: apple.com and berry.com ,I have two folders in the bucket,one for each website.But I went through the S3 bucket,and I found I can set hosting rules on the bucket and the website.
I wanted to understand if what I am trying is possible and if not how can I manage it as if I create one bucket for one template,it will be difficult for me to track which user has how many templates as in the DB I will have to have multiple entries.
I am aware I will have to use AWS services and API's to store the templates to S3,I am interested if I can have multiple websites in a BUCKET.
EDIT:Figured out a solution using proxy server nginx and updated the answer
To use the static website feature of s3, you can only map one domain per bucket. There is no way to tell a domain to use the folder of the bucket instead of the bucket itself.
This is a bit tricky but doable.After some research I found a solution that can work.Steps are as follows:
- We need a proxy server in order to perform the operations.(eg:Nginx)
- We need to make config changes to the default.conf file to proxy requests to the bucket you have your website.
This is the conf file:
server {
# Listen on port 80 for all IPs associated with your machine
listen 80;
# Catch all other server names
server_name _;
# This code gets the host without www. in front and places it inside
# the $host_without_www variable
# If someone requests www.coolsite.com, then $host_without_www will have the value coolsite.com
set $host_without_www $host;
if ($host ~* www\.(.*)) {
set $host_without_www $1;
}
location / {
# This code rewrites the original request, and adds the host without www in front
# E.g. if someone requests
# /directory/file.ext?param=value
# from the coolsite.com site the request is rewritten to
# /coolsite.com/directory/file.ext?param=value
set $foo 'http://sites.abcd.com';
# echo "$foo";
rewrite ^(.*)$ $foo/$host_without_www$1 break;
# The rewritten request is passed to S3
proxy_pass http://sites.abcd.com;
include /etc/nginx/proxy_params;
}
}
Now in your DNS settings change your CNAME to your proxy server address(Something like router.abcd.com
).The proxy server will take your request and forward it to the S3 bucket where your site is hosted.
- Also, you can use wwwizer.com ip address for
@
record.This will send your request to correct destination irrespective of the www
in your URL.