澄清使用“Cache-Control”头 [英] Clarification on usage of "Cache-Control" header
问题描述
我们正在努力改善网站的网页加载时间。除了其他一些优化外,我还添加了一个servlet过滤器来将 Cache-Control 标头设置为图像文件(png,gif,jpg)。正如所料,图像文件现在缓存在客户端,我可以通过检查临时文件夹(IE)来验证每个文件的过期时间。我的问题是,浏览器仍然请求静态资源并获得 HTTP304 (未修改)响应。我的意思是,实际上我所取得的成绩是我减少了传输的字节数,但没有减少网络往返数(在我的情况下速度很慢)。有没有其他的头/方法可以阻止浏览器检查服务器是否修改了资源?
@chedine ,b
$ b浏览器缓存的文件不应该返回304,他们应该返回200.你应该使用mod_expires来设置max age和mod_headers来设置Cache-控制标题。下面是一个为浏览器缓存的静态文件返回有效200响应的示例:
< IfModule mod_expires.c>
ExpiresActive On
ExpiresByType文本/ css M5184000
ExpiresByType应用程序/ x-javascript M5184000
ExpiresByType text / html M1200
ExpiresByType text / plain M1200
ExpiresByType image / gif M31536000
ExpiresByType application / x-gzip M31536000
ExpiresByType image / x-icon M31536000
ExpiresByType image / jpeg M31536000
ExpiresByType image / png M31536000
< IfModule mod_headers.c>
Header setAccept-Encoding,Cookie
Header set Pragmapublic
Header append Cache-Controlpublic,must-revalidate,proxy-revalidate
< / IfModule>
< / FilesMatch>
We are trying to improve the page load time of a site. In addition to the few other optimizations, I have added a servlet filter to set "Cache-Control" headers to image files(png,gif,jpg). As expected,the image files are now getting cached at the client end and I could verify the expires time of each file by inspecting the temp folder(IE).My question is, browsers still request the static resources and gets a HTTP304 (Not modified) response. I mean,in effect what i have achieved is I have reduced the no of bytes transferred but not the number of network roundtrips (Which in my case is little slow). Is there any other header/way to stop browsers checking the server whether the resource has been modified?
@chedine,
Browser cached files should not return a 304 they should return a 200. You should use mod_expires to set the max age and mod_headers to set the Cache-Control headers. Here is an example that returns a valid 200 response for browser cached static files:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css M5184000
ExpiresByType application/x-javascript M5184000
ExpiresByType text/html M1200
ExpiresByType text/plain M1200
ExpiresByType image/gif M31536000
ExpiresByType application/x-gzip M31536000
ExpiresByType image/x-icon M31536000
ExpiresByType image/jpeg M31536000
ExpiresByType image/png M31536000
</IfModule>
<FilesMatch "\.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$">
<IfModule mod_headers.c>
Header set Vary "Accept-Encoding, Cookie"
Header set Pragma "public"
Header append Cache-Control "public, must-revalidate, proxy-revalidate"
</IfModule>
</FilesMatch>
这篇关于澄清使用“Cache-Control”头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!