简体   繁体   English

如何停止在Apache服务器上缓存图像

[英]How to stop caching of images on apache server

I have been looking everywhere to solve the caching problem on a part of my website that requires the most current version of a file. 我一直到处寻找解决网站部分需要最新版本文件的缓存问题的方法。 This file is a background image that can be uploaded by the user. 此文件是用户可以上传的背景图片。 It uses a form to upload an image to a certain folder on the server. 它使用一种形式将图像上载到服务器上的某个文件夹。 Everytime the user's page is loaded the php script echos CSS into the head tag of the page. 每次加载用户页面时,php脚本都会将CSS回显到页面的head标签中。

I know about tricks like appending a random string or time stamp to the end of the source file in the src attribute of an image. 我知道一些技巧,例如在图像的src属性中将随机字符串或时间戳添加到源文件的末尾。 However, in my case there is no img tag. 但是,就我而言,没有img标签。 The background image is applied to a wrapper div via css: 通过CSS将背景图片应用于包装器div:

#wrapper{
   background-image:url('dir/imagename.jpg');
}

In terms of stopping the browser caching I have tried the following: 在停止浏览器缓存方面,我尝试了以下方法:

 <meta Http-Equiv="Cache-Control" Content="no-cache"> <meta Http-Equiv="Pragma" Content="no-cache"> <meta Http-Equiv="Expires" Content="0"> <meta Http-Equiv="Pragma-directive: no-cache"> <meta Http-Equiv="Cache-directive: no-cache"> 

That code has been added in to the head tag of the page. 该代码已添加到页面的head标签中。

I have also tried this with php: 我也尝试过用php:

 header("Expires: Tue, 01 Jan 2000 00:00:00 GMT"); header("Last-Modified: " . gmdate("D, d MYH:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); 

Even after all these changes, I have to refresh the page at least a dozen times for the image to update. 即使进行了所有这些更改,我也必须刷新页面至少十二次才能更新图像。 When downloading the image via ftp client or built in file manager I can see that the image has been updated instantly. 通过ftp客户端或内置文件管理器下载映像时,我可以看到映像已立即更新。 So, I conclude that the browser (Google Chrome) is caching these images and I can't seem to interrupt or prevent this process. 因此,我得出的结论是,浏览器(Google Chrome)正在缓存这些图像,而我似乎无法中断或阻止此过程。

Any ideas? 有任何想法吗?

You need to "uncache" the static assets like images, CSS and JavaScript. 您需要“取消缓存”图像,CSS和JavaScript等静态资产。 I don't know how you are sending the headers for those, coz they don't work that way. 我不知道您是如何发送这些标头的,因为它们不能那样工作。 Using .htaccess add this rule to tell browsers not to cache .jpg , .jpeg , .png , .gif : 使用.htaccess添加此规则,以告诉浏览器不要缓存.jpg.jpeg.png.gif

<FilesMatch "\.(jpg|jpeg|png|gif)$">
Header set Expires "Tue, 01 Jan 2000 00:00:00 GMT"
</FilesMatch>

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

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