Search code examples
apache.htaccessamazon-s3cachingamazon-cloudfront

set cache ttl for cloudfront images with query string


I am using S3 with cloudfront to deliver the static content. The only issue is with images cache TTL when I use query string in my images.

For example

https://cloudfronturl/static_images/banners/web/Slider-1.jpeg?format=auto

For this I am getting issue of

Serve static assets with an efficient cache policy

I have already setup cache policy in my cloudfront and it is working if I request without query string

for example

https://cloudfronturl/static_images/banners/web/Slider-1.jpeg

How can I fix the cache issue. Query string is required because I want images in different format and size for different place


Solution

  • There are few options in Cloudfront for whitelisting or allowing/blocking queryStrings to optimize cache.

    • Don't forward query strings to the origin at all. If you don't forward query strings, CloudFront doesn't cache based on the query string parameters.

    • Forward query strings to the origin, and cache based on all
      parameters in the query string.

    • Forward query strings to the origin, and cache based on specified
      parameters in the query string

    https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/QueryStringParameters.html

    If the above doesn't work you can try adding a new custom header to cache policy with cache-contorl header https://i.imgur.com/rvzgDkh.png