简体   繁体   中英

Rails + Heroku + Cloudfront & S3 Cache

I am testing my site and got an alert that I don't have any asset caching configured for static images served though S3/Cloudfront. Almost all my get requests return a "X-Cache:Miss from cloudfront" response. I just set this up a couple days ago and all the tutorials say to configure each item and add the headers manually. I have an asset library of 6k icons, so I'd rather have this dealt with where I dont have to set the headers on upload.

I use design.cdn.getsprk.com which is pointed to my cloudfront account, which points to my s3 as the origin. I'm using Rails on Heroku.

I have also tested the headers using the direct s3 link, the cloudfront and custom domains. None are showing the correct headers.

I attached my configuration settings for cloudfront.

Example icon: design.cdn.getsprk.com/icons/space/flat/11-neil-armstrong.svg

Request header:

GET /icons/space/flat/11-neil-armstrong.svg HTTP/1.1
Host: design.cdn.getsprk.com
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36
DNT: 1
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
If-None-Match: "746b7f4bce35cf3e6a8c965207087631"
If-Modified-Since: Tue, 10 Nov 2015 23:59:32 GMT

Response:

Connection:keep-alive
Date:Mon, 16 Nov 2015 22:29:05 GMT
ETag:"746b7f4bce35cf3e6a8c965207087631"
Last-Modified:Tue, 10 Nov 2015 23:59:32 GMT
Server:AmazonS3
Via:1.1 483919577b54800dcfc93dcd11176f74.cloudfront.net (CloudFront)
X-Amz-Cf-Id:mliY9nJt-2KEyu3aPpsxLm-Vd1LTKLRWYj9dTmfg54Mui0SKVhHL-g==
X-Cache:Miss from cloudfront

Cloudfront config: Cloudfront设定

It looks like you are making a conditional request, notice the presence of If-None-Match/If-Modified-Since. If the object is not in cache, it will always be a Miss. Can you try pressing Ctrl+F5 in your browser to make a non-conditional request?

curl --compressed -v -o /dev/null design.cdn.getsprk.com/icons/space/flat/11-neil-armstrong.svg
* About to connect() to design.cdn.getsprk.com port 80 (#0)
*   Trying 54.192.144.164...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* connected
* Connected to design.cdn.getsprk.com (54.192.144.164) port 80 (#0)
> GET /icons/space/flat/11-neil-armstrong.svg HTTP/1.1
> User-Agent: curl/7.26.0
> Host: design.cdn.getsprk.com
> Accept: */*
> Accept-Encoding: deflate, gzip
> 
* additional stuff not fine transfer.c:1037: 0 0
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 200 OK
< Content-Type: image/svg+xml
< Content-Length: 7702
< Connection: keep-alive
< Date: Wed, 18 Nov 2015 00:43:12 GMT
< Last-Modified: Tue, 10 Nov 2015 23:59:32 GMT
< ETag: "746b7f4bce35cf3e6a8c965207087631"
< Accept-Ranges: bytes
< Server: AmazonS3
< Age: 9094
< **X-Cache: Hit from cloudfront**
< Via: 1.1 43ff408868c37128de942a235a1225f2.cloudfront.net (CloudFront)
< X-Amz-Cf-Id: G7VwUZ3l15YnxjbZaeZmeeqSaj_E4OszrsS2sSfFjqkh1ZlTOXoKEg==
< 
{ [data not shown]
100  7702  100  7702    0     0  70606      0 --:--:-- --:--:-- --:--:--  104k
* Connection #0 to host design.cdn.getsprk.com left intact
* Closing connection #0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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