Skip to content

Caching Conflict with AWS CloudFront for Multiple Subdomains

0

I have deployed multiple sites on AWS EC2 using different subdomains, and I'm using a single CloudFront distribution to serve content for all of these sites. However, I am encountering a caching conflict issue: For example, the following two images have the same path but are located on different subdomains:

https://site1.example.com/wp-content/uploads/2024/05/cta_bg.jpeg
https://site2.example.com/wp-content/uploads/2024/05/cta_bg.jpeg

Both URLs reference different images, yet they return the same cached image due to the identical path. How can I resolve this caching conflict and ensure that each image is served correctly based on its subdomain? Any guidance would be greatly appreciated!

asked 2 years ago602 views

2 Answers
3
Accepted Answer

Hello,

To resolve the caching conflict in your CloudFront distribution while using multiple subdomains with the same URL paths, you can take the following approachs by using Cache Key Customization :

You can customize the cache key in CloudFront to include the subdomain as part of the cache key. This way, CloudFront will cache assets separately for each subdomain.

  • Steps to Customize Cache Key: 1- Open the CloudFront Console: Go to the AWS Management Console and open the CloudFront service.

    2- Select Your Distribution: Click on the distribution you want to modify.

    3- Edit Cache Behavior: In the "Behaviors" tab, select the cache behavior you want to modify, then click "Edit".

    4- Configure Cache Key Settings: Cache key and origin requests: Choose "Use a Cache Policy and Origin Request Policy". Cache Policy: Either create a new cache policy or modify an existing one. In the cache policy settings, ensure that you include the "Headers" option that specifies the Host header. This will differentiate cache entries based on the subdomain.

    5- Save Changes: After making the changes, save your settings.

By following the above steps, you can effectively customize the cache key in CloudFront to differentiate between resources served from different subdomains. This approach helps maintain the integrity of your content and ensures that each subdomain serves the correct resources without conflicts.

I hope this help !

Regards,

AWS

answered 2 years ago

EXPERT

reviewed 2 years ago

EXPERT

reviewed 2 years ago

0

I think you will need a separate CF distribution for each subdomain with its own custom domain and an origin pointing the corresponding EC2 instance.

AWS
EXPERT

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.