歡迎您光臨本站 註冊首頁

Etag和Expires

←手機掃碼閱讀     火星人 @ 2014-03-03 , reply:0

Etag和Expires

Etag和Expires


題記:本文對頁面中Etag和Expires標識處理,使得頁面更加有效被Cache。
原版來源: http://phaedo.cx/archives/2007/0 ... pache-and-lighttpd/
摘要
1、Etag和Expires中Client 端Http Request Header及Server端Http Reponse Header工作原理。
2、靜態下Apache、Lighttpd和Nginx中Etag和Expires配置
3、非實時交互動態頁面中Etag和Expires處理
在客戶端通過瀏覽器發出第一次請求某一個URL時,根據 HTTP 協議的規定,瀏覽器會向伺服器傳送報頭(Http Request Header),伺服器端響應同時記錄相關屬性標記(Http Reponse Header),伺服器端的返回狀態會是200,格式類似如下:
HTTP/1.1 200 OK
Date: Tue, 03 Mar 2009 04:58:40 GMT
Content-Type: image/jpeg
Content-Length: 83185
Last-Modified: Tue, 24 Feb 2009 08:01:04 GMT
Cache-Control: max-age=2592000

Expires: Thu, 02 Apr 2009 05:14:08 GMT
Etag: 「5d8c72a5edda8d6a:3239″

客戶端第二次請求此URL時,根據 HTTP 協議的規定,瀏覽器會向伺服器傳送報頭(Http Request Header),伺服器端響應並記錄相關記錄屬性標記文件沒有發生改動,伺服器端返回304,直接從緩存中讀取:
HTTP/1.x 304 Not Modified
Date: Tue, 03 Mar 2009 05:03:56 GMT
Content-Type: image/jpeg
Content-Length: 83185
Last-Modified: Tue, 24 Feb 2009 08:01:04 GMT
Cache-Control: max-age=2592000
Expires: Thu, 02 Apr 2009 05:14:08 GMT
Etag: 「5d8c72a5edda8d6a:3239″
其中Last-Modified、Expires和Etag是標記頁面緩存標識

一、Last-Modified、Expires和Etag相關工作原理
1、Last-Modified
在瀏覽器第一次請求某一個URL時,伺服器端的返回狀態會是200,內容是你請求的資源,同時有一個Last-Modified的屬性標記(Http Reponse Header)此文件在服務期端最後被修改的時間,格式類似這樣:
Last-Modified: Tue, 24 Feb 2009 08:01:04 GMT
客戶端第二次請求此URL時,根據 HTTP 協議的規定,瀏覽器會向伺服器傳送 If-Modified-Since 報頭(Http Request Header),詢問該時間之後文件是否有被修改過:
If-Modified-Since: Tue, 24 Feb 2009 08:01:04 GMT
如果伺服器端的資源沒有變化,則自動返回 HTTP 304 (NotChanged.)狀態碼,內容為空,這樣就節省了傳輸數據量。當伺服器端代碼發生改變或者重啟伺服器時,則重新發出資源,返回和第一次請求時類似。從而保證不向客戶端重複發出資源,也保證當伺服器有變化時,客戶端能夠得到最新的資源。
註:如果If-Modified-Since的時間比伺服器當前時間(當前的請求時間request_time)還晚,會認為是個非法請求

2、Etag工作原理
HTTP 協議規格說明定義ETag為「被請求變數的實體標記」 (參見14.19)。簡單點即伺服器響應時給請求URL標記,並在HTTP響應頭中將其傳送到客戶端,類似伺服器端返回的格式:
Etag: 「5d8c72a5edda8d6a:3239″
客戶端的查詢更新格式是這樣的:
If-None-Match: 「5d8c72a5edda8d6a:3239″
如果ETag沒改變,則返回狀態304。
即:在客戶端發出請求后,Http Reponse Header中包含 Etag: 「5d8c72a5edda8d6a:3239″
標識,等於告訴Client端,你拿到的這個的資源有表示ID:5d8c72a5edda8d6a:3239。當下次需要發Request索要同一個URI的時候,瀏覽器同時發出一個If-None-Match報頭( Http RequestHeader)此時包頭中信息包含上次訪問得到的Etag: 「5d8c72a5edda8d6a:3239″標識。
If-None-Match: 「5d8c72a5edda8d6a:3239「
,這樣,Client端等於Cache了兩份,伺服器端就會比對2者的etag。如果If-None-Match為False,不返回200,返回304 (Not Modified) Response。

