如何加快自己的網(wǎng)站的速度,應(yīng)該是各位站長、博主比較關(guān)心的問題。一個(gè)站點(diǎn)的打開速度關(guān)系到很多方面的內(nèi)容,如服務(wù)器性能、網(wǎng)絡(luò)速度及站點(diǎn)的網(wǎng)頁設(shè)計(jì)等等。
在Yahoo! Developer Network中的Best Practices for Speeding Up Your Web Site一文中就介紹了34種加速你的網(wǎng)站的建議。其中3項(xiàng)就涉及有關(guān)Web服務(wù)器的優(yōu)化內(nèi)容,這篇文章就是專門針對(duì)這3項(xiàng)建議來對(duì)Apache進(jìn)行相應(yīng)的設(shè)置,以達(dá)到網(wǎng)站加速的目的。
為文件設(shè)置過期時(shí)間
這樣做的目的就是控制瀏覽器緩存我們不經(jīng)常修改的文件,如圖片、css、js等等,這樣當(dāng)瀏覽者打開另外一個(gè)網(wǎng)頁的時(shí)候就減少了文件的下載,達(dá)到了加快網(wǎng)站速度的目的。要實(shí)現(xiàn)讓Apache增加文件有效期讓瀏覽器緩存可以用兩種辦法實(shí)現(xiàn):
第一種:利用mod_headers模塊為文件增加有效期
加載mod_headers模塊,在配置文件中增加:
LoadModule headers_module modules/mod_headers.so
配置mod_headers模塊,設(shè)置jpg jpeg gif png ico js css swf flv等文件的Expires:(Expires頭只能為固定的時(shí)間,形象點(diǎn)說就好像商品的過期時(shí)間。這樣做的目的是為了兼容不支持Cache-Control的瀏覽器,如果設(shè)置了Cache-Control且瀏覽器支持Cache-Control的話就優(yōu)先采用Cache-Control的設(shè)置,當(dāng)然目前大部分瀏覽器支持Cache-Control文件頭)
<FilesMatch "/.(jpg|jpeg|gif|png|ico|js|css|swf|flv)$">
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>
配置mod_headers模塊,設(shè)置jpg jpeg gif png ico js css swf flv等文件的Cache-Control:(Cache-Control控制更為靈活一些,它設(shè)置的是一個(gè)有效時(shí)間,就好比商品的保質(zhì)期,下面的604800單位為秒,就是設(shè)置文件的有效時(shí)間為為1星期)
<FilesMatch "/.(jpg|jpeg|gif|png|ico|swf|flv|js|css)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
第二種:利用mod_expires模塊為文件增加Expires
加載mod_expires模塊,在配置文件中增加:
LoadModule expires_module modules/mod_expires.so
配置mod_expires模塊:
ExpiresActive On
ExpiresDefault "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 weeks"
ExpiresByType text/css access plus 3 days
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType text/plain "access plus 1 weeks"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType video/x-flv "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/html "access plus 1 weeks"
開啟Gzip壓縮
要讓Apache實(shí)現(xiàn)Gzip壓縮需要開啟mod_deflate模塊和mod_headers模塊,前面已經(jīng)開啟了mod_headers模塊,這里就再開啟mod_deflate模塊即可,打開Apache配置文件,添加:
LoadModule deflate_module modules/mod_deflate.so
配置mod_deflate模塊,讓其壓縮html xml css php js等文件:
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-httpd-php application/x-javascript
AddOutputFilter DEFLATE html xml css php js
</ifmodule>
關(guān)閉ETags
即使設(shè)置了Expires或者Cache-Control,有時(shí)瀏覽器在訪問資源時(shí)往往會(huì)因?yàn)镋Tag的存在而重新下載整個(gè)資源,所以簡(jiǎn)單的做法是關(guān)閉它們:
FileETag None
相信設(shè)置好上面3項(xiàng)以后你的站點(diǎn)瀏覽速度會(huì)提高很多,如果自己感覺不到速度的提升,不妨安裝Firefox的YSlow插件來檢測(cè)一下你的設(shè)置是否成功。
新聞熱點(diǎn)
疑難解答
圖片精選