Forward a request coming to my CDN to another server

378 Views Asked by At

I'm trying to play Instagram Video assets. The challenge is the videos are expirable. They expire every N mins.

I'm brainstorming a solution where I set up my CDN (Cloudfront/Cloudflare) which forwards the incoming requests to the original server (Instagram in this case), caches the video at CDN, and then keeps serving it without the need to request Instagram again. I don't want to download the videos and keep them in my bucket. Is it even possible?

====Update

After @Ross Suggestion, I'd a look at CloudFront functions and was able to redirect the incoming requests to another URL, basis on some condition. Following is the code.

function handler(event) {
    var request = event.request;
    var headers = request.headers;
    
    if request.uri == '/assets/1.jpg'{
        var newurl = 'https://instagram.com/media/1.jpg'
      
        var response = {
            statusCode: 302,
            statusDescription: 'Found',
            headers:
                { "location": { "value": newurl } }
        }

        return response;
     }
   return request
}

However, this redirects it to the newURL. What I'm looking for is not redirect, but the following

  1. when the request is made to my server cdn, ie mydomain.com/assets/1.jpg, the file 1.jpg should be served from the instagram server, whose value is the newURL in the above code snippet. This should be done without changing my domain URL (in the address bar) to instagram.

  2. The following requests to mydomain.com/assets/1.jpg should be directly served from the cache, and should not be routed again to instagram.

1

There are 1 best solutions below

3
Stefan Norberg On

Why are you not using mydomain.com as the origin for all requests in the CDN config? There will be a minimal amount of requests from the edge CDN nodes to the origin if you set the cache headers appropriately.

Use your server to fetch the video from IG unless it's not already fetched.

You can set up the origin as a reverse proxy (using Squid for example) and have it manage the cache size if you don't want to save videos in S3. Or set up an S3 policy that clears out files after X number of days.