无法通过Cloudfront从EC2解码CORS gzip压缩内容

时间:2015-05-06 17:21:31

标签: amazon-web-services gzip amazon-cloudfront

这可能与Serving gzipped CSS and JavaScript from Amazon CloudFront via S3有关,但是虽然我遵循了文档,但仍然可以让这种情况发挥作用:

S3存储桶包含我已经gzip压缩的文件(使用grunt-contrib-compress,如果有任何相关性的话。)

我的存储桶有CORS配置:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*.myorigin.net</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>DELETE</AllowedMethod>
        <AllowedMethod>HEAD</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

该存储桶通过Cloudfront提供。

My Cloudfront已将CORS配置为:

  • 允许GET,HEAD,OPTIONS,PUT等......
  • on HTTPS
  • 缓存OPTIONS请求
  • 将Access-Control-Request-Headers,Access-Control-Request-Method和Origin
  • 列入白名单

使用curl,我能够获取我的资源,使用正确的标头来请求gzip,我得到一个gzip压缩版本

curl --insecure https:/whatever.cloudfront.net/.../foo.js --silent -H "Accept-Encoding:gzip,deflate,sdch" -I

HTTP/1.1 200 OK
Content-Type: application/javascript
Content-Length: 33990
Connection: keep-alive
Date: Wed, 06 May 2015 17:12:53 GMT
Content-Encoding: gzip
Last-Modified: Wed, 06 May 2015 16:36:40 GMT
ETag: "7a92919df6117827de9474851afe06c0"
Accept-Ranges: bytes
Server: AmazonS3
Age: 70
X-Cache: Hit from cloudfront
Via: 1.1 (redacted).cloudfront.net (CloudFront)
X-Amz-Cf-Id: iaghGGDg3f3l4njz7mpXeOzqIS5OCR5kaehk4Td4-Bxiv2KtljHVlQ==

我能够通过添加--compressed标志来解码内容,因此文件似乎是正确的。

如果我现在再次使用curl执行CORS请求,我会得到正确的编码:

curl --insecure https://.../foo.js --silent -H "Accept-Encoding: gzip,deflate,sdch" -I -H "Origin: myorigin.net"
HTTP/1.1 200 OK
Content-Type: application/javascript
Content-Length: 33990
Connection: keep-alive
Date: Wed, 06 May 2015 17:16:06 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, HEAD
Access-Control-Max-Age: 3000
Content-Encoding: gzip
Last-Modified: Wed, 06 May 2015 16:36:40 GMT
ETag: "7a92919df6117827de9474851afe06c0"
Accept-Ranges: bytes
Server: AmazonS3
Vary: Origin,Access-Control-Request-Headers,Access-Control-Request-Method
X-Cache: Miss from cloudfront
Via: 1.1 redacted.cloudfront.net (CloudFront)
X-Amz-Cf-Id: kaUV4S9wiCGzXp8N_Gg2LhGK8uYw1-qRtSw6w_Ry4V8oHZiLRvWULA==

仍然出于某种原因,当我的浏览器执行请求时...没有&#34; Content-Encoding&#34;,因此内容未解压缩,因为我和#39; m用requirejs加载它,我得到一个错误的错误。

Request :
GET /resources-gz/foo.js HTTP/1.1
Host: d28p9e2ugw1o8w.cloudfront.net
Connection: keep-alive
Cache-Control: no-cache
Accept: */*
Pragma: no-cache
User-Agent: Mozilla/5.0 (X11; Linux i686 (x86_64)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Referer: http://localhost:3000/start
Accept-Encoding: gzip,deflate,sdch
Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,de;q=0.2

Response:
HTTP/1.1 200 OK
Content-Type: application/javascript
Content-Length: 33990
Connection: keep-alive
Date: Wed, 06 May 2015 16:31:27 GMT
Last-Modified: Wed, 06 May 2015 14:36:32 GMT
ETag: "7a92919df6117827de9474851afe06c0"
Accept-Ranges: bytes
Server: AmazonS3
Age: 1261
X-Cache: Hit from cloudfront
Via: 1.1 eazeaze.cloudfront.net (CloudFront)
X-Amz-Cf-Id: IjpNd37e-c38Moz3HQJ940KNTXfp1NN7O4enQJUYPd6Aet5Egw8XPg==

我显然错过了一些东西......

1 个答案:

答案 0 :(得分:0)

是的,你错过了什么。

阅读最后一个回复的回复标题。

Last-Modified: Wed, 06 May 2015 14:36:32 GMT
ETag: "7a92919df6117827de9474851afe06c0"
Age: 1261
X-Cache: Hit from cloudfront

这是CloudFront缓存命中,CloudFront在您请求之前20分钟从S3获取此对象...此对象的版本最后在S3中被修改了两个多小时...所以,您&# 39;重新获得一份可能在您修复之前的复制品#34;无论你最近修好了什么。

当一个物体的etag没有改变时,这意味着身体没有改变......但它并没有暗示任何有关响应头的信息。

最简单的测试方法是在S3中添加一个新文件,其中包含以前没有使用过的名称...当然,请尝试下载。