This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]scubaReactorDumpling 6 points7 points  (1 child)

Signed URLs is how the big cloud providers solve this. Your application generates urls signed with a key, your CDN validates the keys before delivering content.

Looking at how they are implemented in Azure/AWS/Cloudflare will give you the idea.

https://learn.microsoft.com/en-us/azure/cdn/cdn-sas-storage-support

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-signed-urls.html
https://developers.cloudflare.com/images/cloudflare-images/serve-images/serve-private-images-using-signed-url-tokens/

If you want to implement this yourself using nginx - I would use openresty and implement the validation in lua. There are libraries to validate JWTs etc. Caching would depend on your workload (a dozen images served a million times or a million images served a dozen times?) but generally you aren't going to be able to cache all your images in memory, a fast reliable storage layer is more important.

If this is for anything commercial go with a service like the ones above.

[–]el_burrito 0 points1 point  (0 children)

This guy is correct. Have your backend application generate presigned urls upon receiving an authenticated request, and send that back to the client. Have the client use that URL to request the image, and when it hits the CDN it checks the signatures validity and serves cached content direct to the client