amazon-web-services - 使用 CloudFront 的 Gzip 压缩不起作用

标签 amazon-web-services amazon-s3 gzip amazon-cloudfront

我有一个有角度的应用程序,即使是使用生产模式构建的,它也有多个大文件(超过 1MB)。

我想使用 CloudFront 上的 gzip 压缩功能来压缩它们。

我在 CloudFront 控制台中激活了“自动压缩对象”选项。我的分发源是一个 s3 存储桶。

但是当我通过我的浏览器加载页面时下载的包没有用 gzip 压缩

这是一个请求/响应的例子

请求 header :

:authority:dev.test.com
:method:GET
:path:/vendor.cc93ad5b987bea0611e1.bundle.js
:scheme:https
accept:*/*
accept-encoding:gzip, deflate, br
accept-language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
cache-control:no-cache
pragma:no-cache
referer:https://dev.test.com/console/projects
user-agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36

响应头

accept-ranges:bytes
age:17979
content-length:5233622
content-type:text/javascript
date:Tue, 07 Nov 2017 08:42:08 GMT
etag:"6dfe6e16901c5ee5c387407203829bec"
last-modified:Thu, 26 Oct 2017 09:57:15 GMT
server:AmazonS3
status:200
via:1.1 9b307acf1eed524f97301fa1d3a44753.cloudfront.net (CloudFront)
x-amz-cf-id:9RpiXSuSGszUaX7hBA4ZaEO949g76UDoCaxzwFtiWo7C-wla-PyBsA==
x-cache:Hit from cloudfront

根据 AWS 文档,一切正常:

  • 接受编码:gzip
  • 存在的内容长度
  • 1,000 到 10,000,000 字节
  • ...

你知道为什么 cloudfront 不压缩我的文件吗?

最佳答案

这个响应是几个小时前缓存的。

age:17979

CloudFront 不会返回并 gzip 已缓存的内容。

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

做一个缓存失效,等待它完成,然后重试。

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

关于amazon-web-services - 使用 CloudFront 的 Gzip 压缩不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47159895/

相关文章:

python - 文件从swift上传到django

angular - 如何只允许特定用户访问 Amazon S3 上的内容

node.js - 使用 Node.js 的 AWS 签名版本 4 S3 上传

javascript - 使用 <script> 导入 JavaScript 是否会使用 gzip 流?

linux - 在 bash 中应用 CHMOD 并压缩文件

amazon-web-services - 在 ACM 中为 Elastic Beanstalk 后端请求证书

amazon-web-services - 在存储库的 cloudformation 模板中使用 AWS::CodeBuild::Project 环境变量

java - GZIP压缩和解压问题

java - 在 AWS EMR 中,我如何记录类路径以调试类加载器问题

amazon-web-services - 如何在 AWS ECR 中使用 ASSUME_ROLE 推送到账户