歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> squid 強制緩存動態頁面

squid 強制緩存動態頁面

日期:2017/3/1 18:07:15   编辑:Linux技術

其實我本不想寫這個標題,我的本意是緩存yupoo api的查詢數據,這個過程中找到了參考方法(Caching Google Earth with Squid)。呵呵,所以偶也來一回標題黨。

這篇參考流傳非常廣,Digg上也被提過,我也不知道原出處是哪裡了。

可是。。。。你按照它的指示設置,它並不能正確工作!!

話說回來,先說說我的需求。

最近yupoo的訪問速度很慢,我有一堆api請求經常無法完成,猜測要麼對方限制了同一ip的連接數,要麼是yupoo又遇到了新一輪的流量瓶頸。跟Yupoo的zola聯系後,確認是他們的負荷太高引起的,並沒有限制連接數。所以我要想辦法在我這邊做一些緩存了。

因為我這邊本身就是用squid代理來解決Ajax中調用API的跨域問題的,所以自然是目標瞄准了squid的配置文件。

yupoo api的請求地址是 www.yupoo.com/api/rest/?method=xx&xxxxxxx...

大家都知道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的郵件列表裡也提到。 ( 看標題進來的朋友,不要急,繼續往下讀,不會讓你空手而回的 )

我也沒在意,估計人家功力問題 。先試著用改寫一下解決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-revalidate
Pragma: no-cache

這兩行是控制浏覽器的緩存行為的,指示浏覽器不得緩存。squid也是遵循RFC的,正常情況下自然不會去緩存這些頁面。override-expire override-lastmod reload-into-ims ignore-reload 統統不能對付它。

而那個補丁正是對付這兩個Cache-Control:no-cache 和 Pragma: no-cache的。

因此把 refresh_pattern那句要改寫成

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的圖片服務器
http://nukq.malmam.com/archives/16

Copyright © Linux教程網 All Rights Reserved