Apache如何實現靜態快取 實操案例

2021-07-29 20:17:26 字數 3345 閱讀 5566

apache如何實現靜態快取可以啟用mod_expires&mod_headers

一.瀏覽器快取原理

loadmodule expires_module modules/mod_expires.so
mod_expires預設快取指令是expiresdefault,這個可以對所有檔案進行快取,我們如果想設定預設快取,選用以下方式

expiresactive on

#訪問之後的乙個月不再更新

expiresdefault "access plus 1 month"

#訪問之後的4周不再更新

#expiresdefault "access plus 4 weeks"

#訪問之後的30天不再更新

#expiresdefault "access plus 30 days"

2.1 expiresdefault指令

按照apache server的介紹,expiresdefault的格式如下

expiresdefault "[plus] {}*"
其中,base取值如下

access                    #訪問之後,從當前時間計算

now(等價於access) #訪問之後,從當前時間計算

modification         #修改之後,從伺服器檔案修改後計算

plus是關鍵字,這個是系統指定的寫法

表示計數,單位為秒

表示日期單位,後者取值如下

2.2 expiresbytype 指令

實際上,expiresdefault指令有非常嚴重的快取問題,我們應該知道,所有資源被快取,導致**會出現更新問題,我們更應該少用expiresdefault而多用expiresbytype,此外expiresdefault設定的時間要盡可能短。

expiresbytype 指令規則如下

expiresbytype type/encoding "[plus] {}*"
其中base,num,type和 expiresdefault 類似。

當然,在apache http server中,指令還可以使用簡寫方式

expiresdefault type/encoding  [ufrist(base)][seconds]

expiresbytype type/encoding [ufrist(base)][seconds]

ufrist(base) 表示 base的首字母大寫, seconds表示過期時間,單位為秒

# a***x - access seconds ,表示訪問之後經過多少秒

# m***x - modifyed seconds ,表示修改之後經過多少秒(推薦)

expiresactive on

expiresdefault a3600 #表示一小時後更新

expiresbytype image/x-icon a86400 #表示1天後更新

#指令碼檔案和css樣式,我們最好使用 modification

expiresbytype text/css m2592000

#表示修改後如果沒有再次修改,那麼經過乙個星期才允許更新快取

expiresbytype image/gif m604800

expiresbytype image/png a604800

expiresbytype image/jpeg m604800

expiresbytype text/plain a604800

expiresbytype video/x-flv a604800

expiresbytype text/html a900

此外,以上寫法可讀性好,但是如果要更方便一些,不妨試試如下寫法

expiresactive on

expiresdefault a3600

# 1 年

expiresdefault a9030400

# 1 星期

expiresdefault a604800

# 3 小時

expiresdefault m10800

三.mod_headers快取實現1.關於mod_headers

apache http server官網關於mod_headers的描述是自定義乙個request header和response header

2.mod_headers用法

載入模組

簡單例子

header set myheader "hello joe. it took %d microseconds for apache to serve this request."

當然,既然允許自定義,我們不妨可以通過新增cache-control來增強快取

# htm,html,txt類的檔案快取乙個小時  

header set cache-control "max-age=3600"

# css, js, swf類的檔案快取乙個星期

header set cache-control "max-age=604800"

3.資源更新問題

不像mod_expires模組具有modification指令,那麼資源更新了就會出現問題,對於這些問題如何處理?

cache-control加入http/1.1是為了解決時間精度問題,當然,他有好幾個搭檔,etag,if-range,last-modified,這幾個選項apache伺服器本身就實現了,當然,我們還要注意,http.conf和.htaccess不能出現如下 header  unset etag和 header unset last-modifie,否則更新快取將成為大問題,導致**更新ui失敗,資料提交出錯,斷點續傳失敗等問題。

解決上述問題,最好不要出現如下配置

header unset last-modified

header unset etag

注意:etag在分布式跨伺服器請求時會可能遇到校驗失敗的問題,導致快取實效,從而也會引發更新ui失敗,資料提交出錯,斷點續傳失敗等問題。因此,如果你是分布式系統,建議還是header unset etag或者fileetag none。

apache實現偽靜態

首先在apache裡開啟配置檔案httpd.conf,找到loadmodule rewrite module modules mod rewrite.so,將前面的 號刪掉,如果沒有這句話則自己新增一句。一般有倆種方法可以重寫url 第一 如果有修改httpd.conf許可權,可以直接在httpd....

Apache開啟rewrite實現偽靜態

開啟rewrite的方法非常簡單,開啟apache安裝目錄下的conf httpd.conf檔案 去掉 loadmodule rewrite module modules mod rewrite.so 前面的井號注釋 loadmodule rewrite module modules mod rew...

Apache開啟rewrite實現偽靜態

開啟rewrite的方法非常簡單,開啟apache安裝目錄下的conf httpd.conf檔案 去掉 loadmodule rewrite module modules mod rewrite.so 前面的井號注釋 loadmodule rewrite module modules mod rew...