Conditional HTTP requests to cache


#1

I can’t seem to get Fastly to respond with “304 Not Modified” when I set an “If-None-Match” header that matches the “Etag” header served by my origin. I can tell that the resource is served from the cache but Fastly responds with 200 instead of 304.

Is there any way I can enable 304 responses?


#2

Can you share the output of a curl with -svo /dev/null ? Or a URL for us to test with? And if you don’t want to share publicly, could you open a ticket with the info? Because we most definitely support conditionals, and I would like to figure out what’s going wrong in your case.


#3

I can provide you with URLs, that’s no problem, everything is public anyway.

Fastly endpoint: http://s3.hex.pm.global.prod.fastly.net/tarballs/poison-1.5.2.tar
Origin endpoint: http://s3.hex.pm.s3.amazonaws.com/tarballs/poison-1.5.2.tar

Normal request to Fastly

$ curl -svo /dev/null http://s3.hex.pm.global.prod.fastly.net/tarballs/poison-1.5.2.tar
*   Trying 23.235.43.249...
* Connected to s3.hex.pm.global.prod.fastly.net (23.235.43.249) port 80 (#0)
> GET /tarballs/poison-1.5.2.tar HTTP/1.1
> Host: s3.hex.pm.global.prod.fastly.net
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< x-amz-id-2: tBDG+THGaVFGJQBDTxuWn4vPGdLOiPC7HROb9CrTGw478VVSzCnxLuN2HSiyowqYlu4vunkQUJE=
< x-amz-request-id: 5C053BC5723FCA28
< x-amz-replication-status: COMPLETED
< x-amz-version-id: Q7FpW60..0nH.jVgkSh9WJn9klIkgSJL
< x-amz-meta-surrogate-key: tarballs/poison-1.5.2
< Cache-Control: public, max-age=604800
< Last-Modified: Mon, 07 Mar 2016 20:09:39 GMT
< ETag: "59f42e90453d2c8624fbde40a7e630fe"
< Content-Type: application/octet-stream
< Server: AmazonS3
< Content-Length: 20480
< Accept-Ranges: bytes
< Date: Tue, 08 Mar 2016 22:57:57 GMT
< Via: 1.1 varnish
< Age: 0
< Connection: keep-alive
< X-Served-By: cache-ams4141-AMS
< X-Cache: MISS
< X-Cache-Hits: 0
< X-Timer: S1457477876.592347,VS0,VE499
<
{ [745 bytes data]
* Connection #0 to host s3.hex.pm.global.prod.fastly.net left intact

Request with If-None-Match to Fastly

$ curl -svo /dev/null -H "if-none-match: 59f42e90453d2c8624fbde40a7e630fe" http://s3.hex.pm.global.prod.fastly.net/tarballs/poison-1.5.2.tar
*   Trying 23.235.43.249...
* Connected to s3.hex.pm.global.prod.fastly.net (23.235.43.249) port 80 (#0)
> GET /tarballs/poison-1.5.2.tar HTTP/1.1
> Host: s3.hex.pm.global.prod.fastly.net
> User-Agent: curl/7.43.0
> Accept: */*
> if-none-match: 59f42e90453d2c8624fbde40a7e630fe
>
< HTTP/1.1 200 OK
< x-amz-id-2: tBDG+THGaVFGJQBDTxuWn4vPGdLOiPC7HROb9CrTGw478VVSzCnxLuN2HSiyowqYlu4vunkQUJE=
< x-amz-request-id: 5C053BC5723FCA28
< x-amz-replication-status: COMPLETED
< x-amz-version-id: Q7FpW60..0nH.jVgkSh9WJn9klIkgSJL
< x-amz-meta-surrogate-key: tarballs/poison-1.5.2
< Cache-Control: public, max-age=604800
< Last-Modified: Mon, 07 Mar 2016 20:09:39 GMT
< ETag: "59f42e90453d2c8624fbde40a7e630fe"
< Content-Type: application/octet-stream
< Server: AmazonS3
< Content-Length: 20480
< Accept-Ranges: bytes
< Date: Tue, 08 Mar 2016 23:01:05 GMT
< Via: 1.1 varnish
< Age: 188
< Connection: keep-alive
< X-Served-By: cache-ams4123-AMS
< X-Cache: HIT
< X-Cache-Hits: 1
< X-Timer: S1457478065.275572,VS0,VE2
<
{ [2194 bytes data]
* Connection #0 to host s3.hex.pm.global.prod.fastly.net left intact

Normal request to origin

$ curl -svo /dev/null http://s3.hex.pm.s3.amazonaws.com/tarballs/poison-1.5.2.tar
*   Trying 54.231.82.98...
* Connected to s3.hex.pm.s3.amazonaws.com (54.231.82.98) port 80 (#0)
> GET /tarballs/poison-1.5.2.tar HTTP/1.1
> Host: s3.hex.pm.s3.amazonaws.com
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< x-amz-id-2: hNc8LM6hMiHMQ08moNt//aNmQYaUTD6cQYMggnRHwk5loslTvRNVmYtjfmOFFfufgVpDDqk+fG0=
< x-amz-request-id: EF3FA8E731801182
< Date: Tue, 08 Mar 2016 23:02:07 GMT
< x-amz-replication-status: COMPLETED
< x-amz-version-id: Q7FpW60..0nH.jVgkSh9WJn9klIkgSJL
< x-amz-meta-surrogate-key: tarballs/poison-1.5.2
< Cache-Control: public, max-age=604800
< Last-Modified: Mon, 07 Mar 2016 20:09:39 GMT
< ETag: "59f42e90453d2c8624fbde40a7e630fe"
< Accept-Ranges: bytes
< Content-Type: application/octet-stream
< Content-Length: 20480
< Server: AmazonS3
<
{ [4296 bytes data]
* Connection #0 to host s3.hex.pm.s3.amazonaws.com left intact

Request with if-none-match to origin

$ curl -svo /dev/null -H "if-none-match: 59f42e90453d2c8624fbde40a7e630fe" http://s3.hex.pm.s3.amazonaws.com/tarballs/poison-1.5.2.tar
*   Trying 54.231.17.25...
* Connected to s3.hex.pm.s3.amazonaws.com (54.231.17.25) port 80 (#0)
> GET /tarballs/poison-1.5.2.tar HTTP/1.1
> Host: s3.hex.pm.s3.amazonaws.com
> User-Agent: curl/7.43.0
> Accept: */*
> if-none-match: 59f42e90453d2c8624fbde40a7e630fe
>
< HTTP/1.1 304 Not Modified
< x-amz-id-2: KiB2kxp396Wr1xYs4r2hFgczN/i0vImC2827yt4IHZU1U7CngzmOR0llENWIrUgrbJNMoCo5NEE=
< x-amz-request-id: CF56C5B8089C405F
< Date: Tue, 08 Mar 2016 23:02:22 GMT
< x-amz-replication-status: COMPLETED
< x-amz-version-id: Q7FpW60..0nH.jVgkSh9WJn9klIkgSJL
< x-amz-meta-surrogate-key: tarballs/poison-1.5.2
< Cache-Control: public, max-age=604800
< Last-Modified: Mon, 07 Mar 2016 20:09:39 GMT
< ETag: "59f42e90453d2c8624fbde40a7e630fe"
< Server: AmazonS3
<
* Connection #0 to host s3.hex.pm.s3.amazonaws.com left intact
```

#4

Ah, your origin is very forgiving (and ignoring the RFC.) You should include the double quotes in the INM header, like so:

curl -svo /dev/null -H 'if-none-match: "59f42e90453d2c8624fbde40a7e630fe"' http://s3.hex.pm.global.prod.fastly.net/tarballs/poison-1.5.2.tar```

Note that I switched to single quotes to surround the header. 

I'm on mobile, so I can't test the above easily, but I'm pretty sure that will work. ;-)

#5

That works indeed. Thanks for your help!