mst
mst

Reputation: 466

is it possible to create serverless architecture for this goal?

i have multiple domains like

x.static.abc.com
y.static.abc.com
z.static.abc.com

i want to use Route53 & cloudfront & s3 to serve static contents.

when a request sent to x.static.abc.com/1.png i need to soft rewrite it to abc.com/static/x/1.png.

i dont want redirect request with 301 and 302 response. so i dont want to use Redirection Rules in S3 under Enable website hosting option.

is there any way to do it ? I need your solutions or advices. Thanks all.

edit: i use Route53 & cloudfront & s3, more than 4 years. this question is not about basics like "how can i define DNS settings in rout53".

i created bucket in S3 named with x.static.abc.com, enabled website hosting settings in properties, i added endpoint to Route53 and added Routing Rules to redirection rule like:

<RoutingRules>
    <RoutingRule>
        <Condition>
            <KeyPrefixEquals>..</KeyPrefixEquals>
        </Condition>
        <Redirect>
            <ReplaceKeyWith>...</ReplaceKeyWith>
        </Redirect>
    </RoutingRule>
</RoutingRules>

but this is not what i am looking for. Because it redirects requests with 301 or 302.

i am looking for an option like cloudflare's :

Resolve Override (Enterprise Only): Changes the origin address for the request to the URL. Users will see the domain name in the browser address bar, but content will be served from the URL in the resolve field. https://support.cloudflare.com/hc/en-us/articles/200168306-Is-there-a-tutorial-for-Page-Rules-

Upvotes: 0

Views: 124

Answers (2)

Michael - sqlbot
Michael - sqlbot

Reputation: 179054

Doing this with HAProxy on EC2 would be trivial, but if we are constrained to only using AWS-managed offerings, here is the closest you can get (and it seems pretty close):

Create a CloudFront distribution with the following configuration:

  • alternate domain name for the distribution set to x.static.example.com

  • origin server example.com with origin path (on the same screen as the origin server, not to be confused with the path pattern in the cache behavior) set to /static/x

  • default cache behavior forwards to the origin server but is configured not to whitelist the Host: header

  • map x.static.example.com in Route 53 to the new CloudFront distribution.

The net effect of this configuration is that the incoming host header, once recognized and used to map the request to this distribution, is discarded, and the origin host's hostname will be sent as the host header.

Original request coming into CloudFront:

GET /images/lolcat.jpg HTTP/1.1
Host: x.static.example.com

Request sent from CloudFront to the origin server:

GET /static/x/images/lolcat.jpg HTTP/1.1
Host: example.com

Seems legit.

You would need one of these for each hostname, because there is no provision for dynamic mapping, but CloudFront doesn't charge for the existence of a distribution -- only the traffic -- so, no harm, there. I think the above makes for a pretty straightforward solution.

Upvotes: 0

TomTom
TomTom

Reputation: 62093

Yes, it is possible. But the approach has a significant disadvantage - you should not use static.abc.com but preferable a separate domain. Why? Cookies ;)

Upvotes: 1

Related Questions