我尝试在S3上设置CloudFront,但我遇到了以下问题。
我在跨域请求图片和文本文件。有时,对文件的请求是使用XHR进行的,有时是嵌入图像标记。
我看到的问题是,如果通过XHR向资产发出请求,则其中包含&{39; Origin
'标题,并且回复具有正确的&{39; Access-Control-Allow-Origin
'。如果我之后请求相同的资产,则不包括&{39; Origin
'标题,它没有' Access-Control-Allow-Origin
'头。 但是 ...如果我通过XHR提出另一项请求,包括' Origin
'标题为同一网址时,会返回错误的回复没有' Access-Control-Allow-Origin
'为了更多。
我将CloudFront设置为白名单' Origin
'标题,而不是缓存OPTIONS
。
我的S3 CORS政策:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<MaxAgeSeconds>60</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
这是一个cURL示例:
通过XHR提出请求
~$ curl 'http://xxxxxxxx.cloudfront.net/test/test_03.txt' -H 'Origin: http://www.cnn.com' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-GB,en;q=0.8,en-US;q=0.6,en-NZ;q=0.4,zh-TW;q=0.2,zh;q=0.2,en-AU;q=0.2' -H 'User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53' -H 'Accept: */*' -H 'Referer: http://www.cnn.com/' -H 'Proxy-Connection: keep-alive' --compressed
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sat, 22 Aug 2015 08:42:56 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Access-Control-Max-Age: 60
Last-Modified: Sat, 22 Aug 2015 08:41:08 GMT
ETag: "0cbc6611f5540bd0809a388dc95a615b"
Accept-Ranges: bytes
Server: AmazonS3
Vary: Origin
X-Cache: Miss from cloudfront
Via: 1.1 8d45ffe3c8bfd31eef4b048ab3ea99b2.cloudfront.net (CloudFront)
X-Amz-Cf-Id: lY4reBVHrFq3MVgdFLx4LvbaOj8UnypXIHxBRvUN4TkkXzyHVmQcmQ==
Content-Length: 4
Connection: Keep-Alive
重复请求(请注意缓存命中)
~$ curl 'http://xxxxxxxx.cloudfront.net/test/test_03.txt' -H 'Origin: http://www.cnn.com' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-GB,en;q=0.8,en-US;q=0.6,en-NZ;q=0.4,zh-TW;q=0.2,zh;q=0.2,en-AU;q=0.2' -H 'User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53' -H 'Accept: */*' -H 'Referer: http://www.cnn.com/' -H 'Proxy-Connection: keep-alive' --compressed
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sat, 22 Aug 2015 08:42:56 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Access-Control-Max-Age: 60
Last-Modified: Sat, 22 Aug 2015 08:41:08 GMT
ETag: "0cbc6611f5540bd0809a388dc95a615b"
Accept-Ranges: bytes
Server: AmazonS3
Age: 6
Vary: Origin
X-Cache: Hit from cloudfront
Via: 1.1 90d8e168b0948a3eb36a451ebb27f4f9.cloudfront.net (CloudFront)
X-Amz-Cf-Id: cU-TCt-_5MsdLUrUqQk7_HLCUVyNEWOWW2ET19--Pc6j4M-8LkfsyQ==
Content-Length: 4
Connection: Keep-Alive
请求不带Origin
标题
~$ curl 'http://xxxxxxxx.cloudfront.net/test/test_03.txt' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-GB,en;q=0.8,en-US;q=0.6,en-NZ;q=0.4,zh-TW;q=0.2,zh;q=0.2,en-AU;q=0.2' -H 'User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53' -H 'Accept: */*' -H 'Referer: http://www.cnn.com/' -H 'Proxy-Connection: keep-alive' --compressed
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sat, 22 Aug 2015 08:43:18 GMT
Last-Modified: Sat, 22 Aug 2015 08:41:08 GMT
ETag: "0cbc6611f5540bd0809a388dc95a615b"
Accept-Ranges: bytes
Server: AmazonS3
X-Cache: Miss from cloudfront
Via: 1.1 bd9375a232718e4567ed228bf8c06fc9.cloudfront.net (CloudFront)
X-Amz-Cf-Id: 03bYSXai6AmwmPRHukm3g8Qv09qB-KdyYs5sXb5RLtPuPdYQHD35hA==
Content-Length: 4
Connection: Keep-Alive
Age: 0
重复请求,不带Origin
标题
~$ curl 'http://xxxxxxxx.cloudfront.net/test/test_03.txt' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-GB,en;q=0.8,en-US;q=0.6,en-NZ;q=0.4,zh-TW;q=0.2,zh;q=0.2,en-AU;q=0.2' -H 'User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53' -H 'Accept: */*' -H 'Referer: http://www.cnn.com/' -H 'Proxy-Connection: keep-alive' --compressed
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sat, 22 Aug 2015 08:43:18 GMT
Last-Modified: Sat, 22 Aug 2015 08:41:08 GMT
ETag: "0cbc6611f5540bd0809a388dc95a615b"
Accept-Ranges: bytes
Server: AmazonS3
X-Cache: Miss from cloudfront
Via: 1.1 bd9375a232718e4567ed228bf8c06fc9.cloudfront.net (CloudFront)
X-Amz-Cf-Id: 03bYSXai6AmwmPRHukm3g8Qv09qB-KdyYs5sXb5RLtPuPdYQHD35hA==
Content-Length: 4
Connection: Keep-Alive
Age: 3
使用Origin
标头发出请求。请注意缺少Access-Control-Allow-Origin
~$ curl 'http://xxxxxxxx.cloudfront.net/test/test_03.txt' -H 'Origin: http://www.cnn.com' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-GB,en;q=0.8,en-US;q=0.6,en-NZ;q=0.4,zh-TW;q=0.2,zh;q=0.2,en-AU;q=0.2' -H 'User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53' -H 'Accept: */*' -H 'Referer: http://www.cnn.com/' -H 'Proxy-Connection: keep-alive' --compressed
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sat, 22 Aug 2015 08:43:18 GMT
Last-Modified: Sat, 22 Aug 2015 08:41:08 GMT
ETag: "0cbc6611f5540bd0809a388dc95a615b"
Accept-Ranges: bytes
Server: AmazonS3
X-Cache: Miss from cloudfront
Via: 1.1 bd9375a232718e4567ed228bf8c06fc9.cloudfront.net (CloudFront)
X-Amz-Cf-Id: 03bYSXai6AmwmPRHukm3g8Qv09qB-KdyYs5sXb5RLtPuPdYQHD35hA==
Content-Length: 4
Connection: Keep-Alive
Age: 9
与Origin
标头相同的请求。请注意缺少Access-Control-Allow-Origin
和缓存未命中
~$ curl 'http://xxxxxxxx.cloudfront.net/test/test_03.txt' -H 'Origin: http://www.cnn.com' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-GB,en;q=0.8,en-US;q=0.6,en-NZ;q=0.4,zh-TW;q=0.2,zh;q=0.2,en-AU;q=0.2' -H 'User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53' -H 'Accept: */*' -H 'Referer: http://www.cnn.com/' -H 'Proxy-Connection: keep-alive' --compressed
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sat, 22 Aug 2015 08:43:18 GMT
Last-Modified: Sat, 22 Aug 2015 08:41:08 GMT
ETag: "0cbc6611f5540bd0809a388dc95a615b"
Accept-Ranges: bytes
Server: AmazonS3
X-Cache: Miss from cloudfront
Via: 1.1 bd9375a232718e4567ed228bf8c06fc9.cloudfront.net (CloudFront)
X-Amz-Cf-Id: 03bYSXai6AmwmPRHukm3g8Qv09qB-KdyYs5sXb5RLtPuPdYQHD35hA==
Content-Length: 4
Connection: Keep-Alive
Age: 14
答案 0 :(得分:2)
正如@Michael所怀疑的那样,这是由于某些不良中介缓存造成的。
通过SSL访问CloudFront可防止问题发生。