Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 553
panipsilos Avatar asked Jan 05 '23 15:01

panipsilos


1 Answers

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.

like image 129
Michael - sqlbot Avatar answered Jan 11 '23 15:01

Michael - sqlbot