Amazon S3 和 Cloudfront 不 gzip on-fly

Amazon S3 and Cloudfront don't gzip on-fly

我想使用 Cloudfront 中的新功能,它允许使用 Accept-Encoding 压缩文件 on-fly:gzip header。我设置了我的 CDN 分发,打开 "Compress Objects Automatically",白名单 headers: Origin,Accept-Control-Request-Headers 和 Accept-Control-Request-Method(我正在使用 AngularJS,我需要它用于 OPTIONS 方法)。我的 S3 存储桶上没有设置任何 CORS。

如他们的文档所述,当我将 Accept-Encoding: gzip header 添加到请求时,它应该开始工作。但是,我仍在获取原始文件。

Response Headers

Accept-Ranges:bytes
Age:65505
Cache-Control:public, max-age=31557600
Connection:keep-alive
Content-Length:408016
Content-Type:text/css
Date:Mon, 21 Mar 2016 16:00:36 GMT
ETag:"5a04faf838d5165f24ebcba54eb5fbac"
Expires:Tue, 21 Mar 2017 21:59:21 GMT
Last-Modified:Mon, 21 Mar 2016 15:59:22 GMT
Server:AmazonS3
Via:1.1 0e6067b46ed4b3e688f898d03e5c1c67.cloudfront.net (CloudFront)
X-Amz-Cf-Id:gKYTTq0cIcUvHTtlrdMig8D1R2ZVdea4EnflV0-IxhtaxgRvLYj6LQ==
X-Cache:Hit from cloudfront

Request Headers

Accept:text/css,*/*;q=0.1
Accept-Encoding:gzip, deflate, sdch
Accept-Language:pl,en-US;q=0.8,en;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Host: XXX.cloudfront.net
Referer: XXX
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36

我的配置是:

注意这两个响应headers。

Age: 65505
X-Cache: Hit from cloudfront

此 object 已被先前的请求缓存,在您这次特定时间请求之前 65,505 秒(≅ 18 小时)。

一旦 CloudFront 在特定边缘缓存了 object,如果您稍后配置相关缓存行为以启用 on-the-fly 压缩,CloudFront 将不会返回并且 re-compress objects 已经在它的缓存中。它将继续为 object 的原始版本提供服务,直到它被驱逐。

如果这 18 小时的时间间隔早于您在分发上启用压缩,那么这就是您所看到的最可能的解释。

CloudFront compresses files in each edge location when it gets the files from your origin. When you configure CloudFront to compress your content, it doesn't compress files that are already in edge locations. In addition, when a file expires in an edge location and CloudFront forwards another request for the file to your origin, CloudFront doesn't compress the file if your origin returns an HTTP status code 304, which means that the edge location already has the latest version of the file. If you want CloudFront to compress the files that are already in edge locations, you'll need to invalidate those files.

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html

通过为路径 *(覆盖所有内容)提交 invalidation request 或只是这个特定的 /path/path* 等,从您的分发缓存中驱逐所有内容。几分钟内,您的分发(或特定路径匹配,如果您未指定 * 一切)的所有缓存内容将被驱逐(等待失效显示它已完成),并且您 应该看到压缩对后续请求有效。

密切关注 Age:(CloudFront 拥有特定响应的副本多长时间),一旦它消失然后重新设置,我敢猜测您会看到您期望的结果.

如果这不能解决问题,还有另一种可能性,但我希望这是一个相当不寻常的事件:

In rare cases, when a CloudFront edge location is unusually busy, some files might not be compressed.

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html