简体   繁体   English

在Amazon S3的BUCKET中托管多个网站

[英]Hosting multiple websites in a BUCKET on Amazon S3


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). 我正在开发一个应用程序,用户可以创建自己的html模板并将其发布到网络上。现在,当用户单击发布时,我想在他选择的名称的子域上创建其网站托管(例如:他为该网站命名)苹果,我创建了一个子域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. 在应用程序中,一个用户可以创建多个网站/模板。现在,我想将一个用户网站存储在一个存储桶中。如果一个用户有两个模板, 例如apple.com和berry.com ,则我在其中有两个文件夹存储桶,每个网站一个。但是我浏览了S3存储桶,发现可以在存储桶和网站上设置托管规则。
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. 我知道我将必须使用AWS服务和API将模板存储到S3,如果我可以在BUCKET中拥有多个网站,我很感兴趣。

EDIT:Figured out a solution using proxy server nginx and updated the answer 编辑:找出使用代理服务器Nginx的解决方案并更新答案

To use the static website feature of s3, you can only map one domain per bucket. 要使用s3的静态网站功能,每个存储桶只能映射一个域。 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: 这有点棘手但可行。经过研究后,我找到了一个可行的解决方案,步骤如下:

  1. We need a proxy server in order to perform the operations.( eg:Nginx ) 我们需要代理服务器才能执行操作。( 例如:Nginx
  2. We need to make config changes to the default.conf file to proxy requests to the bucket you have your website. 我们需要对default.conf文件进行配置更改,以将请求代理到您拥有网站的存储桶中。
  3. This is the conf file: 这是conf文件:

     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 # Eg 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; } } 
  4. 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. 现在在您的DNS设置中,将CNAME更改为代理服务器地址(类似于router.abcd.com )。代理服务器将接受您的请求,并将其转发到托管您的站点的S3存储桶。

  5. 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. 另外,您可以使用wwwizer.com的 IP地址进行@记录,这将使您的请求发送到正确的目的地,而不管URL中的www

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM