Apache提供旧版本的文件

时间:2012-12-20 20:40:33

标签: apache2 cache-control

我正在调试localhost上的js代码,我需要阻止浏览器缓存文件。我不能使用附加到网址的时间戳,因为它会删除chrome调试程序断点。

通常我不需要刷新缓存,但我会做一段时间。这是一个很大的问题,因为我去别处寻找bug。我前段时间将此代码添加到apache中:

    <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会误认为某个文件有效,或者为配置代码提供一些可以一劳永逸地解决这个问题的补充吗?

标题使用以下解决方案:

<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

2 个答案:

答案 0 :(得分:10)

在传递静态文件时,Apache会发送一个ETag标头,这类似于文件的校验和。浏览器将缓存文件并记住ETag,它随下一个请求一起发送。

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

要禁用etags,您可以使用apaches

FileETag None

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

维基百科有一篇关于Etag标题的好文章 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>

不要忘记配置更改需要重启服务器才能生效。

sudo /etc/init.d/httpd restart

<强> EDIT2

围绕配置包装filesMatch以仅禁用特定文件扩展名的缓存

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

答案 1 :(得分:1)

如果我正确理解您的要求,您希望网络浏览器不记得您正在访问的网页的任何内容,并且您的Apache网络服务器应将其视为新页面请求。您可能首先要启用mod_expires和mod_headers,我使用ubuntu以便我的

a2enmod headers && a2enmod expires && service apache2 restart

比你想要添加下面的代码来做最小的缓存控制,

<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>

如果您使用的是firefox,可以通过安装/运行Live Http header Plugin进行测试,如果您是linux / unix,则可以使用 curl -v your_url

运行此请求