首頁 > 网站应用 > 使用mod_headers或mod_expires落实缓存

使用mod_headers或mod_expires落实缓存

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明

实施这一方法将节省你难以置信数额的带宽,极大地加快你的网站为你的网站访客。基本上,对于图片,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-

原始文章: Speed Up Sites with htaccess Caching (http://www.askapache.com/htaccess/speed-up-sites-with-htaccess-caching.html)

Categories: 网站应用 Tags: , ,
  1. 2008年10月14日12:11 | #1

    “Всегда приятно читать умных людей”

  2. 2008年10月15日02:07 | #2

    “Мне очень помогали ваши записи”

  3. 2008年10月15日05:35 | #3

    “Ты один из немногих, кто действительно хорошо пишет”

  4. 2008年10月15日17:15 | #4

    “Полезного много”

  5. 2008年10月16日02:27 | #5

    очень красиво, вот бы у нас так сделали

  6. 2008年10月16日03:23 | #6

    С чистым юмором.

  7. 2008年10月16日03:46 | #7

    “Превосходно”

  8. 2008年10月16日04:49 | #8

    ну что тут скажешь…

  9. 2008年10月16日12:34 | #9

    круто..взяла почти все))

  10. 2008年10月16日13:42 | #10

    ух ты как крууууууууууутооооооо))

  11. 2008年10月18日04:29 | #11

    класс)мне понра)особенно!

  12. 2008年10月24日19:58 | #12

    “Классный пост”

  13. 2008年10月24日21:13 | #13

    “здорово!”

  14. 2008年10月24日23:25 | #14

    “Занятно”

  15. 2008年10月25日15:08 | #15

    “Спасибо за статью”

  16. 2008年10月25日16:38 | #16

    “Прикольно”

  17. 2008年10月28日03:59 | #17

    очень красиво, вот бы у нас так сделали

  18. 2008年11月7日09:21 | #18

    Хороший пост! Подчерпнул для себя много нового и интересного!
    Пойду ссылку другу дам в аське :)

評論分頁
  1. 目前尚無任何 trackbacks 和 pingbacks。