note: apache Expired模組 (mod_expires)

2013100514:02


 
 

確認 apache 是否已內建 mod_expires ?

 

 
 

$ httpd -l

Compiled in modules:
  core.c
  mod_access.c
  mod_auth.c
  mod_include.c
  mod_log_config.c
  mod_env.c
  mod_expires.c
  mod_setenvif.c
  : : :

 
 

自己 compiler apache httpd:

 

 
 

./configure --enable-so --prefix=/var/www/httpd --enable-rewrite --enable-expires



 
 

httpd.conf 設定範例

 

 
 

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType application/x-javascript "access plus 1 week"
ExpiresByType text/javascript "access plus 1 week"
ExpiresByType text/css "access plus 1 day"
ExpiresByType text/html A900
ExpiresByType text/xml A14400
ExpiresByType text/plain "access plus 1 hours"
ExpiresByType image/gif "access plus 1 months"
ExpiresByType image/jpg "access plus 1 months"
ExpiresByType image/jpeg "access plus 1 months"
ExpiresByType image/png "access plus 1 months 15days 2horus"
ExpiresByType image/bmp "access plus 1 day"
ExpiresByType application/x-shockwave-flash A3600
</IfModule>


<IfModule mod_expires.c> 
   ExpiresActive On 
   ExpiresDefault A0  
    
   # ExpiresDefault "<base> [plus] {<num> <type>}*" 
   # base:[ access | modification ] 
   # type:[ years | months | weeks | days | hours | minutes | seconds ] 
    
   <FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$"> 
       ExpiresDefault "access plus 1 years" 
   </FilesMatch> 
    
   <FilesMatch "\.(jpg|jpeg|png|gif|swf|txt|xml|js|css)$"> 
       ExpiresDefault "access plus 7 days" 
   </FilesMatch> 
</IfModule> 



plus 後面可用的關鍵字
  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds



"M" modification 表示 server 端檔案的最後修改時間
"A" access 表示 client 端讀取 server 檔案的訪問時間

例如

# GIF 有效期為客戶端讀取檔案後後 一個月 (2592000 secs)
ExpiresByType image/gif A2592000

# HTML文件的有效期是 檔案最後修改時間之後的一星期 (604800 secs)
ExpiresByType text/html M604800

 
 

參考

 

 
 
http://httpd.apache.org/docs/2.2/mod/mod_expires.html
http://www.phpchina.com/resource/manual/apache/mod/mod_expires.html

What's the best way to version CSS and JS URLs?