| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
You can control how long your objects stay in a CloudFront cache before CloudFront forwards another request to your origin. Reducing the duration allows you to serve dynamic content. Increasing the duration means your customers get better performance because your objects are more likely to be served directly from the edge cache. A longer duration also reduces the load on your origin.
Typically, CloudFront serves an object from an edge location until the object expires. After it expires, the next time the edge location gets an end-user request for the object, CloudFront forwards the request to the origin server to verify that the cache contains the latest version of the object:
If CloudFront already has the latest version, the origin returns only a 304 status code (not modified).
If CloudFront does not have the latest version, the origin returns a 200 status code (OK) and the latest version of the object.
If an object in an edge location isn't frequently requested, CloudFront might evict the object—remove the object before its expiration date—to make room for objects that are more popular.
By default, each object automatically expires after 24 hours. To specify a different expiration time, configure your origin to add
a value for either the Cache-Control max-age directive or the Expires header field to each object:
The Cache-Control max-age directive lets you specify how long (in seconds) you want the object
to remain in the cache before CloudFront gets the object again from the origin server. The minimum expiration time CloudFront supports is
0 seconds and the maximum is in the year 2038.
The Expires header field lets you specify an expiration date and time using the format specified in
RFC 2616, Hypertext Transfer Protocol -- HTTP/1.1
Section 3.3.1, Full Date, for example:
Sat, 30 Jun 2012 23:59:59 GMT
Important
After the expiration date and time passes, CloudFront gets the object again from the origin server every time an edge location receives a request for the object.
We recommend that you use the Cache-Control max-age directive instead of the Expires header field
to control object caching. If you specify values both for Cache-Control max-age and for Expires,
CloudFront uses only the value of max-age.
You cannot use the HTTP Cache-Control or Pragma header fields in a
GET request from an end user to force CloudFront to go back to the origin server for the object.
CloudFront ignores those header fields from the end user.
For more information about the Cache-Control and Expires header fields, see the following sections in
RFC 2616, Hypertext Transfer Protocol -- HTTP/1.1:
For an example of how to add Cache-Control and Expires header fields using the AWS SDK for PHP, see
Upload an Object Using the AWS SDK for PHP in the
Amazon Simple Storage Service Developer Guide. Some third-party tools are also able to add these fields.
For download distributions, if you are adding Cache-Control or Expires headers to your objects,
you can also specify the minimum amount of time that CloudFront keeps an object in the cache before forwarding another request to
the origin. Here's how object headers and a minimum TTL control how long objects remain in the CloudFront cache:
| Minimum TTL = 0 (Default) | Minimum TTL > 0 | |
|---|---|---|
Origin adds | Objects are cached for the value of the | Objects are cached for the greater of the value of the |
Origin does not add | Objects are cached for 24 hours. | Objects are cached for 24 hours. |
Origin adds | Objects are cached until the date in the | Objects are cached until the date in the |
Origin adds | CloudFront respects the headers | Objects are cached for the Minimum TTL |
For information about how to change settings for download distributions using the CloudFront console, see Listing, Viewing, and Updating CloudFront Distributions. For information about how to change settings for download distributions using the CloudFront API, see PUT Config.
To add a Cache-Control or Expires header field to Amazon S3 objects using the Amazon S3 console
Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/.
In the Amazon S3 console, in the Buckets pane, click the name of the bucket that contains the files.
In the Objects and Folders pane, select the first object to which you want to add a header field.
At the top of the Objects and Folders pane, click Actions and click Properties.
In the Properties pane, click the Metadata tab.
On the Metadata tab, click Add More Metadata.
In the Key list, click Cache-Control or Expires, as applicable.
In the Value field, enter the applicable value:
For a Cache-Control field, enter:
max-age=number of seconds that you want objects to stay in a CloudFront edge cache
For an Expires field, enter a date and time in HTML format.
Click Save.
If you want to add a header field to additional objects, in the Objects and Folders pane, click the name of the next object, and repeat Steps 6 through 9.