取势 明道 优术

作者为 扶 凯 发表

转:http://www.pcxingxing.net.ru/main/2008-02/100-using-mod_headers-or-mod_expires-implement-caching.html

实施这一方法将节省你难以置信数额的带宽,极大地加快你的网站为你的网站访客.基本上,对于图片,CSS , JavaScript以及其他文件可以通过优化更快的下载,告诉你的网站访问者快取记忆体,为他们在某一段时间内.默认的行为是每一次请求检查文件的last-modified 和/或者  Etag headers.

所以一个用户去/home/index.html,及浏览器缓存所有图象和文件.然后用户离开网站稍后回来,与浏览器发送If-Modified-Since 有条件的GET 请求为每一个缓存的项目时,基本上看,如果文件已被改变和他们必须更新他们的缓存.

当你执行在这篇文章中所述的缓存方法,你可以指定某文件或扩展名被缓存为某一特定数额的时间.这些文件然后缓存在你的网站访客和他们不发送If-Modified-Since头直到设置的缓存时间已经到了.

#================================================= ============================#

# TIME CHEAT SHEET

#================================================= ============================#

# 300 5 M # 604800 1 W

# 2700 45 M # 1814400 3 W

# 3600 1 H # 2419200 1 M

# 54000 15 H # 14515200 6 M

# 86400 1 D # 26611200 11 M

# 518400 6 D # 29030400 1 Y (never expire)

第一个解决办法是Apache模块mod_expires 1.3 2.0 2.2

ExpiresActive On

ExpiresDefault A300

ExpiresByType image/x-icon A2592000

ExpiresByType application/x-javascript A2592000

ExpiresByType text/css A2592000

ExpiresByType image/gif A604800

ExpiresByType image/png A604800

ExpiresByType image/jpeg A604800

ExpiresByType text/plain A604800

ExpiresByType application/x-shockwave-flash A604800

ExpiresByType video/x-flv A604800

ExpiresByType application/pdf A604800

ExpiresByType text/html A300

第二个解决办法是mod_headers 1.3 2.0 2.2

 # YEAR

<FilesMatch “\.(flv|gif|ico)$”>

Header set Cache-Control “max-age=2592000″

</FilesMatch>

# WEEK

<FilesMatch “\.(pdf|swf|js|css)$”>

Header set Cache-Control “max-age=604800″

</FilesMatch>

# NEVER CACHE

<FilesMatch “\.(html|cgi|php|htm)$”>

Header set Expires “Thu, 01 Dec 2003 16:00:00 GMT”

Header set Cache-Control “no-store, no-cache, must-revalidate”

Header set Pragma “no-cache”

</FilesMatch>

注:用filesmatch和files在htaccess文件

这里是Headers当下载一个JPEG图像的时候,

这个缓存方案实施后和没有缓存时的效果.

JPEG 没有缓存的时

Last-Modified: Wed, 22 Feb 2006 12:16:56 GMT

ETag: “b57d54-45e7″

Accept-Ranges: bytes

Content-Length: 17895

Connection: close

Content-Type: image/jpeg

缓存过的

Cache-Control: max-age=2592000

Expires: Tue, 28 Mar 2006 16:23:52 GMT

Last-Modified: Wed, 22 Feb 2006 12:16:56 GMT

ETag: “b57d54″

Accept-Ranges: bytes

Content-Length: 17895

Connection: close

Content-Type: image/jpeg

Content-Language: en

附:

apache配置文件例子:

example 1

# htm files are php

AddHandler application/x-httpd-php .php .htm

# setup errordocuments to local php file

ErrorDocument 404 /cgi-bin/error.htm

ErrorDocument 403 /cgi-bin/error.htm

ErrorDocument 500 /cgi-bin/error.htm

# Turn on Expires and set default expires to 3 days

ExpiresActive On

ExpiresDefault A259200

# Set up caching on media files for 1 month

<FilesMatch “\.(ico|gif|jpg|jpeg|png|flv|pdf|swf|mov|mp3|wmv|pp t)$”>

ExpiresDefault A2419200

Header append Cache-Control “public”

</FilesMatch>

# Set up 2 Hour caching on commonly updated files

<FilesMatch “\.(xml|txt|html|js|css)$”>

ExpiresDefault A7200

Header append Cache-Control “private, must-revalidate”

</FilesMatch>

# Force no caching for dynamic files

<FilesMatch “\.(php|cgi|pl|htm)$”>

ExpiresDefault A0

Header set Cache-Control “no-store, no-cache, must-revalidate, max-age=0″

Header set Pragma “no-cache”

</FilesMatch>

example 2

# htm files are php

AddHandler application/x-httpd-php .php .htm

# setup errordocuments to local php file

ErrorDocument 404 /cgi-bin/error.htm

ErrorDocument 403 /cgi-bin/error.htm

ErrorDocument 500 /cgi-bin/error.htm

# Turn on Expires and set default to 0

ExpiresActive On

ExpiresDefault A0

# Set up caching on media files for 1 year (forever?)

<FilesMatch “\.(ico|flv|pdf|mov|mp3|wmv|ppt)$”>

ExpiresDefault A29030400

Header append Cache-Control “public”

</FilesMatch>

# Set up caching on media files for 1 week

<FilesMatch “\.(gif|jpg|jpeg|png|swf)$”>

ExpiresDefault A604800

Header append Cache-Control “public, proxy-revalidate”

</FilesMatch>

# Set up 2 Hour caching on commonly updated files

<FilesMatch “\.(xml|txt|html|js|css)$”>

ExpiresDefault A7200

Header append Cache-Control “private, proxy-revalidate, must-revalidate”

</FilesMatch>

# Force no caching for dynamic files

<FilesMatch “\.(php|cgi|pl|htm)$”>

ExpiresDefault A0

Header set Cache-Control “no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform”

Header set Pragma “no-cache”

</FilesMatch>

-end-

来了就留个评论吧! 2个评论



    kongjian 2009年07月23日 的 17:24

    Server: Apache/1.3.41 (Unix)
    安装了mod_expires
    在网站根目录添加了文件.htaccess
    .htaccess内容如下

    ExpiresActive On
    ExpiresDefault “access plus 12 hours”
    ExpiresByType text/html “access plus 3 days”
    ExpiresByType text/plain “access plus 3 days”
    ExpiresByType image/gif “access plus 30 days”
    ExpiresByType image/png “access plus 30 days”
    ExpiresByType image/jpeg “access plus 30 days”
    ExpiresByType image/x-icon “access plus 30 days”

    但是浏览网站时显示还是没有no-expires
    这是怎么回事啊?还要做些什么呢?
    谢谢

    Loy40 2009年10月23日 的 00:35

    A question arises as to whether it is possible for all countries to do this in the general equilibrium of the world economy. ,