squid緩存動態文件及cache-control參數

大家都知道squid會自動緩存靜態文件,可對於這種動態網頁怎麼讓它也緩存起來呢,所以在google上找啊找,找到上面提得那片緩存Google Earth的博客文章。
他的方法是:
acl QUERY urlpath_regex cgi-bin \? intranet
acl forcecache url_regex -i kh.google keyhole.com
no_cache allow forcecache
no_cache deny QUERY
# ----
refresh_pattern -i kh.google 1440 20% 10080 override-expire override-lastmod reload-into-ims ignore-reload
refresh_pattern -i keyhole.com 1440 20% 10080 override-expire override-lastmod reload-into-ims ignore-reload
原理就是用 no_cache allow 和 refresh_pattern 來設定一些緩存規則,將google earth的請求強行緩存起來。
此文一出,自然早有人去驗證,可是沒人成功,原作者也音訊全無 :( ... squid的郵件列表裏也提到。 ( 看標題進來的朋友,不要急,繼續往下讀,不會讓你空手而回的 :) )
我也沒在意,估計人家功力問題 :P 。先試着用改寫一下解決yupoo api的緩存問題。
acl QUERY urlpath_regex cgi-bin \?
acl forcecache url_regex -i yupoo\.com
no_cache allow forcecache
no_cache deny QUERY
refresh_pattern -i yupoo\.com 1440 50% 10080 override-expire override-lastmod reload-into-ims ignore-reload
嘿,果然nnd毫無用處,訪問記錄裏還是 一坨坨 TCP_MISS
於是翻來覆去看文檔,找資料,發現是squid的bug惹得禍,不過早已經修正(嚴格來說是功能擴展補丁)。
我的squid是2.6.13,翻了一下源代碼,確實已經打好補丁了。
解決這個問題需要refresh_pattern的幾個擴展參數(ignore-no-cache ignore-private),這幾個參數在squid的文檔和配置例子中均沒有提到,看來squid還不夠與時俱進。
下面講一下問題所在。
先看看yupoo api返回的HTTP頭部信息(cache 相關部分)
Cache-Control: no-cache, must-ridate
Pragma: no-cache
這兩行是控制瀏覽器的緩存行爲的,指示瀏覽器不得緩存。squid也是遵循RFC的,正常情況下自然不會去緩存這些頁面。override-expire override-lastmod reload-into-ims ignore-reload 統統不能對付它。
而那個補丁正是對付這兩個Cache-Control:no-cache 和 Pragma: no-cache的。
因此把 refresh_pattern那句要改寫成
來源:(http://blog.sina.com.cn/s/blog_5dc960cd0100d5fh.html) - 轉:squid緩存動態文件及cache-control參數_soulmate_新浪博客
refresh_pattern -i yupoo\.com 1440 50% 10080 override-expire override-lastmod reload-into-ims ignore-reload ignore-no-cache ignore-private
這樣就大功告成了, squid -k reconfigure 看看 access.log ,這回裏面終於出現
TCP_HIT/200 TCP_MEM_HIT/200 了,說明緩存規則確實起作用了,那個激動啊 555~~~~
====================
補充:
後來我看了一下google earth 服務器 hk1.google.com的HTTP頭部,只有
Expires: Wed, 02 Jul 2008 20:56:20 GMT
Last-Modified: Fri, 17 Dec 2004 04:58:08 GMT
,這麼看來照理不需ignore-no-cache ignore-private也能工作,可能是作者這裏寫錯了
kh.google 應該是 kh.\.google纔對。
最後總結一下,緩存Google Earth/Map的正確的配置應該是
acl QUERY urlpath_regex cgi-bin \? intranet
acl forcecache url_regex -i kh.\.google mt.\.google mapgoogle\.mapabc keyhole.com
no_cache allow forcecache
no_cache deny QUERY
# ----
refresh_pattern -i kh.\.google 1440 20% 10080 override-expire override-lastmod reload-into-ims ignore-reload ignore-no-cache ignore-private
refresh_pattern -i mt.\.google 1440 20% 10080 override-expire override-lastmod reload-into-ims ignore-reload ignore-no-cache ignore-private
refresh_pattern -i mapgoogle\.mapabc 1440 20% 10080 override-expire override-lastmod reload-into-ims ignore-reload ignore-no-cache ignore-private
refresh_pattern -i keyhole.com 1440 20% 10080 override-expire override-lastmod reload-into-ims ignore-reload ignore-no-cache ignore-private
注:
khX.google.com 是google earth的圖片服務器
mtX.google.com 是google map 的圖片服務器
mapgoogle.mapabc.com 是google ditu的圖片服務器
 
 
 
header中的Cache-control參數說明:
 
網頁的緩存是由HTTP消息頭中的“Cache-control”來控制的,常見的取值有private、no-cache、max-age、must-ridate等,默認爲private。其作用根據不同的重新瀏覽方式分爲以下幾種情況:

(1) 打開新窗口
    值爲private、no-cache、must-ridate,那麼打開新窗口訪問時都會重新訪問服務器。
而如果指定了max-age值,那麼在此值內的時間裏就不會重新訪問服務器,例如:
Cache-control: max-age=5(表示當訪問此網頁後的5秒內再次訪問不會去服務器)
  • Last-Modified: 告訴反向代理頁面什麼時間被修改
  • Expires: 告訴反向代理頁面什麼時間應該從緩衝區中刪除
  • Cache-Control: 告訴反向代理頁面是否應該被緩衝
  • Pragma: 用來包含實現特定的指令,最常用的是 Pragma:no-cache

    (2) 在地址欄回車
        值爲private或must-ridate則只有第一次訪問時會訪問服務器,以後就不再訪問。
        值爲no-cache,那麼每次都會訪問。
        值爲max-age,則在過期之前不會重複訪問。

    (3) 按後退按扭
       值爲private、must-ridate、max-age,則不會重訪問,
       值爲no-cache,則每次都重複訪問

    (4) 按刷新按扭
      無論爲何值,都會重複訪問
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章