3、Expires
給出的日期/時間后,被響應認為是過時。如Expires: Thu, 02 Apr 2009 05:14:08 GMT
需和Last-Modified結合使用。用於控制請求文件的有效時間,當請求數據在有效期內時客戶端瀏覽器從緩存請求數據而不是伺服器端. 當緩存中數據失效或過期,才決定從伺服器更新數據。

4、Last-Modified和Expires
Last-Modified標識能夠節省一點帶寬,但是還是逃不掉發一個HTTP請求出去,而且要和Expires一起用。而Expires標識卻使得瀏覽器乾脆連HTTP請求都不用發,比如當用戶F5或者點擊Refresh按鈕的時候就算對於有Expires的URI,一樣也會發一個HTTP請求出去,所以,Last-Modified還是要用的,而 且要和Expires一起用。

5、Etag和Expires
如果伺服器端同時設置了Etag和Expires時,Etag原理同樣,即與Last-Modified/Etag對應的HttpRequest Header:If-Modified-Since和If-None-Match。我們可以看到這兩個Header的值和WebServer發出的Last-Modified,Etag值完全一樣;在完全匹配If-Modified-Since和If-None-Match即檢查完修改時間和Etag之後,伺服器才能返回304.

6、Last-Modified和Etag
Last-Modified 和ETags請求的http報頭一起使用,伺服器首先產生 Last-Modified/Etag標記,伺服器可在稍後使用它來判斷頁面是否已經被修改,來決定文件是否繼續緩存
過程如下:
1. 客戶端請求一個頁面(A)。
2. 伺服器返回頁面A,並在給A加上一個Last-Modified/ETag。
3. 客戶端展現該頁面,並將頁面連同Last-Modified/ETag一起緩存。
4. 客戶再次請求頁面A,並將上次請求時伺服器返回的Last-Modified/ETag一起傳遞給伺服器。
5. 伺服器檢查該Last-Modified或ETag,並判斷出該頁面自上次客戶端請求之後還未被修改,直接返迴響應304和一個空的響應體。
註:
1、Last-Modified和Etag頭都是由Web Server發出的Http Reponse Header,Web Server應該同時支持這兩種頭。
2、Web Server發送完Last-Modified/Etag頭給客戶端后,客戶端會緩存這些頭;
3、客戶端再次發起相同頁面的請求時,將分別發送與Last-Modified/Etag對應的Http RequestHeader:If-Modified-Since和If-None-Match。我們可以看到這兩個Header的值和WebServer發出的Last-Modified,Etag值完全一樣;
4、通過上述值到伺服器端檢查,判斷文件是否繼續緩存;

二、Apache、Lighttpd和Nginx中針配置Etag和Expires,有效緩存純靜態如css/js/pic/頁面/流媒體等文件。
A、Expires
A.1、Apache Etag
使用Apache的mod_expires 模塊來設置,這包括控制應答時的Expires頭內容和Cache-Control頭的max-age指令
ExpiresActive On
ExpiresByType image/gif 「access plus 1 month」
ExpiresByType image/jpg 「access plus 1 month」
ExpiresByType image/jpeg 「access plus 1 month」
ExpiresByType image/x-icon 「access plus 1 month」
ExpiresByType image/bmp 「access plus 1 month」
ExpiresByType image/png 「access plus 1 month」
ExpiresByType text/html 「access plus 30 minutes」
ExpiresByType text/css  「access plus 30 minutes」
ExpiresByType text/txt  「access plus 30 minutes」
ExpiresByType text/js   」access plus 30 minutes」
ExpiresByType application/x-javascript   」access plus 30 minutes」
ExpiresByType application/x-shockwave-flash     」access plus 30 minutes」


ExpiresActive on
ExpiresDefault 「access plus 1 year」

當設置了expires后,會自動輸出Cache-Control 的max-age 信息
具體關於 Expires 詳細內容可以查看Apache官方文檔。
在這個時間段里,該文件的請求都將直接通過緩存伺服器獲取,
當然如果需要忽略瀏覽器的刷新請求(F5),緩存伺服器squid還需要使用 refresh_pattern 選項來忽略該請求
refresh_pattern -i \.gif$ 1440 100% 28800 ignore-reload
refresh_pattern -i \.jpg$ 1440 100% 28800 ignore-reload
refresh_pattern -i \.jpeg$ 1440 100% 28800 ignore-reload
refresh_pattern -i \.png$ 1440 100% 28800 ignore-reload
refresh_pattern -i \.bmp$ 1440 100% 28800 ignore-reload
refresh_pattern -i \.htm$ 60 100% 100 ignore-reload
refresh_pattern -i \.html$ 1440 50% 28800 ignore-reload
refresh_pattern -i \.xml$ 1440 50% 28800 ignore-reload
refresh_pattern -i \.txt$ 1440 50% 28800 ignore-reload
refresh_pattern -i \.css$ 1440 50% 28800 reload-into-ims
refresh_pattern -i \.js$ 60 50% 100 reload-into-ims
refresh_pattern . 10 50% 60
有關Squid中Expires的說明,請參考Squid官方中refresh_pattern介紹。

