Apache提供旧版本的文件 [英] Apache serves old versions of files

查看:68
本文介绍了Apache提供旧版本的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在本地主机上调试js代码,并且需要防止浏览器缓存文件.我无法在网址上附加时间戳,因为它会删除Chrome调试器的断点.

I am debugging js code on localhost and I need to prevent the caching of files by the browser. I can't use a timestamp appended to the url because it erases chrome debugger breakpoints.

通常,我不必刷新缓存,但是每隔一段时间我都会刷新.这是一个很大的问题,因为我要在其他地方搜索错误.我在一段时间前将以下代码添加到了Apache:

Usually I don't have to refresh the cache, but everyone in a while I do. It is a large problem because I go searching elsewhere for the bugs. I added this code to apache some time ago:

    <IfModule mod_headers.c>
            Header add Expires "Sun, 19 Nov 1978 05:00:00 GMT"
            Header add Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
    </IfModule>

有人可以解释为什么Apache会错误地将文件视为有效文件,或者为配置代码提供一些附加内容,从而可以一劳永逸地解决该问题吗?

Can someone explain why Apache would mistake a file for valid or provide some additions to the configuration code that could fix this once and for all?

标题使用以下解决方案:

Headers using the solution below:

<IfModule mod_expires.c>
  expiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType text/html "access plus 1 seconds"
  ExpiresByType text/javascript "access plus 1 seconds"
  ExpiresByType application/x-javascript "access plus 1 seconds"
</IfModule>

http://localhost/static/images/%d0%9a%d0%be%d0%bf%d0%b8%d1%8f%20logo_inner.png

GET /static/images/%d0%9a%d0%be%d0%bf%d0%b8%d1%8f%20logo_inner.png HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://localhost/static/images/
Cache-Control: max-age=0

HTTP/1.1 200 OK
Date: Sun, 23 Dec 2012 19:33:20 GMT
Server: Apache/2.2.22 (Ubuntu)
Last-Modified: Thu, 28 Jun 2012 17:32:51 GMT
Etag: "b3c27-f1f-4c38bb88d96c0"
Accept-Ranges: bytes
Content-Length: 3871
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Content-Type: image/png



HTTP/1.1 200 OK
Date: Sun, 23 Dec 2012 19:33:54 GMT
Server: Apache/2.2.22 (Ubuntu)
Last-Modified: Thu, 28 Jun 2012 17:32:51 GMT
Etag: "b3c27-f1f-4c38bb88d96c0"
Accept-Ranges: bytes
Content-Length: 3871
Cache-Control: max-age=1
Expires: Sun, 23 Dec 2012 19:33:55 GMT
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: image/png




The second request:

http://localhost/static/images/%d0%9a%d0%be%d0%bf%d0%b8%d1%8f%20logo_inner.png

GET /static/images/%d0%9a%d0%be%d0%bf%d0%b8%d1%8f%20logo_inner.png HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://localhost/static/images/
If-Modified-Since: Thu, 28 Jun 2012 17:32:51 GMT
If-None-Match: "b3c27-f1f-4c38bb88d96c0"
Cache-Control: max-age=0

HTTP/1.1 304 Not Modified
Date: Sun, 23 Dec 2012 19:34:58 GMT
Server: Apache/2.2.22 (Ubuntu)
Connection: Keep-Alive
Keep-Alive: timeout=15, max=99
Etag: "b3c27-f1f-4c38bb88d96c0"
Expires: Sun, 23 Dec 2012 19:34:59 GMT
Cache-Control: max-age=1

推荐答案

在交付静态文件时,Apache发送一个ETag头,类似于文件的校验和.浏览器将缓存文件并记住ETag,该ETag与下一个请求一起发送.

When delivering static files, Apache sends an ETag header, which is something like a checksum of the file. The browser will cache the file and remember the ETag, which is sent with the next request.

如果文件更改,则浏览器的ETag应该有所不同,并且网络服务器应重新发送,当etag相等时,网络服务器将以 304 Not Modified 进行响应.ETag机制具有比其他缓存头更高的优先级.

If the file changes the browser ETag should differ and the webserver should resend, when the etag is equal, the webserver will respond with 304 Not Modified. The ETag mechanism has a higher priority than other cache headers.

要禁用etag,您可以使用apches

To disable etags you can use apaches

FileETag None

http://httpd.apache.org/docs/current/en/mod/core.html#fileetag

维基百科上有一篇关于Etag标头的好文章 http://en.wikipedia.org/wiki/HTTP_ETag

Wikipedia has a nice article about the Etag header http://en.wikipedia.org/wiki/HTTP_ETag

修改

这应该是防水配置

FileETag None
<ifModule mod_headers.c>
    Header unset ETag
    Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>

别忘了配置更改需要重新启动服务器才能生效.

Don't forget that configuration changes require a server restart to take effect.

sudo /etc/init.d/httpd restart

EDIT2

环绕文件匹配配置以仅对特定文件扩展名禁用缓存

Wrap filesMatch around the configuration to disable caching for specific file extensions only

<filesMatch ".(php|js|css)$">
    FileETag None
    [..]
</filesMatch>

这篇关于Apache提供旧版本的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