|« PreviousNext »|
|Did this page help you? Yes | No | Tell us about it...|
Amazon CloudFront can serve both compressed and uncompressed files from an origin server. CloudFront relies on the origin server either to compress the files or to have compressed and uncompressed versions of files available; CloudFront does not perform the compression on behalf of the origin server. With some qualifications, CloudFront can also serve compressed content from Amazon S3. For more information, see Choosing the File Types to Compress.
CloudFront can only serve compressed data if the viewer (for example, a web browser or media player) requests
compressed content by including
Accept-Encoding: gzip in the request header. The content must be compressed
using gzip; other compression algorithms are not supported. If the request header includes additional content
encodings, for example,
sdch, CloudFront removes them before forwarding the request
to the origin server. If
gzip is missing from the
CloudFront serves only the uncompressed version of the file. For more information about the
field, see "Section 14.3 Accept Encoding" in Hypertext Transfer Protocol -- HTTP/1.1 at
For more information, see the following topics:
Here's how CloudFront commonly serves compressed content from a custom origin to a web application:
You configure your web server to compress selected file types. For more information, see Choosing the File Types to Compress.
You create a CloudFront distribution.
You program your web application to access files using CloudFront URLs.
A user accesses your application in a web browser.
CloudFront directs web requests to the edge location that has the lowest latency for the user, which may or may not be the geographically closest edge location.
At the edge location, CloudFront checks the cache for the object referenced in each request.
If the browser included
Accept-Encoding: gzip in the request header, CloudFront
checks for a compressed version of the file. If not, CloudFront checks for an uncompressed version.
If the file is in the cache, CloudFront returns the file to the web browser. If the file is not in the cache:
CloudFront forwards the request to the origin server.
If the request is for a type of file that you want to serve compressed (see Step 1), the web server compresses the file.
The web server returns the file (compressed or uncompressed, as applicable) to CloudFront.
CloudFront adds the file to the cache and serves the file to the user's browser.
If you want to serve compressed files from Amazon S3:
Create two versions of each file, one compressed and one uncompressed. To ensure that the compressed and uncompressed versions of a file don't overwrite one another in the CloudFront cache, give each file a unique name, for example, welcome.js and welcome.js.gz.
Open the Amazon S3 console at https://console.aws.amazon.com/s3/.
Upload both versions to Amazon S3.
If you're using the Amazon S3 API to upload files, include a
Content-Type header to
specify the MIME type of each file. The default MIME type is
If you're using the Amazon S3 console to upload files, Amazon S3 automatically figures out the content types of the files by default.
Content-Encoding header field for each
compressed file and set the field value to
For an example of how to add a
Content-Encoding header field 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 this field.
To add a
Content-Encoding header field and set the field value using the Amazon S3 console, perform the following
In the Amazon S3 console, in the Buckets pane, click the name of the bucket that contains the compressed files.
Click the name of a file for which you want to add a
At the top of the page, click Actions. In the Actions list, click Properties.
In the right pane, click Metadata.
Under Metadata, click Add More Metadata.
In the Key list, click Content-Encoding.
In the Value field, enter gzip.
Repeat steps 4b through 4h for the remaining compressed files.
When generating HTML that links to content in CloudFront (for example, using php, asp, or jsp),
evaluate whether the request from the viewer includes
in the request header. If so, rewrite the corresponding link to point to the compressed object name.