A.2、Lighttpd Expires
和Apache一樣Lighttpd設置expire也要先查看是否支持了mod_expire模塊,
下面的設置是讓URI中所有images目錄下的文件1小時後過期;
expire.url = ( 「/images/」 => 「access 1 hours」 )
下面是讓作用於images目錄及其子目錄的文件;$HTTP["url"] =~ 「^/images/」 {
expire.url = ( 「」 => 「access 1 hours」 )
}也可以指定文件的類型;$HTTP["url"] =~ 「\.(jpg|gif|png|css|js)$」 {
expire.url = ( 「」 => 「access 1 hours」 )
}
具體參考Lighttpd官方Expires解釋

A.3、Nginx中Expires
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}這類文件並不常修改,通過 expires 指令來控制其在瀏覽器的緩存,以減少不必要的請求。 expires 指令可以控制 HTTP 應答中的「 Expires 」和「 Cache-Control 」的頭標(起到控制頁面緩存的作用)。其他請參考Nginx中Expires

B.1、Apache中Etag設置
在Apache中設置Etag的支持比較簡單,只用在含有靜態文件的目錄中建立一個文件.htaccess, 裡面加入:
FileETag MTime Size
這樣就行了,詳細的可以參考Apache的FileEtag文檔頁

B.2、Lighttpd Etag
在Lighttpd中設置Etag支持:
etag.use-inode: 是否使用inode作為Etag
etag.use-mtime: 是否使用文件修改時間作為Etag
etag.use-size: 是否使用文件大小作為Etag
static-file.etags: 是否啟用Etag的功能
第四個參數肯定是要enable的, 前面三個就看實際的需要來選吧,推薦使用修改時間

B.3、 Nginx Etag
Nginx中默認沒有添加對Etag標識.Igor Sysoev的觀點」在對靜態文件處理上看不出如何Etag好於Last-Modified標識。」
Note:
Yes, it』s addition,and it』s easy to add, however, I do not see howETag is better than Last-Modified for static files. -Igor Sysoev
A nice short description is here:
http://www.mnot.net/cache_docs/#WORK
It looks to me that it makes some caches out there to cache theresponse from the origin server more reliable as in rfc2616(ftp://ftp.rfc-editor.org/in-notes/rfc2616.txt) is written.
3.11 Entity Tags 13.3.2 Entity Tag Cache Validators 14.19 ETag
當然也有第三方nginx-static-etags 模塊了,請參考
http://mikewest.org/2008/11/generating-etags-for-static-content-using-nginx

三、對於非實時交互動態頁面中Epires和Etag處理
對數據更新並不頻繁、如tag分類歸檔等等,可以考慮對其cache。簡單點就是在非實時交互的動態程序中輸出expires和etag標識,讓其緩存。但需要注意關閉session,防止http response時http header包含session id標識;
3.1、Expires
如expires.php<?php
header(』Cache-Control: max-age=86400,must-revalidate』);
header(』Last-Modified: 『 .gmdate(』D, d M Y H:i:s』) . 『 GMT』 );
header(」Expires: 」 .gmdate (』D, d M Y H:i:s』, time() + 『86400′ ). 『 GMT』);
?>以上信息表示該文件自請求后24小時後過期。
其他需要處理的動態頁面直接調用即可。
3.2、Etag
根據Http返回狀態來處理。當返回304直接從緩存中讀取
如etag.php<?php
cache();
echo date(」Y-m-d H:i:s」);
function cache()
{
$etag = 「http://longrujun.name」;
if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag)
{
header(』Etag:』.$etag,true,304);
exit;
}
else header(』Etag:』.$etag);
}
?>來自: http://longrujun.name/index.php/2009/03/04/etag%E5%92%8Cexpires/
《解決方案》

謝謝分享

[火星人 ] Etag和Expires已經有617次圍觀

http://coctec.com/docs/service/show-post-1111.html