Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Cache-Control Header in amazon cloudfront?

I have a amazon cloudfront distribution that creates a 'origin' to my CDN. In my behavior, I set the Max TTL, Min TTL and default TTL. But my response header does't return the Cache-control header in my static files that are redirected. How to set the cache-control? I need this for the google page insights

Files to create cache-control

My header responses

like image 609
Igor Corradi Avatar asked May 17 '19 13:05

Igor Corradi


People also ask

How do I add a Cache-Control header?

If you want to enable Cache-Control for all files, add Header set line outside the filesMatch block. As you can see, we set the Cache-Control header's max-age to 3600 seconds and to public for the listed files.

How do I set cache policy in CloudFront?

Open the CloudFront console at https://console.aws.amazon.com/cloudfront/v3/home . Choose Create distribution. In the Cache key and origin requests section, make sure that Cache policy and origin request policy is chosen. For Cache policy, choose the cache policy to attach to this distribution's default cache behavior.

What is Cache-Control HTTP header?

Cache-control is an HTTP header used to specify browser caching policies in both client requests and server responses. Policies include how a resource is cached, where it's cached and its maximum age before expiring (i.e., time to live).

What should you add to a Cache-Control response header to specify that a response?

private. The private response directive indicates that the response can be stored only in a private cache (e.g. local caches in browsers). You should add the private directive for user-personalized content, especially for responses received after login and for sessions managed via cookies.


2 Answers

You can add a Cache-Control header to your CloudFront instance without the use of functions.

  • Go to AWS Console and navigate to the CloudFront instance
  • Go to Policies -> Response Headers and click on "Create response header policy" under custom policies. If you have an existing policy edit it.
  • In the next screen under Custom headers add, Cache-Control header along with the max age value you want. This value can be set to override origin if you want it.
  • Now go to your Distribution and navigate to the Behaviours tab.
  • In your default behaviour, under the Response headers policy select the one you just created.

enter image description here

All your responses will now include the cache headers you have set.

like image 52
Kolappan N Avatar answered Sep 21 '22 14:09

Kolappan N


You can : 1. configure origin to add the Cache-Control header in response. Or 2. use Lambda@edge (Viewer response as you just want to use it for google page insights) to add Cache-control header in the response. Example: https://github.com/jkrnak/serverless-lambda-at-edge/blob/master/handler.js

Defining TTL doesn't add any cache-control header, it uses to consider how long it need to cache.

like image 28
James Dean Avatar answered Sep 19 '22 14:09

James Dean