標籤:
http://blog.hehehehehe.cn/a/10994.htm
用戶端通過瀏覽器發出第一次請求某一個URL時,根據 HTTP 協議的規定,瀏覽器會向伺服器傳送前序(Http Request Header),伺服器端響應同時記錄相關屬性標記(Http Reponse Header),伺服器端的返回狀態會是200,格式類似如下:1. HTTP/1.1 200 OK2. Date: Tue, 03 Mar 2012 04:58:40 GMT3. Content-Type: image/jpeg4. Content-Length: 831855. Last-Modified: Tue, 24 Feb 2012 08:01:04 GMT6. Cache-Control: max-age=25920007. Expires: Thu, 02 Apr 2012 05:14:08 GMT8. Etag: “5d8c72a5edda8d6a:3239″用戶端第二次請求此URL時,根據 HTTP 協議的規定,瀏覽器會向伺服器傳送前序(Http Request Header),伺服器端響應並記錄相關記錄屬性標記檔案沒有發生改動,伺服器端返回304,直接從緩衝中讀取:1. HTTP/1.x 304 Not Modified2. Date: Tue, 03 Mar 2012 05:03:56 GMT3. Content-Type: image/jpeg4. Content-Length: 831855. Last-Modified: Tue, 24 Feb 2012 08:01:04 GMT6. Cache-Control: max-age=25920007. Expires: Thu, 02 Apr 2012 05:14:08 GMT8. Etag: "5d8c72a5edda8d6a:3239"9. //其中Last-Modified、Expires和Etag是標記頁面緩衝標識一、Last-Modified、Expires和Etag相關工作原理 1、Last-Modified 在瀏覽器第一次請求某一個URL時,伺服器端的返回狀態會是200,內容是你請求的資源,同時有一個Last-Modified的屬性標記(Http Reponse Header)此檔案在服務期端最後被修改的時間,格式類似這樣: Last-Modified: Tue, 24 Feb 2012 08:01:04 GMT 用戶端第二次請求此URL時,根據 HTTP 協議的規定,瀏覽器會向伺服器傳送 If-Modified-Since 前序(Http Request Header),詢問該時間之後檔案是否有被修改過: If-Modified-Since: Tue, 24 Feb 2012 08:01:04 GMT 如果伺服器端的資源沒有變化,則自動返回 HTTP 304 (Not Changed.)狀態代碼,內容為空白,這樣就節省了傳輸資料量。當伺服器端代碼發生改變或者重啟伺服器時,則重新發出資源,返回和第一次請求時類似。從而保證不向用戶端重複發出資源,也保證當伺服器有變化時,用戶端能夠得到最新的資源。 註:如果If-Modified-Since的時間比伺服器目前時間(當前的請求時間request_time)還晚,會認為是個非法請求 2、Etag工作原理 HTTP 協議規格說明定義ETag為“被請求變數的實體標記” 簡單點即伺服器響應時給請求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 Request Header)此時包頭中資訊包含上次訪問得到的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 2012 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對應的Http Request Header:If-Modified-Since和If-None-Match。我們可以看到這兩個Header的值和Web Server發出的Last-Modified,Etag值完全一樣;在完全符合If-Modified-Since和If-None-Match即檢查完修改時間和Etag之後,伺服器才能返回304.6、Last-Modified和EtagLast-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 Request Header:If-Modified-Since和If-None-Match。我們可以看到這兩個Header的值和Web Server發出的Last-Modified,Etag值完全一樣;4、通過上述值到伺服器端檢查,判斷檔案是否繼續緩衝;二、Apache、Lighttpd和Nginx中針配置Etag和Expires,有效緩衝純靜態如css/js/pic/頁面/流媒體等檔案。A、ExpiresA.1、Apache Etag使用Apache的mod_expires 模組來設定,這包括控制應答時的Expires頭內容和Cache-Control頭的max-age指令1. ExpiresActive On2. ExpiresByType image/gif “access plus 1 month”3. ExpiresByType image/jpg “access plus 1 month”4. ExpiresByType image/jpeg “access plus 1 month”5. ExpiresByType image/x-icon “access plus 1 month”6. ExpiresByType image/bmp “access plus 1 month”7. ExpiresByType image/png “access plus 1 month”8. ExpiresByType text/html “access plus 30 minutes”9. ExpiresByType text/css “access plus 30 minutes”10. ExpiresByType text/txt “access plus 30 minutes”11. ExpiresByType text/js ”access plus 30 minutes”12. ExpiresByType application/x-javascript ”access plus 30 minutes”13. ExpiresByType application/x-shockwave-flash ”access plus 30 minutes”或1. <ifmodule mod_expires.c>2. <filesmatch “.(jpg|gif|png|css|js)$”>3. ExpiresActive on4. ExpiresDefault “access plus 1 year”5. </filesmatch>6. </ifmodule>當設定了expires後,會自動輸出Cache-Control 的max-age 資訊具體關於 Expires 詳細內容可以查看Apache官方文檔。在這個時間段裡,該檔案的請求都將直接通過快取服務器擷取,當然如果需要忽略瀏覽器的重新整理請求(F5),快取服務器squid還需要使用 refresh_pattern 選項來忽略該請求1. refresh_pattern -i .gif$ 1440 100% 28800 ignore-reload2. refresh_pattern -i .jpg$ 1440 100% 28800 ignore-reload3. refresh_pattern -i .jpeg$ 1440 100% 28800 ignore-reload4. refresh_pattern -i .png$ 1440 100% 28800 ignore-reload5. refresh_pattern -i .bmp$ 1440 100% 28800 ignore-reload6. refresh_pattern -i .htm$ 60 100% 100 ignore-reload7. refresh_pattern -i .html$ 1440 50% 28800 ignore-reload8. refresh_pattern -i .xml$ 1440 50% 28800 ignore-reload9. refresh_pattern -i .txt$ 1440 50% 28800 ignore-reload10. refresh_pattern -i .css$ 1440 50% 28800 reload-into-ims11. refresh_pattern -i .js$ 60 50% 100 reload-into-ims12. refresh_pattern . 10 50% 60有關Squid中Expires的說明,請參考Squid官方中refresh_pattern介紹。A.2、Lighttpd Expires和Apache一樣Lighttpd設定expire也要先查看是否支援了mod_expire模組,下面的設定是讓URI中所有images目錄下的檔案1小時後到期;1. expire.url = ( “/images/” => “access 1 hours” )下面是讓作用於images目錄及其子目錄的檔案;1. $HTTP["url"] =~ “^/images/” {2. expire.url = ( “” => “access 1 hours” )3. }也可以指定檔案的類型;1. $HTTP["url"] =~ “.(jpg|gif|png|css|js)$” {2. expire.url = ( “” => “access 1 hours” )3. }具體參考Lighttpd官方Expires解釋A.3、Nginx中Expires1. location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$2. {3. expires 30d;4. }5. location ~ .*.(js|css)?$6. {7. expires 1h;8. }這類檔案並不常修改,通過 expires 指令來控制其在瀏覽器的緩衝,以減少不必要的請求。 expires 指令可以控制 HTTP 應答中的“ Expires ”和“ Cache-Control ”的頭標(起到控制頁面緩衝的作用)。其他請參考Nginx中ExpiresB.1、Apache中Etag設定在Apache中設定Etag的支援比較簡單,只用在含有靜態檔案的目錄中建立一個檔案.htaccess, 裡面加入:FileETag MTime Size這樣就行了,詳細的可以參考Apache的FileEtag文檔頁B.2、Lighttpd Etag在Lighttpd中設定Etag支援:etag.use-inode: 是否使用inode作為Etagetag.use-mtime: 是否使用檔案修改時間作為Etagetag.use-size: 是否使用檔案大小作為Etagstatic-file.etags: 是否啟用Etag的功能第四個參數肯定是要enable的, 前面三個就看實際的需要來選吧,推薦使用修改時間B.3、 Nginx EtagNginx中預設沒有添加對Etag標識. Igor Sysoev的觀點”在對靜態檔案處理上看不出如何Etag好於Last-Modified標識。”Note:Yes, it’s addition,and it’s easy to add, however, I do not see how ETag is better than Last-Modified for static files. -Igor SysoevA nice short description is here:http://www.mnot.net/cache_docs/#WORKIt looks to me that it makes some caches out there to cache the response 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.php1. <?php2. header(’Cache-Control: max-age=86400,must-revalidate’);3. header(’Last-Modified: ‘ .gmdate(’D, d M Y H:i:s’) . ‘ GMT’ );4. header(”Expires: ” .gmdate (’D, d M Y H:i:s’, time() + ‘86400′ ). ‘ GMT’);5. ?>以上資訊表示該檔案自請求後24小時後到期。其他需要處理的動態網頁面直接調用即可。3.2、Etag根據Http返回狀態來處理。當返回304直接從緩衝中讀取如etag.php1. <?php2. cache();3. echo date(”Y-m-d H:i:s”);4. function cache()5. {6. $etag = “http://longrujun.name”;7. if ($_SERVER[‘HTTP_IF_NONE_MATCH‘] == $etag)8. {9. header(’Etag:’.$etag,true,304);10. exit;11. }12. else header(’Etag:’.$etag);13. }14. ?> [喝小酒的網摘]http://blog.hehehehehe.cn/a/10994.htm原文:http://www.phpzixue.cn/detail1181.shtml[喝小酒的網摘]http://blog.hehehehehe.cn/a/10994.htm
轉載自 [blog.hehehehehe.cn]喝小酒的網摘 及本文連結地址:http://blog.hehehehehe.cn/a/10994.htm
相關文章
- 簡單說說PHP最佳化
- Apache中關於頁面緩衝的設定
- apache中PHP利用HTTP緩衝協議原理實現緩衝
- 用表格讓兩個廣告代碼並列放在同一行顯示
- 讓兩個百度廣告代碼用DIV並列放在同一行顯示
訪問來源#
- IP:124.126.244.30:http+rfc+expires+瀏覽器
- IP:58.67.143.105:java Last-Modified和Etags
用PHP操作http中Etag、lastModified和Expires標籤