以 S3 网站为源的 CloudFront 不提供压缩文件 [英] CloudFront with S3 website as origin is not serving gzipped files

查看:39
本文介绍了以 S3 网站为源的 CloudFront 不提供压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AWS 现在支持通过 CloudFront 压缩文件

我已按照提供压缩文件中的所有说明进行操作,但 gzip 压缩不起作用.

I've followed along with all of the instructions in Serving Compressed Files, and yet gzipping is not working.

我将一个 S3 存储桶设置为 CloudFront 用作源的网站.

I have an S3 bucket set up as a website that CloudFront is using as the origin.

  • 自动压缩对象 已启用
  • 我正在提供具有正确内容类型的文件,例如 application/javascripttext/css
  • 文件在 1,000 到 10,000,000 字节之间
  • 据我所知,S3 网站提供带有 Content-Length 的文件
  • 为了确保没有缓存任何内容,我使整个 S3 存储桶无效并将文件的较新版本上传到 S3.
  • 此外,我使用的网络浏览器 Chrome 接受 gzipped 文件.
  • Compress Objects Automatically is enabled
  • I am serving files with the correct content types such as application/javascript and text/css
  • The files are within 1,000 and 10,000,000 bytes
  • The S3 website serves the files with a Content-Length as far as I know
  • To be extra sure nothing was cached, I both invalidated the entire S3 bucket and uploaded newer versions of the files to S3.
  • Additionally, the web browser I am using, Chrome, does accept gzipped files.

尽管如此,我还是无法让 gzipping 工作.我已经让包括 SSL 在内的其他一切都完美运行,您可以在此处访问该站点:https://formulagrid.com/

Despite all this, I can't get gzipping to work. I have gotten everything else including SSL working perfectly, and you can visit the site here: https://formulagrid.com/

如果您打开 chrome 控制台,您会注意到从 S3 提供的所有文件都没有被压缩.唯一的 gzipped 文件(例如 google 字体)是我从其他 CDN 中抓取的文件.

If you open up the chrome console, you'll notice that none of the files being served from S3 are being gzipped. The only gzipped files such as the google font are the ones I'm grabbing from other CDNs.

推荐答案

我今天遇到了同样的错误,并通过向 S3 存储桶添加 CORS 规则解决了这个问题.此规则可确保将 Content-Length 标头发送到 Cloudfront,以便可以对内容进行 gzip:

I hit the same error today and solved it by adding a CORS rule to the S3 bucket. This rule ensures the Content-Length header is sent to Cloudfront so content can be gzipped:

S3 > Bucket > 权限 > CORS 配置

S3 > Bucket > Permissions > CORS Configuration

<?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

Credit goes to Robert Ellison: http://ithoughthecamewithyou.com/post/enable-gzip-compression-for-amazon-s3-hosted-website-in-cloudfront

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

As far I know, this seems to be an undocumented requirement.

这篇关于以 S3 网站为源的 CloudFront 不提供压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