简体   繁体   English

使用AWS S3和Route 53托管静态网站

[英]Hosting static website with AWS S3 and Route 53

Let's say I currently have website www.a.com (entirely static) on a dedicated server and I want to switch to AWS. 假设我目前在专用服务器上拥有网站www.a.com(完全静态),并且我想切换到AWS。

So far I did the following: Create bucket www.a.com, give proper permission to make it web (visible for public) and uploaded all files. 到目前为止,我已经执行了以下操作:创建存储桶www.a.com,获得适当的权限以使其成为网络(公共可见)并上传所有文件。 Obtain http://www.a.com.s3-website-eu-west-1.amazonaws.com . 获取http://www.a.com.s3-website-eu-west-1.amazonaws.com

I created Hosted Zone in AWS Route 53 and automatically got two Record Sets, NS and SOA. 我在AWS Route 53中创建了托管区域,并自动获得了两个记录集,即NS和SOA。 Next, I added a CNAME for *.a.com. 接下来,我为* .a.com添加了一个CNAME。 with value http://www.a.com.s3-website-eu-west-1.amazonaws.com 其值http://www.a.com.s3-website-eu-west-1.amazonaws.com

Question is now, what do I add for an A record? 现在的问题是,我要为A记录添加什么? I don't have an IP. 我没有IP。 (As a cause of not having an A record, www.a.com works, but a.com does not work). (由于没有A记录,www.a.com有效,但a.com不起作用)。

Can I use my dedicated server somehow to keep serving email? 我可以使用我的专用服务器以某种方式继续提供电子邮件吗? How should I setup MX given I have a WHM/cPanel server? 如果我有WHM / cPanel服务器,应如何设置MX?

Thanks. 谢谢。

First, I would recommend adding an additional bucket called a.com and working off of that, don't delete www.a.com though, you'll later add a CNAME to that so www.a.com redirects to a.com in the browser. 首先,我建议添加一个名为a.com的其他存储桶,然后进行处理,尽管不要删除www.a.com,您以后将添加一个CNAME以便www.a.com重定向到a.com在浏览器中。

Now that you've created the a.com bucket, go to Properties > Static Website Hosting > Enable Website Hosting > enter index.html and upload a file with that name to your bucket. 现在,您已经创建了a.com存储桶,请转至“属性”>“静态网站托管”>“启用网站托管”>输入index.html并将具有该名称的文件上传到您的存储桶。

Then go Properties > Permissions > Edit Bucket Policy > Paste the code below: 然后转到属性>权限>编辑存储桶策略>粘贴以下代码:

{
  "Version":"2008-10-17",
  "Statement":[{
    "Sid":"PublicReadForGetBucketObjects",
        "Effect":"Allow",
      "Principal": {
            "AWS": "*"
         },
      "Action":["s3:GetObject"],
      "Resource":["arn:aws:s3:::example-bucket/*"
      ]
    }
  ]
}

Change {example-bucket} to {a.com} and save. 将{example-bucket}更改为{a.com}并保存。

Now go to Route53 > Select your domain > Go to record sets > create record set 现在转到Route53>选择域>转到记录集>创建记录集

Make sure 'Type' is set to 'A' and then select 'Yes' next to 'Alias'. 确保将“类型”设置为“ A”,然后在“别名”旁边选择“是”。 Click into the Alias Target area and under S3 websites you should now see a.com. 单击“别名目标”区域,在S3网站下,您现在应该看到a.com。 Select it and then at the bottom choose 'Create record set'. 选择它,然后在底部选择“创建记录集”。

You're done. 你完成了。

This is a commonly encountered restriction when hosting a static site on S3. 这是在S3上托管静态站点时经常遇到的限制。 You have a few options. 您有几种选择。

  • Determine the IP addresses used by your S3 bucket using dig , nslookup or similar tools. 使用dignslookup或类似工具确定S3存储桶使用的IP地址。 This will work, but it's likely to eventually break as Amazon shuffles around their IPs. 这行得通,但是随着亚马逊改组其IP,最终可能会破裂。
  • Use a DNS provider other than Route53 that provides a "naked redirect" service. 使用除Route53之外的提供“裸重定向”服务的DNS提供程序。 The DNS provider will do a HTTP 301 redirect to your "www" record when someone accesses your root. 当有人访问您的根目录时,DNS提供程序将执行HTTP 301重定向到您的“ www”记录。
  • Host a really simple website on your own server/EC2 instance that issues that same HTTP 301 redirect. 在您自己的服务器/ EC2实例上托管一个非常简单的网站,该网站发出相同的HTTP 301重定向。

Edit: As of Dec 27, 2012 S3 now supports a way to do this without all the above mentioned workarounds. 编辑:从2012年12月27日起,S3现在支持一种无需上述所有解决方法的方法。 Check out the AWS blog about root website hosting from S3 . 查看有关S3的根网站托管的AWS博客。

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

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