2012-12-20 54 views
4

我正在調試本地主機上的js代碼,我需要阻止瀏覽器緩存文件。我無法使用追加到url的時間戳,因爲它會清除Chrome調試器斷點。Apache服務於舊版本的文件

通常我不必刷新緩存,但每隔一段時間我都會刷新緩存。這是一個很大的問題,因爲我在別處尋找錯誤。我加入這個代碼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> 

有人可以解釋爲什麼阿帕奇會誤認爲是有效的文件或提供一些附加的配置代碼,可以解決這個問題一勞永逸?

<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 

回答

10

當提供靜態文件,Apache的發送ETag頭,這是一樣的東西文件的校驗和。瀏覽器將緩存該文件並記住隨同下一個請求一起發送的ETag。

如果文件更改瀏覽器ETag應該不同,並且Web服務器應該重新發送,當etag相等時,網絡服務器將以304 Not Modified作出響應。 ETag機制比其他緩存頭具有更高的優先級。

要禁用的ETag可以使用阿帕奇

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

謝謝,但它不按照你的意圖工作(我仍然得到304頭)。我甚至還嘗試添加 頭未設置附註 頭未設置ETag的 FileETag無 – user1122069

+0

刪除瀏覽器緩存一次,並確保ETag的實際上是不再被Apache(正確配置)設置 –

+0

的eTag肯定是由Apache的發送。我不知道如何糾正配置,因爲我像你說的和其他教程所說的那樣將這些行添加到我的虛擬主機文件中。 – user1122069

1

如果我正確理解你的要求,你想要的網頁瀏覽器不記得你正在訪問的網頁和你的Apache Web服務器應該把它像一個新的一頁什麼:使用下面的解決方案

頁眉請求。你可能首先要啓用指定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頭插件,或者如果您的Linux/Unix您可以運行申請測試這種捲曲-v YOUR_URL

+0

我與liveheaders測試此和所附調試輸出。在第二次嘗試時,文件從緩存中加載。 – user1122069