HTTP中cache-control的應用及說明

網頁的緩存是由http消息頭中的“Cache-control”來控制的,常見的取值有private、no-cache、max-age、must-revalidate等,默認為private。其作用根據不同的重新瀏覽方式分為以下幾種情況:

  • 打開新窗口

值為private、no-cache、must-revalidate,那么打開新窗口訪問時都會重新訪問服務器。

而如果指定了max-age值,那么在此值內的時間里就不會重新訪問服務器,例如:

Cache-control: max-age=5(表示當訪問此網頁后的5秒 內再次訪問不會去服務器)

  • 在地址欄回車

值為private或must-revalidate則只有第一次訪問時會訪問服務器,以后就不再訪問。

值為no-cache,那么每次都會訪問。

值為max-age,則在過期之前不會重復訪問。

  • 按后退按扭

值為private、must-revalidate、max-age,則不會重訪問,

值為no-cache,則每次都重復訪問

  • 按刷新按扭

無論為何值,都會重復訪問

Cache-control值為“no-cache”時,訪問此頁面不會在Internet臨時文章夾留下頁面備份。

另外,通過指定“Expires”值也會影響到緩存。例如,指定Expires值為一個早已過去的時間,那么訪問此網時若重復在地址欄按回車,那么每次都會重復訪問: Expires: Fri, 31 Dec 1999 16:00:00 GMT

比如:禁止頁面在IE中緩存

http響應消息頭部設置:

CacheControl = no-cache
Pragma=no-cache
Expires = -1

Expires是個好東東,如果服務器上的網頁經常變化,就把它設置為-1,表示立即過期。如果一個網頁每天凌晨1點更新,可以把Expires設置為第二天的凌晨1點。

當HTTP1.1服務器指定CacheControl = no-cache時,瀏覽器就不會緩存該網頁。

舊式 HTTP 1.0 服務器不能使用 Cache-Control 標題。

所以為了向后兼容 HTTP 1.0 服務器,IE使用Pragma:no-cache 標題對 HTTP 提供特殊支持。

如果客戶端通過安全連接 (https://)與服務器通訊,且服務器在響應中返回 Pragma:no-cache 標題,

則 Internet Explorer不會緩存此響應。注意:Pragma:no-cache 僅當在安全連接中使用時才防止緩存,如果在非安全頁中使用,處理方式與 Expires:-1相同,該頁將被緩存,但被標記為立即過期
header常用指令

header分為三部分:
第一部分為HTTP協議的版本(HTTP-Version);

第二部分為狀態代碼(Status);

第三部分為原因短語(Reason-Phrase)。

// fix 404 pages:   用這個header指令來解決URL重寫產生的404 header
header('HTTP/1.1 200 OK');
// set 404 header: 頁面沒找到
header('HTTP/1.1 404 Not Found');
//頁面被永久刪除,可以告訴搜索引擎更新它們的urls 

// set Moved Permanently header (good for redrictions) 

// use with location header 

header('HTTP/1.1 301 Moved Permanently'); 
// 訪問受限 

header('HTTP/1.1 403 Forbidden'); 
// 服務器錯誤 

header('HTTP/1.1 500 Internal Server Error'); 


// 重定向到一個新的位置 

// redirect to a new location: 

header('Location: http://www.example.org/'); 

//延遲一段時間后重定向 

// redrict with delay: 

header('Refresh: 10; url=http://www.example.org/'); 
print 'You will be redirected in 10 seconds'; 

// 覆蓋 X-Powered-By value 

// override X-Powered-By: PHP: 

header('X-Powered-By: PHP/4.4.0'); 
header('X-Powered-By: Brain/0.6b'); 

// 內容語言 (en = English) 

// content language (en = English) 

header('Content-language: en'); 


//最后修改時間(在緩存的時候可以用到) 

// last modified (good for caching) 
$time = time() - 60; // or filemtime($fn), etc 
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT'); 

// 告訴瀏覽器要獲取的內容還沒有更新 
// header for telling the browser that the content 
// did not get changed 

header('HTTP/1.1 304 Not Modified'); 

// 設置內容的長度 (緩存的時候可以用到): 


// set content length (good for caching): 

header('Content-Length: 1234'); 

// 用來下載文件: 

// Headers for an download: 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename="example.zip"'); 
header('Content-Transfer-Encoding: binary'); 



// 禁止緩存當前文檔: 
// load the file to send:readfile('example.zip'); 
// Disable caching of the current document: 
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate'); 
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 



// 設置內容類型: 
// Date in the pastheader('Pragma: no-cache'); 
// set content type: 
header('Content-Type: text/html; charset=iso-8859-1'); 
header('Content-Type: text/html; charset=utf-8'); 
header('Content-Type: text/plain'); 



// plain text file 
header('Content-Type: image/jpeg');

// JPG picture 
header('Content-Type: application/zip');

// ZIP file 
header('Content-Type: application/pdf'); 
// PDF file 

header('Content-Type: audio/mpeg'); 
// Audio MPEG (MP3,...) file 

header('Content-Type: application/x-shockwave-flash'); 


// 顯示登錄對話框,可以用來進行HTTP認證 

// Flash animation// show sign in box 
header('HTTP/1.1 401 Unauthorized'); 
header('WWW-Authenticate: Basic realm="Top Secret"'); 
print 'Text that will be displayed if the user hits cancel or '; 
print 'enters wrong login data';
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 本文是《圖解HTTP》讀書筆記的第二篇,主要包括此書的第六章內容,因為第六章的內容較多,而且比較重要,所以單獨寫為...
    lijiankun24閱讀 1,392評論 0 6
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,869評論 18 139
  • 本篇文章篇幅比較長,先來個思維導圖預覽一下。 一、概述 1.計算機網絡體系結構分層 2.TCP/IP 通信傳輸流 ...
    滌生_Woo閱讀 55,218評論 24 557
  • 一、概念(載錄于:http://www.cnblogs.com/EricaMIN1987_IT/p/3837436...
    yuantao123434閱讀 8,420評論 6 152
  • API定義規范 本規范設計基于如下使用場景: 請求頻率不是非常高:如果產品的使用周期內請求頻率非常高,建議使用雙通...
    有涯逐無涯閱讀 2,586評論 0 6