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-
原始文章: Speed Up Sites with htaccess Caching (http://www.askapache.com/htaccess/speed-up-sites-with-htaccess-caching.html)
翻译:XINGXING
相关日志
如果你喜欢我的文章, 随时 订阅我的RSS feeds




















BlogoSquare
02月 5th, 2008 at 7:00 am
Correct link is: http://www.askapache.com/htaccess/speed-up-sites-with-htaccess-caching.html
09月 21st, 2008 at 10:50 pm
=))))))
09月 21st, 2008 at 11:09 pm
спасибо большое.
09月 21st, 2008 at 11:25 pm
Читаешь это и думаешь….
09月 21st, 2008 at 11:46 pm
да,но это еще и не все…
09月 22nd, 2008 at 12:07 am
Забавно.
09月 22nd, 2008 at 3:53 am
жжот))
09月 22nd, 2008 at 12:14 pm
Спасибо!, в цитатник!
09月 22nd, 2008 at 12:33 pm
Классно!
09月 22nd, 2008 at 12:53 pm
ух ты как крууууууууууутооооооо))
09月 22nd, 2008 at 1:08 pm
супер оригинально
09月 22nd, 2008 at 2:42 pm
Блеск.
09月 22nd, 2008 at 3:03 pm
тема реально старая
09月 22nd, 2008 at 3:24 pm
Классно!
09月 23rd, 2008 at 4:32 am
потрясающие идеи…нам перенять бы …великолепно.
09月 23rd, 2008 at 11:13 pm
Отличная работа!
09月 23rd, 2008 at 11:32 pm
Какая прелесть!!!!!!!!!!!!)
09月 24th, 2008 at 12:23 am
Спасибо огромное!
09月 24th, 2008 at 6:16 pm
Да, Именно так и было!:))
09月 27th, 2008 at 5:35 am
круто..взяла почти все))
09月 30th, 2008 at 1:32 am
Напомнили….Точно, все так.
09月 30th, 2008 at 2:40 am
забрала в цитатник, спасибо!
09月 30th, 2008 at 3:15 am
фантастика!…
09月 30th, 2008 at 3:51 am
Ничего себе подборочка!!!!!!! Великолепно!
09月 30th, 2008 at 4:09 am
Благодарю!
09月 30th, 2008 at 4:27 am
мдяяяя ….. *много думал*….
автору спасибо за пост !!
09月 30th, 2008 at 4:44 am
Большое спасибо! Есть ещё повод получить удовольствие… С вашего разрешения, беру.
09月 30th, 2008 at 5:16 am
…. вот так вот…..
09月 30th, 2008 at 5:33 am
Интересненько=)
09月 30th, 2008 at 5:51 am
Классно!
09月 30th, 2008 at 2:19 pm
ммм)) так клёво))
09月 30th, 2008 at 2:36 pm
Очень рада, что возникло желание взять этот пост в цитатник!
09月 30th, 2008 at 9:26 pm
Интересненько=)
10月 8th, 2008 at 6:53 am
Забавно.
10月 9th, 2008 at 4:14 am
Красота, особенно первая фотка
10月 9th, 2008 at 4:56 am
Класс! Афтару респект!
10月 9th, 2008 at 5:37 am
Да, Именно так и было!:))
10月 10th, 2008 at 3:15 am
Ну жесть конечно…
10月 10th, 2008 at 4:10 am
Шикарно, возьму в дневник
10月 10th, 2008 at 4:35 am
Спасибки)))))) в цитатник!
10月 10th, 2008 at 6:50 am
jaqyxtdikhfwfmydbkkvhiwuqqjvdr
10月 11th, 2008 at 9:11 pm
=))))))
10月 11th, 2008 at 11:51 pm
Случайно увидел. Не ожидал.
10月 13th, 2008 at 12:06 am
а в каком это городе,какой стране??очень креативненько!!!!!)))))
10月 13th, 2008 at 12:46 am
Было интересно посмотреть!!!
10月 13th, 2008 at 11:00 pm
Вам спасибо - за тёплый приём )
10月 13th, 2008 at 11:44 pm
Шикарно, возьму в дневник
10月 14th, 2008 at 12:27 am
Случайно увидел. Не ожидал.
10月 14th, 2008 at 1:11 am
забрала в цитатник, спасибо!
10月 14th, 2008 at 3:39 am
“подробней пожалуйста”
10月 14th, 2008 at 12:11 pm
“Всегда приятно читать умных людей”
10月 15th, 2008 at 2:07 am
“Мне очень помогали ваши записи”
10月 15th, 2008 at 5:35 am
“Ты один из немногих, кто действительно хорошо пишет”
10月 15th, 2008 at 5:15 pm
“Полезного много”
10月 16th, 2008 at 2:27 am
очень красиво, вот бы у нас так сделали
10月 16th, 2008 at 3:23 am
С чистым юмором.
10月 16th, 2008 at 3:46 am
“Превосходно”
10月 16th, 2008 at 4:49 am
ну что тут скажешь…
10月 16th, 2008 at 12:34 pm
круто..взяла почти все))
10月 16th, 2008 at 1:42 pm
ух ты как крууууууууууутооооооо))
10月 18th, 2008 at 4:29 am
класс)мне понра)особенно!
10月 24th, 2008 at 7:58 pm
“Классный пост”
10月 24th, 2008 at 9:13 pm
“здорово!”
10月 24th, 2008 at 11:25 pm
“Занятно”
10月 25th, 2008 at 3:08 pm
“Спасибо за статью”
10月 25th, 2008 at 4:38 pm
“Прикольно”
10月 25th, 2008 at 6:16 pm
“подробней пожалуйста”
10月 25th, 2008 at 8:02 pm
“Интересная заметка”
10月 26th, 2008 at 2:29 am
“познавательно”
10月 26th, 2008 at 3:33 am
“Добавил в закладки”
10月 26th, 2008 at 4:38 am
“шикарно”
10月 26th, 2008 at 5:42 am
мдяяяя ….. *много думал*….
автору спасибо за пост !!
10月 26th, 2008 at 6:46 am
“Прямо даже не верится”
10月 26th, 2008 at 1:58 pm
“подробней пожалуйста”
10月 27th, 2008 at 1:12 am
Класс! Афтару респект!
10月 27th, 2008 at 4:20 am
“Пост хорош”
10月 28th, 2008 at 3:59 am
очень красиво, вот бы у нас так сделали
10月 28th, 2008 at 5:55 am
отличная статья, интересно было читать
10月 28th, 2008 at 6:55 am
“Жжешь”
11月 7th, 2008 at 9:21 am
Хороший пост! Подчерпнул для себя много нового и интересного!
Пойду ссылку другу дам в аське
11月 11th, 2008 at 4:15 am
Катя, ты? Сори если ошибся