Redirect S3 subfolder to another domain with Cloudfront

后端 未结 2 850
猫巷女王i
猫巷女王i 2021-01-07 05:19

I have a static showcase website hosted on S3 and using CloudFront, and an online shop (Prestashop) and a blog (Wordpress), both hosted on OVH servers.

I want to mak

相关标签:
2条回答
  • 2021-01-07 05:26

    I'll check about proxies since it's my last hope

    Wait.

    I have a static showcase website hosted on S3 and using CloudFront

    CloudFront is a reverse proxy.

    Depending on how much flexibility you have with the other two sites, CloudFront can potentially take you where you want to go, combining multiple independent sites under one hostname.

    This is done by creating additional origin servers for your distributions and then creating additional cache behaviors, with path patterns matching the additonal paths, such as /blog and /blog/* that send requests to the alternate origins.

    There is, however, a catch. CloudFront can't remove the matched pattern, so mainsite.example.com/blog/hello-world, matching the pattern /blog/* will be forwarded to blog.example.com/blog/hello-world -- not to blog.example.com/hello-world.¹ This will require changes to the other sites in order to integrate them in this way.

    Unless...

    If you already have unique path patterns, no problem, but if the extra sites' content is in the root of each individual site, you see the issue, here. Not insurmoubtable, but still an issue.

    Your only alternative will be a reverse proxy behind CloudFront to rewrite those paths and send the requests on to the alternate servers. Truly not a big deal either, since HAProxy, Nginx, and Varnish all offer such functionality and can handle a large number of proxied requests on surprisingly small hardware.

    The recently (2017) released Lambda@Edge service allows you to rewrite paths on the fly, as requests are processed, if necessary.

    But the bottom line is that the reason you have not found a real solution other than a proxy is that there is no alternative -- every path at a given hostname must be handled in one logical place -- one group of one or more identically-configured endpoints. In the case of CloudFront, the logical place is physically distributed globally.


    ¹ CloudFront, natively, can actually prepend onto the path before forwarding the request, so requests for mainsite.example.com/bar/fizz can be forwarded to foosite.example.com/foo/bar/fizz by setting the origin path to /foo when you configure the origin. But it can't remove path parts or otherwise modify the path without also using Lambda@Edge. In the scenario discussed above, you would leave the origin path blank when configuring the additional origin servers.

    0 讨论(0)
  • 2021-01-07 05:40

    Single S3 bucket with the following behavior :

    domain.com-> serves the files from root of bucket

    domain.com/blog -> serves the files from subfolder in S3 bucket (this is not default behavior)

    How to :

    https://aws.amazon.com/ru/blogs/compute/implementing-default-directory-indexes-in-amazon-s3-backed-amazon-cloudfront-origins-using-lambdaedge/

    Lambda edge code:

    'use strict'; exports.handler = (event, context, callback) => {

    // Extract the request from the CloudFront event that is sent to Lambda@Edge 
    var request = event.Records[0].cf.request;
    
    // Extract the URI from the request
    var olduri = request.uri;
    
    // Match any '/' that occurs at the end of a URI. Replace it with a default index
    var newuri = olduri.replace(/\/$/, '\/index.html');
    
    // Log the URI as received by CloudFront and the new URI to be used to fetch from origin
    console.log("Old URI: " + olduri);
    console.log("New URI: " + newuri);
    
    // Replace the received URI with the URI that includes the index page
    request.uri = newuri;
    
    // Return to CloudFront
    return callback(null, request);
    

    };

    Summary of code higher :

    • lambda edge rewrites the path "/blog/" to "/blog/index.html"
    0 讨论(0)
提交回复
热议问题