【发布时间】:2017-09-19 23:35:30
【问题描述】:
我启用了 Amazon CloudFront gzip 功能:“Compress Objects Automatically”。
这发生在我的 CloudFront 中的所有文件上,而其他 CSS/JS 文件作为 gzip 加载(仔细检查我的服务器请求标头是否接受 gzip 文件Accept-Encoding: gzip)。
我真的很想弄清楚这一点,因为所有教程和谷歌搜索结果都对如何检查单选按钮“自动压缩对象”给出了相同的解释——这显然没有帮助。
我想也许我不能 gzip 文件,因为它们太小而无法压缩 - 但是遵循 google speed test 明确表示我可以使用 gzip 压缩这些文件。
Compressing https://Cloudfront.cloudfront.net/live/static/rcss/bootstrap.min.css could save 100.3KiB (83% reduction).
Compressing https://Cloudfront.cloudfront.net/live/static/rcss/style.css could save 60.5KiB (80% reduction).
Compressing https://Cloudfront.cloudfront.net/live/static/shop/css/jquery.range.css could save 4.6KiB (83% reduction).
Compressing https://Cloudfront.cloudfront.net/live/static/rcss/font-awesome.min.css could save 21.9KiB (77% reduction).
Compressing https://Cloudfront.cloudfront.net/live/static/rcss/responsive.css could save 20KiB (80% reduction).
Compressing https://Cloudfront.cloudfront.net/live/static/general.min.js?ver=9.70 could save 232.9KiB (72% reduction).
Compressing https://Cloudfront.cloudfront.net/live/static/rcss/magnific-popup.css could save 5.7KiB (75% reduction).
Compressing https://Cloudfront.cloudfront.net/live/static/bootstrap.min.js could save 26.4KiB (73% reduction).
Compressing https://Cloudfront.cloudfront.net/…ve/static/plugins/jquery.validate.min.js could save 14KiB (67% reduction).
Compressing https://Cloudfront.cloudfront.net/…tic/plugins/jquery.magnific-popup.min.js could save 13.2KiB (63% reduction).
Compressing https://Cloudfront.cloudfront.net/live/static/plugins/jquery.range.min.js could save 3.9KiB (66% reduction).
Compressing https://Cloudfront.cloudfront.net/live/static/voting/jquery.cookie.js could save 1.2KiB (53% reduction).
我缺少哪些部分可以帮助我使用 Cloudfront 压缩文件?
这是我的响应标头的样子:
Accept-Ranges:bytes
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:122540
Content-Type:text/css
Date:Sun, 23 Apr 2017 13:14:07 GMT
ETag:"2cb56af0a65d6ac432b906d085183457"
Last-Modified:Tue, 02 Aug 2016 08:49:54 GMT
Server:AmazonS3
Via:1.1 2cb56af0a65d6ac432b906d085183457.cloudfront.net (CloudFront)
X-Amz-Cf-Id:eCPcSDedADnqDZMlMbFjj08asdBSn7_lfR0imlXAT181Y8qRMtSZASDF27AiSTK8PDQ==
x-amz-meta-s3cmd-attrs:uid:123/gname:ubuntu/uname:ubuntu/gid:666/mode:666/mtime:666/atime:666/md5:2cb56af0a65d6ac432b906d085183457/ctime:666
X-Cache:RefreshHit from cloudfront
编辑:
我理解在 200 和 304 上返回的概念 - 当删除浏览器缓存时,它总是显示 200 响应。
所以 Cloudfront 有一些缓存?我将 bootstrap3.min.css 文件添加到“无效”表中 - 没有用。
确保文件设置为压缩。
将此添加到我的 website.com.conf 文件以启用 gzip 并显示 content-length 标头:
DeflateBufferSize 8096
SetOutputFilter DEFLATE
DeflateCompressionLevel 9
尝试从我的 .conf 文件中删除 DeflateBufferSize 8096 并将 <AllowedHeader>Content-Length</AllowedHeader> 添加到“CORS 配置”中 - 我确实得到了 Content-Length 正确 - 但仍然没有 GZIPed。 (关注CloudFront with S3 website as origin is not serving gzipped files)
这是我目前得到的:
Request URL:https://abc.cloudfront.net/live/static/rcss/bootstrap3.min.css
Request Method:GET
Status Code:200 OK
Remote Address:77.77.77.77:443
Referrer Policy:no-referrer-when-downgrade
Response Headers
Accept-Ranges:bytes
Age:1479
Connection:keep-alive
Content-Length:122555
Content-Type:text/css
Date:Wed, 26 Apr 2017 08:48:34 GMT
ETag:"83527e410cd3fff5bd1e4aab253910b2"
Last-Modified:Wed, 26 Apr 2017 08:43:05 GMT
Server:AmazonS3
Via:1.1 5fc044210ebc4ac6efddab8b0bf5a686.cloudfront.net (CloudFront)
X-Amz-Cf-Id:3ZBgDY0c1WV_Pc0o_Bjwa5cQ9D9T-Cr30QDxd_GvD30iQ8W1ImReQIH==
X-Cache:Hit from cloudfront
Request Headers
Accept:text/css,*/*;q=0.1
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:abc.cloudfront.net
Pragma:no-cache
Referer:https://example.com/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36
编辑#2:
关注:http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html
如果您将 CloudFront 配置为压缩内容,CloudFront 会从它压缩的文件中删除 ETag 响应标头。当 ETag 标头存在时,CloudFront 和您的源可以使用它来确定 CloudFront 边缘缓存中的文件版本是否与源服务器上的版本相同。但是,压缩后两个版本不再相同。
我得到相同的 Etag 含义 - 提供的 css 文件没有经过任何压缩。
想也许我没有为这个特定文件设置正确的压缩 - 现在设置为
*/bootstrap3.min.css(因为它在目录中)。
我之前有这套
bootstrap3.min.css
两者都不起作用。
我的网址是:https://abc.cloudfront.net/live/static/rcss/bootstrap3.min.css 在此之后,我将我的失效部分编辑为:
/live/static/rcss/bootstrap3.min.css
/static/rcss/bootstrap3.min.css
/rcss/bootstrap3.min.css
/bootstrap3.min.css
这可能是我的实际问题吗?
【问题讨论】:
-
我启用了 Amazon S3 gzip 功能:“自动压缩对象”。 嗯……不,你没有……因为 S3 没有这样的选项。这是 CloudFront 的功能,而不是 S3。请提供一个无法按预期工作的 URL 的真实示例,因为显然“s3.cloudfront.net”没有意义。
-
@Michael-sqlbot 对不起!我的意思是 CloudFront! (现在好累)我跟着这个教程:deliciousbrains.com/wp-offload-s3/doc/…
-
P.S:我不喜欢展示真实 URL 的实际示例。 @Michael-sqlbot
-
@Michael-sqlbot 可能设置
DeflateBufferSize 8096是我的问题?我添加了这个以获得Content-Length,因为我没有得到它。我应该遵循这个吗? *.com/questions/35590622/… -
我确实将
Content-Length行添加到了CORS 文件中,并从我的my-website.conf文件中删除了DeflateBufferSize 8096。它确实显示了Content-Length,但仍然不起作用。
标签: amazon-s3 compression gzip amazon amazon-cloudfront