6

I am running static hosting on s3 for my website. Eg. www.somedomain.com points to an S3 bucket.

I have a subdomain api.somedomain.com for my api, but it has been annoying to deal with cross domain issues. I want to map www.somedomain.com/api/... -> api.somedomain.com/... but without doing a full redirect (301) since I want to be able to post.

I understand that Cloudfront allows this behavior, but it's a bit overkill since I do not need the CDN.

I have gotten the routing rules to work with a 301 redirect, but is there anyway to configure s3 to pass through my requests to ec2? Thanks!

4

1 回答 1

3

要解决这个问题,您需要做的就是改变范式。在您的 EC2 实例上,只需与您的 Web 应用程序分开运行像 varnish 或 nginx 这样的反向代理,将http://www.somedomain.com/api/ * 的流量路由到 Web 应用程序(您甚至可以重写请求 url 以删除“/api”前缀)和所有其他到 S3 的流量。配置 nginx 或 varnish 来做到这一点非常简单(几小时,而不是几天)。

然后将您的 www.somedomain.com DNS 记录切换为指向您的 ec2 实例而不是 S3。

用于清漆的示例非棉绒 VCL 执行此操作:

backend s3 {
    .host = "s3.amazonaws.com";
    .port = "80";
}

backend app {
    .host = "localhost";
    .port = "8080";
}

sub vcl_recv {
    if (req.url ~ "^/api/.*") {
        set req.backend = app;
        set req.url = regsub(req.url, "^/api", "");
        set req.http.Host = "api.somedomain.com"; /* If your web app cares about host */
    } 
    else {
        set req.backend = s3;
    }
}

在此之后,您可以随心所欲,例如使用路由 53 对您的 ec2 实例进行健康检查,并在 s3 存储桶关闭时将 DNS 查找故障转移到您的 s3 存储桶,以及配置自定义缓存规则等。但没有必要达到你想要的行为。

于 2013-07-20T21:30:56.373 回答