Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CloudFront Caching based on subdomain


I have CloudFront setup which is setup to serve various subdomains under my domain.
ex. http://demo1.mydomain.com/test.html, and
http://demo2.mydomain.com/index.html
can be two requests which are served by the CloudFront.
Now the issue here is with CloudFront caching. It caches the content based on path ie. in the above examples ("/test.html" and "/index.html").
This creates a problem that if two subdomains having same path, content which will get cached for a given path in one subdomain and will also be served from cache (same path) in other subdomain. ex.
http://demo1.mydomain.com/example.html
http://demo2.mydomain.com/example.html
The second request here will serve the cached content of first one.


Can I configure CloudFront to include subdomain when caching? This way I can avoid same path conflicts across subdomain.

Thanks

like image 699
Jakao Avatar asked Nov 20 '25 21:11

Jakao


1 Answers

I had the same challenge, and solved it using headers.

In details: We are indicating our tenants by subdomain id:

<id>.domain.com

We wanted to store a different cached value for each tenant. For example:

123.domain.com/get-config and 456.domain.com/get-config need to return different cached values.

As a solution, since cloudfront doesn't supply an indication based on sub-domains, we based on headers.

In your case, you can pass a header named appName and give it values: demo1, demo2 etc. Cloudfront will host different cache values based on that header.

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/header-caching.html#header-caching-web-selecting

like image 59
Yedidya Schwartz Avatar answered Nov 24 '25 03:11

Yedidya Schwartz