1. 程式人生 > >cache.config配置檔案解讀

cache.config配置檔案解讀

這個是cdn配置快取策略的檔案,可以改變指定物件或是物件集的快取引數cacheing paremeters
該檔案預設存放在/usr/local/etc/trafficserver/,它定義了Traffic Server如何快取web objects。
你能新增caching rules來指定如下東西:

  • 不快取來自某個IP address的objects
  • 指定特定的objects在快取中快取多長時間(pin)
  • 多長時間後去驗證cached objects是fresh的?
  • 是否忽略來自server端的no-cache指令

重要宣告
在你修改完cache.config檔案後,請traffic_line -x來使修改生效(測試可行)
。當你對Cluster中的某個node重新整理
這些修改時,Traffic Server會自動將配置修改應用到叢集內的其它所有節點。

配置檔案格式解讀
每行包含一個快取規則,由若干鍵值對組成,鍵值對形如<tag>=<value>,Traffic Server識別3個空格分割的tag
primary_destination=value secondary_specifier=value action=value

每行必須包含一個主目標指示符primary destination specifiers,下列之一
dest_domain=  匹配來自某個url請求中的域名
dest_host=   匹配來自某個url請求中的主機名
dest_ip=    匹配來自某個url請求中的ip
url_regex=   匹配來自某個url請求的正則表示式
每行可以包含任意個數的不得重複的次指示符secondary specifiers(optional), 下列之一
port=       請求url中的port
scheme=     請求url中的protocol:http or https
prefix=     請求url中的path部分中的字首prefix
suffix=     請求url中的file suffix字尾
method=     請求url的方法:GET,POST,PUT,TRACE等
time=       time range,比如08:00-14:00
src_ip=     client IP address的objects
internal    true或false,指定是否匹配源自內部API的http transaction
每行必須包含一個快取指令cache directive,下列之一
action=nerver-cache             不快取指定物件
action=ignore-no-cache          (client & server no cache)忽略所有的Cache-Control:no-cache頭
action=ignore-client-no-cache   (only client no cache)忽略來自client request中的Cache-Control:no-cache頭
action=ignore-server-no-cache   (only server no cache)忽略來自origin server response中的Cache-Control:no-cache頭
cluster-cache-local  配置cluster cache去允許內容快取到每個節點本地 
pin-in-cache=<time>  保留物件在快取中的時間值,如1h15m20s,避免被改寫,不影響不能快取的物件,注意可能導致效能問題 
revalidate=<time>    配置重新驗證時間 
ttl-in-cache=<time>   (force caching and expire after <time>),類似Cache-Control: max-age:<time> header
每行也可以包含各種不同的tweaks,以調整快取引數,比如
cache-responses-to-cookies=<value>
對匹配的請求改寫cookie全域性設定proxy.config.http.cache.cache_responses_to_cookies

示例

dest_domain=www.example.com  scheme=http revalidate=2h
表示2小時後,重新驗證來自www.example.com的所有http objects

dest_domain=data.video.qiyi.com  suffix=f4v action=never-cache

dest_domain=mydomain.com suffix=gif revalidate=6h
dest_domain=mydomain.com suffix=jpeg revalidate=6h
dest_domain=mydomain.com revalidate=1h

表示6小時後,重新驗證mydomain.com中的gif和jpeg物件,所有其它物件每小時驗證一次

dest_host=127.0.0.1  scheme=http ttl-in-cache=1d

考慮到ats內部的bug,這裡應該用dest_host,而不是dest_ip,參見


url_regex=example.com/articles/popular.* time=19:00-23:00 ttl-in-cache=1d2h
使url正則在伺服器內的 7-11 pm時間段內間斷保留26小時
url_regex=example.com/game/.* pin-in-cache=1h
使符合該正則的url在快取中保留1小時

參考文獻
[1].https://docs.trafficserver.apache.org/en/latest/reference/configuration/cache.config.en.html

[2].cache.config檔案中的註釋