简体   繁体   English

请求标头与响应标头

[英]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? 该图像由PHP提供,其中我将缓存控制的响应标头设置为适当的值,但是这并不缓存图像,我认为这可能与具有无缓存的缓存控制的请求标头有关? 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? 如何获取这些图像进行缓存?

Your idea about the lack of caching happening because of the Cache-Control: no-cache request header is correct; 您对由于Cache-Control: no-cache而导致缺少缓存的想法是Cache-Control: no-cache 请求标头是正确的; 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 ). 该请求标头会告知所有可能的缓存, 以免对请求的资源使用任何缓存的数据(请参阅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; 但是请求中的Cache-Control: no-cache标头表示特定的HTTP客户端不想利用缓存; another HTTP client may be able to use the cached resource. 另一个HTTP客户端可能能够使用缓存的资源。

For situations like there, where some HTTP clients refuse to use any cache, I've seen eg haproxy used to filter out/rewrite that Cache-Control request header; 对于类似HTTP客户端拒绝使用任何缓存的情况,我已经看到例如 haproxy用于过滤/重写该Cache-Control请求标头; I suspect that Apache could be used to do the same. 我怀疑Apache可以用来做同样的事情。 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! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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