简体   繁体   English

Rails 3.2 + Heroku + S3 + CloudFront:不提供gzip CSS js

[英]Rails 3.2 + Heroku + S3 + CloudFront: Not serving gzip css js

I am running Rails 3.2 with assets_sync. 我正在使用asset_sync运行Rails 3.2。

A gz version of my css and js are created. 创建了我的CSS和JS的GZ版本。 CloudFront is not serving the gzip version even though the header requested for gzip,deflate. 即使要求gzip,deflate的标头,CloudFront也不提供gzip版本。

I also tried config.gzip_compression = true in asset_sync.rb but CloudFront is still serving the uncompressed version. 我也尝试在asset_sync.rb尝试config.gzip_compression = true ,但是CloudFront仍在提供未压缩的版本。 I have confirmed that I can access ABC.cloudfront.net/XYZ.css.gz 我已经确认可以访问ABC.cloudfront.net/XYZ.css.gz

Is the origin for your CloudFront distribution an S3 bucket? 您的CloudFront分发的来源是S3存储桶吗? If so, the problem is that S3 doesn't handle the Accept-Encodings header correctly: https://github.com/rumblelabs/asset_sync/issues/153 如果是这样,那么问题在于S3无法正确处理Accept-Encodings标头: https : //github.com/rumblelabs/asset_sync/issues/153

There are some potential solutions outlined in that issue. 该问题中概述了一些潜在的解决方案。 You can either serve gzipped files by default, since most browsers (excluding some mobile browsers) will handle it correctly, or you con try the solution outlined here . 您可以默认提供压缩文件,因为大多数浏览器(某些移动浏览器除外)都可以正确处理该文件,也可以尝试此处概述解决方案

Another possible solution is to use a Varnish cache layer as the custom origin for your CloudFront distribution: 另一个可能的解决方案是使用Varnish缓存层作为CloudFront分配的自定义来源:

Internet -> CloudFront -> Varnish -> S3 Bucket 互联网-> CloudFront->清漆-> S3桶

As a request comes in to CloudFront, it will be forwarded to Varnish if it hasn't been cached in CloudFront, yet. 当向CloudFront发出请求时,如果尚未将其缓存到CloudFront中,它将被转发到Varnish。 Varnish will grab the uncompressed file from the S3 Bucket, compress it as the headers dictate, and send the file back to CloudFront for caching. Varnish将从S3存储桶中获取未压缩的文件,按照标头指示对其进行压缩,然后将文件发送回CloudFront进行缓存。

您是否为应用程序设置了heroku config:add ASSET_SYNC_GZIP_COMPRESSION=true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM