apache配置優化(六)

Apache--mod_expires緩存模塊(這是apache調優的重要參數)

mod_expires介紹:

mod_expires允許圖片在用戶瀏覽器進行緩存,用戶打開網站,第一次會把圖片加載到本地,如果在有效期呢,用戶在訪問,就不需要去網站讀取,只是加載在本地。

好處一:提升用戶體驗

由於用戶讀本地緩存了,所以訪問頁面就快了,也節省網站的帶寬流量成本。

好處二:節省網站帶寬成本

由於讀本地緩存了,和服務器的交互就少了,也節省網站帶寬流量成本。

好處三:節省網站服務器及維護成本

由於用戶讀本地緩存了,和服務器的交互就少了,服務器的壓力小了,服務器數量及維護人員等成本都降低了,這也是爲什麼taobao的緩存是10年過期。

expires失效條件:

1、內容過期

2、用戶自己手動清除

控制expires方法:

如果網站更新功能或更新文件後,用戶再訪問時的內容還是舊的(上面已提到不會再下載了)

怎麼解決這個問題呢?

1)首先,對於大公司業務來說,圖片等資源一般很少會去修改。因此,taobao,jd可以肆無忌憚的吧expirts設置爲10年。一年可以節省費用上億元。

2)對於js,css偶爾會變化的資源,一般expires設置時間會比較短。如1-30天。

3)在更新文件上採取策略,如,更新後以新的文件名發佈,這樣對於用戶又是新的資源了。

expires特殊緩存情況:

1)特殊緩存,google首頁expires一日(經常會變更圖片)

2)網站的js統計代碼不會設置緩存


檢查模塊是否安裝:

[root@eric6 ~]#/application/apache/bin/apachectl -l|grep mod_expires.c

mod_expires.c

[root@eric6 ~]#/application/apache/bin/apachectl -M|grep expires

expires_module (static)

Syntax OK

如果有以上兩個參數,說明mod_expires已經編譯安裝

如果在查找的時候沒有安裝該模塊,可以使用DSO方式安裝:

[root@eric6metadata]# cd /home/tools/httpd-2.2.25/modules/metadata/

[root@eric6metadata]# ll mod_expires.c

-rw-r--r-- 1liuyalei liuyalei 18285 11月 12 2008mod_expires.c

[root@eric6 metadata]#/application/apache/bin/apxs -c -i -a mod_expires.c


[root@eric6metadata]# wget 127.0.0.1

--2013-10-2022:12:10--http://127.0.0.1/

Connecting to127.0.0.1:80... connected.

HTTP request sent,awaiting response... 200 OK

Length: 8[text/html]

Saving to:`index.html.1'


100%[==========================================================================================>]8--.-K/sin 0s


2013-10-20 22:12:10(1.77 MB/s) - `index.html.1' saved [8/8]


[root@eric6metadata]# curl -I 127.0.0.1

HTTP/1.1 200 OK

Date: Sun, 20 Oct2013 14:12:11 GMT

Server:Apache/2.2.25 (Unix) DAV/2

Last-Modified: Sat,19 Oct 2013 04:57:07 GMT

ETag:"43ddb-8-4e910e1fef476"

Accept-Ranges:bytes

Content-Length: 8

Content-Type:text/html

註釋expires配置

ExpiresActive on#開啓expires功能

ExpiresDefault "access plus 12month"#缺省緩存情況(12個月)

ExpiresByType text/html "accessplus 12 months"#緩存類型html

ExpiresByType text/css "accessplus 12 months"#緩存類型css

ExpiresByType p_w_picpath/gif "accessplus 12 months"#緩存類型gif

ExpiresByType p_w_picpath/jpeg "accessplus 12 months"#緩存類型jpeg

ExpiresByType p_w_picpath/jpg "accessplus 12 months"#緩存類型jpg

ExpiresByType p_w_picpath/png "accessplus 12 months"#緩存類型png

EXpiresByTypeapplication/x-shockwave-flash "access plus 12 months"#緩存類型flash

EXpiresByType application/x-javascript"access plus 12 months"#緩存類型JavaScript

ExpiresByType video/x-flv "accessplus 12 months"#緩存類型x-flv

實例:爲一個server標籤添加expires功能

[root@eric6apache]# vi /application/apache/conf/extra/httpd-vhosts.conf

<VirtualHost*:80>

ServerAdmin [email protected]

DocumentRoot "/var/bbs"

ServerName bbs.liuyalei.com

ServerAlias liuyalei.com

ErrorLog "logs/bbs-error_log"

CustomLog "logs/bbs-access_log"common

ExpiresActive on

ExpiresDefault "access plus 12month"

ExpiresByType text/html "accessplus 12 months"

ExpiresByType text/css "accessplus 12 months"

ExpiresByType p_w_picpath/gif "accessplus 12 months"

ExpiresByType p_w_picpath/jpeg "accessplus 12 months"

ExpiresByType p_w_picpath/jpg "accessplus 12 months"

ExpiresByType p_w_picpath/png "accessplus 12 months"

EXpiresByTypeapplication/x-shockwave-flash "access plus 12 months"

EXpiresByType application/x-javascript"access plus 12 months"

ExpiresByType video/x-flv "accessplus 12 months"

</VirtualHost>

[root@eric6apache]# ../apache/bin/apachectl -t

Syntax OK

[root@eric6apache]# ../apache/bin/apachectl graceful

[root@eric6apache]# ps -ef|grep httpd

root473410 11:56 ?00:00:03/application/apache2.2.25/bin/httpd -k start

daemon817747340 22:30 ?00:00:00/application/apache2.2.25/bin/httpd -k start

daemon817847340 22:30 ?00:00:00/application/apache2.2.25/bin/httpd -k start

daemon817947340 22:30 ?00:00:00/application/apache2.2.25/bin/httpd -k start

daemon818047340 22:30 ?00:00:00/application/apache2.2.25/bin/httpd -k start

root826379690 22:30 pts/000:00:00 grep httpd

服務端網站上傳一張圖片,然後在火狐瀏覽器安裝2個插件,進行查看,檢查緩存情況:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章