简体   繁体   中英

How to enable gzip compression on AWS CloudFront

I m trying to gzip compress the img I m serving through CloudFront . My origin is S3

Based on several articles/blogs on aws, what I did is:

1) Set "Content-Length" header for the object I want to compress. I set the value equal to the size appeared on the size property box

2) Set the Compress Objects Automatically value to Yes in the Behaviour of my cloud distribution.

3) I invalidated my object to get a fresh copy from S3 .

Still I m not able to make CloudFront gzip my object. Any ideas?

I'm trying to gzip compress the [image]

You don't typically need to gzip images -- doing so saves very little bandwidth, if any, since virtually all image formats used on the web are already compressed.

Also, CloudFront doesn't support it.

See File Types that CloudFront Compresses for the suported file formats. They are text-based formats, which tend to benefit substantially from gzip compression.

If you really want the files served gzipped, you can store the files in S3, already gzipped.

$ gzip -9 myfile.png

This will create a gzipped file myfile.png.gz .

Upload the file to S3 without the .gz on the end. Set the Content-Encoding: header to gzip and set the Content-Type: header to the normal, correct value for the file, such as image/png .

This breaks any browser that doesn't understand Content-Encoding: gzip , but there should be no browsers in use that have that limitation.

Note that the -9 , above, means maximum compression.

If you're trying to gzip jpegs/pngs, I would suggest that you first compress them online with a tool such as https://tinyjpg.com/

You will not need to compress the images further ideally. Compressing images with image optimization tools will work better than using gzip -9 as it takes into consideration the textures, colors and patterns and such.

Also, make sure that you save your file in the proper formats (actual images in jpg and vector images in png) - This will help in reducing the size of the images

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM