简体   繁体   English

如何在AWS CloudFront上启用gzip压缩

[英]How to enable gzip compression on AWS CloudFront

I m trying to gzip compress the img I m serving through CloudFront . 我试图通过CloudFront gzip压缩img服务。 My origin is S3 我的起源是S3

Based on several articles/blogs on aws, what I did is: 根据关于aws的几篇文章/博客,我所做的是:

1) Set "Content-Length" header for the object I want to compress. 1)为我要压缩的对象设置"Content-Length"标题。 I set the value equal to the size appeared on the size property box 我将值设置为size属性框中显示的size

2) Set the Compress Objects Automatically value to Yes in the Behaviour of my cloud distribution. 2)在我的云分发Behaviour中将“ Compress Objects Automatically值设置为“ Yes ”。

3) I invalidated my object to get a fresh copy from S3 . 3)我使我的对象无效以从S3获得新的副本。

Still I m not able to make CloudFront gzip my object. 我仍然无法使CloudFront gzip成为我的对象。 Any ideas? 有任何想法吗?

I'm trying to gzip compress the [image] 我正在尝试gzip压缩[图片]

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. 您通常不需要gzip图像 - 这样做可以节省很少的带宽(如果有的话),因为Web上使用的几乎所有图像格式都已经过压缩。

Also, CloudFront doesn't support it. 此外,CloudFront不支持它。

See File Types that CloudFront Compresses for the suported file formats. 有关支持的文件格式请参阅CloudFront压缩的文件类型 They are text-based formats, which tend to benefit substantially from gzip compression. 它们是基于文本的格式,往往会从gzip压缩中获益。

If you really want the files served gzipped, you can store the files in S3, already gzipped. 如果你真的想要gzip压缩文件,你可以将文件存储在S3中,已经gzip压缩。

$ gzip -9 myfile.png

This will create a gzipped file myfile.png.gz . 这将创建一个gzip压缩文件myfile.png.gz

Upload the file to S3 without the .gz on the end. 将文件上传到S3 而不使用.gz。 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 . Content-Encoding:标头设置为gzip ,并将Content-Type:标头设置为文件的正常,正确值,例如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. 这会破坏任何不理解Content-Encoding: gzip浏览器Content-Encoding: gzip ,但是应该没有使用具有该限制的浏览器。

Note that the -9 , above, means maximum compression. 请注意,上面的-9表示最大压缩。

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/ 如果您正在尝试gzip jpegs / pngs,我建议您首先使用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. 使用图像优化工具压缩图像比使用gzip -9更好,因为它考虑了纹理,颜色和图案等。

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 此外,请确保以适当的格式保存文件(jpg中的实际图像和png中的矢量图像) - 这将有助于减小图像的大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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