以 S3 网站为源的 CloudFront 不提供 gzip 文件

CloudFront with S3 website as origin is not serving gzipped files

AWS now supports gzipping files through CloudFront

我已按照 Serving Compressed Files 中的所有说明进行操作,但 gzip 压缩不起作用。

我将 S3 存储桶设置为 CloudFront 用作来源的网站。

尽管如此,我还是无法使用 gzip 压缩。我已经得到了包括 SSL 在内的其他所有东西都可以正常工作,你可以在这里访问该站点:https://formulagrid.com/

如果您打开 chrome 控制台,您会注意到 none 从 S3 提供的文件正在被 gzip 压缩。 google 字体等唯一的 gzip 文件是我从其他 CDN 抓取的文件。

我今天遇到了同样的错误,通过向S3存储桶添加CORS规则解决了这个问题。此规则确保将 Content-Length header 发送到 Cloudfront,以便可以压缩内容:

S3 > 存储桶 > 权限 > CORS 配置

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>Authorization</AllowedHeader>
        <AllowedHeader>Content-Length</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

感谢罗伯特·埃里森:http://ithoughthecamewithyou.com/post/enable-gzip-compression-for-amazon-s3-hosted-website-in-cloudfront

据我所知,这似乎是一个未记录的要求。

我的问题是我上传的文件专门使用 utf-8 编码。来自文档:

CloudFront determines whether the file is compressible:

The file must be of a type that CloudFront compresses.

The file size must be between 1,000 and 10,000,000 bytes.

The response must include a Content-Length header so CloudFront can determine whether the size of the file is in the range that CloudFront compresses. If the Content-Length header is missing, CloudFront won't compress the file.

The response must not include a Content-Encoding header.

由于 Cloudfront 现在只接受 JSON,您必须粘贴此内容:

[
    {
        "AllowedHeaders": [
            "Authorization",
            "Content-Length"
        ],
        "AllowedMethods": [
            "GET"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [],
        "MaxAgeSeconds": 3000
    }
]

我已经尝试了以上所有方法,但 CloudFront 仍然没有压缩我的 S3 存储桶内容。

我的问题是我已经有一个禁用压缩的现有 Cloudfront 发行版,后来我又打开了。这不知何故不允许考虑压缩。在许多不成功的变通办法之后,我删除了 CloudFront 分配并从一开始就使用压缩重新创建它。这解决了我的问题。