什么样的页面能够比较好的被缓存服务器缓存呢?如果返回内容的HTTP HEADER中有"Last-Modified"和"Expires"相关声明,比如:

Last-Modified: Wed, 14 May 2003 13:06:17 GMT

Expires: Fri, 16 Jun 2003 13:06:17 GMT

前端缓存服务器在期间会将生成的页面缓存在本地:硬盘或者内存中,直至上述页面过期。

因此,一个可缓存的页面:

  • 页面必须包含Last-Modified: 标记
    一般纯静态页面本身都会有Last-Modified信息,动态页面需要通过函数强制加上,比如在PHP中:
    // always modified now
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  • 必须有Expires或Cache-Control: max-age标记设置页面的过期时间:
    对于静态页面,通过apache的mod_expires根据页面的MIME类型设置缓存周期:比如图片缺省是1个月,HTML页面缺省是2天等。

Apache配置代码:

<IfModule mod_expires.c> 
    ExpiresActive on
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType text/css "now plus 2 day"
    ExpiresDefault "now plus 1 day"
</IfModule>

对于动态页面,则可以直接通过写入HTTP返回的头信息,比如对于新闻首页index.php可以是20分钟,而对于具体的一条新闻页面可能是1天后过 期。比如:在php中加入了1个月后过期:

// Expires one month later

header("Expires: " .gmdate ("D, d M Y H:i:s", time() + 3600 * 24 * 30). " GMT");

  • 如果服务器端有基于HTTP的认证,必须有Cache-Control: public标记,允许前台

Asp代码:

<%
Function SetExpiresHeader(ByVal minutes)
    '设置页面在几分钟内过期,设置 Expires 头
    Response.AddHeader "Last-Modified", DateToHTTPDate(Now())                   '设置页面的 Last-Modified 头
    Response.Expires = minutes                                                  '页面过期时间(分钟)
    Response.CacheControl = "public"                                            '设置高速缓存控制至外部应用程序
End Function

Function DateToHTTPDate(ByVal OleDATE)
    '转换日期为 HTTP 格式 (1999-10-22 11:08:38) --> (Fri, 22 Oct 1999 12:08:38 GMT)
    Const GMTdiff = #8:00:00 AM#                                                '时区
    OleDATE = OleDATE - GMTdiff                                                 '计算时差
    DateToHTTPDate = engWeekDayName(OleDATE) & ", " & _
                     Right("0" & Day(OleDATE), 2) & " " & engMonthName(OleDATE) & " " & Year(OleDATE) & " " & _
                     Right("0" & Hour(OleDATE), 2) & ":" & Right("0" & Minute(OleDATE), 2) & ":" & Right("0" & Second(OleDATE), 2) & " GMT"
End Function

Function engWeekDayName(dt)
    Select Case Weekday(dt, 1)
        Case 1: engWeekDayName = "Sun"
        Case 2: engWeekDayName = "Mon"
        Case 3: engWeekDayName = "Tue"
        Case 4: engWeekDayName = "Wed"
        Case 5: engWeekDayName = "Thu"
        Case 6: engWeekDayName = "Fri"
        Case 7: engWeekDayName = "Sat"
    End Select
End Function

Function engMonthName(dt)
    Select Case Month(dt)
        Case 1: engMonthName = "Jan"
        Case 2: engMonthName = "Feb"
        Case 3: engMonthName = "Mar"
        Case 4: engMonthName = "Apr"
        Case 5: engMonthName = "May"
        Case 6: engMonthName = "Jun"
        Case 7: engMonthName = "Jul"
        Case 8: engMonthName = "Aug"
        Case 9: engMonthName = "Sep"
        Case 10: engMonthName = "Oct"
        Case 11: engMonthName = "Nov"
        Case 12: engMonthName = "Dec"
    End Select
End Function
%>

然后在具体的页面中,比如index.asp和news.asp的“最上面”加入以下代码:HTTP Header

Asp代码:

<!--#include file="../include.asp"-->
<%
'页面将被设置20分钟后过期
SetExpiresHeader(20)
%>

摘自:

如何配置web服务器以提高缓冲兼容性详细内容:3.可缓存的动态页面设计,2006-09-19 16:15,http://hi.baidu.com/%CA%AB%D5%B9/blog/item/fa76860aec9d993eb0351d59.html

可缓存的动态页面设计,2010-12-24 14:21:19,也是转载的上边那个地址的一部分内容,http://wezly.iteye.com/blog/851130

原文内容有少量修改。