Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request Headers vs Response Headers

I am trying to get an image to cache on a website. The image is served by PHP in which I am setting a response header of cache control with the appropriate value, however this is not caching the image, I think it might have something to do with the request header having a cache control of no-cache? Headers below:

Response Headers

Access-Control-Allow-Origin:*
Cache-Control:max-age=290304000, public
Connection:close
Content-Encoding:gzip
Content-Length:5166
Content-Type:image/jpeg
Date:Thu, 28 Jan 2016 15:50:56 GMT
Etag:f31a1f9f6699e0660c6b0b8e2c133add
Expires:Sat, 27 Feb 2016 15:50:56 GMT
Last-Modified:Thu, 26 Nov 2015 11:40:11 GMT
Server:Apache/2.2.15 (Red Hat)
Vary:Accept-Encoding
X-Powered-By:PHP/5.6.17

Request Headers

Accept:image/webp,image/*,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Authorization:Basic dGs6dGs=
Cache-Control:no-cache
Connection:keep-alive
Cookie:_dc_gtm_UA-60001243-5=1; _gat_UA-60001243-5=1; laravel_session=eyJpdiI6IkdcL0VYYUxMNEU0Q1wvM1VvQlpOU3NDZz09IiwidmFsdWUiOiJWaGVDaVJqdGdMaWlOaDBzUTI5Tzd3TmE1SU9UR2VcL29ZallVSWR6TUtaVTMyclM1aWpmM0F6Tk94eFBCZjZzamQ1U05RdlR0WTNUZzdFMEFLMkZYVFE9PSIsIm1hYyI6Ijg3MjY1ZDM3MDI1ZTRmYTI0ODY3NWYxNDEyM2RkMGRhMWFlY2E3NDFjOGEwMTQ1NzZiMWZmODFkNzZjNzRhYWQifQ%3D%3D; _ga=GA1.4.5501114.1453888035
Host:*****.com
Pragma:no-cache
Host:*****.com
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36

How do I get these images to cache?

like image 844
Dogday Avatar asked Nov 08 '22 21:11

Dogday


1 Answers

Your idea about the lack of caching happening because of the Cache-Control: no-cache request header is correct; that request header tells any possible caches along the way to not use any cached data for the requested resource (see the "End-to-end reload" section of RFC 2616).

To be specific, your response may actually be being cached. But that Cache-Control: no-cache header in the request says that that particular HTTP client does not want to take advantage of the cache; another HTTP client may be able to use the cached resource.

For situations like there, where some HTTP clients refuse to use any cache, I've seen e.g. haproxy used to filter out/rewrite that Cache-Control request header; I suspect that Apache could be used to do the same. In theory such rewriting of request headers behind the client's back, so to speak, is not a good idea. But if you really do need such requests using the cache where available, it may be the expedient solution.

Hope this helps!

like image 69
Castaglia Avatar answered Nov 15 '22 05:11

Castaglia