如何将 "Accept-Encoding" header 添加到我的 CloudFront 分配?

How can I add the "Accept-Encoding" header to my CloudFront distribution?

我打开 CloudFront 仪表板并单击分发 ID,然后单击源选项卡并单击,选择默认源然后单击编辑。 试图在 Custom Origin Headers 下的 'Header Name' 中添加 "Accept-Encoding" 并将其值设置为 "gzip",但它显示以下错误:

com.amazonaws.services.cloudfront.model.InvalidArgumentException: The parameter HeaderValue for Accept-Encoding is not empty. (Service: AmazonCloudFront; Status Code: 400; Error Code: InvalidArgument; Request ID: 05685bb6-ca02-11e7-8a62-0b7037d34a58)

如何通过 CloudFront 启用 gzip 压缩?

您无法将 CloudFront 配置为基于 Accept-Encoding header 进行缓存。引自 aws docs:

You can configure CloudFront to cache based on the value of any request header except the following:

  • Accept-Encoding,
  • Connection,
  • ...

但是,您可以将 CloudFront 配置为压缩 supported 文件类型。 在 CloudFront 控制台中“Compress objects automatically” select Yes.

注意:只有大小在 1,000 到 10,000,000 字节之间的文件才会被压缩。当您使用 S3 或任何其他自定义来源时,CloudFront 必须获得带有 Content-Length header 的响应,以便它可以确定文件的大小是否在 CloudFront 压缩的范围内。如果您使用的是 S3,请转至 CORS 配置并将 Content-Length header 添加到允许的 header 列表中。

如果您希望您的来源 压缩文件,设置取决于您在该来源上拥有运行 的服务器。 Server 应设置为使用 Content-Encoding: gzip header 向 CloudFront 提供 gzip-ed 内容,这样它就不会被 CloudFront 压缩。 这些是基本准则,但具体设置取决于您的具体配置。