Archive for Server

[Patch]让Squid对付Cache-Control no-store,must-revalidate

网站需要使用Squid对天气数据和flickr图片搜索做缓存

碰到的问题是这些数据的HTTP头部带有
Cache-Control: no-store, must-revalidate
Cache-Control: no-cache, must-revalidate 缓存控制.

但是Squid对CC中的no-store must-revalidate却无能为力

于是制作该补丁(for squid2.6STABLE),
给refresh_pattern增加两个参数ignore-no-store ignore-revalidate,使squid能对付这种情况

========================================
--- cache_cf_old.c Fri Jan 11 15:43:48 2008
+++ cache_cf.c Fri Jan 11 15:55:27 2008
@@ -2144,6 +2144,10 @@
storeAppendPrintf(entry, " ignore-private");
if (head->flags.ignore_auth)
storeAppendPrintf(entry, " ignore-auth");
+ if (head->flags.ignore_no_store)
+ storeAppendPrintf(entry, " ignore-no-store");
+ if (head->flags.ignore_revalidate)
+ storeAppendPrintf(entry, " ignore-revalidate");
#endif
storeAppendPrintf(entry, "\n");
head = head->next;
@@ -2166,6 +2170,8 @@
int ignore_no_cache = 0;
int ignore_private = 0;
int ignore_auth = 0;
+ int ignore_no_store = 0;
+ int ignore_revalidate = 0;
#endif
int i;
refresh_t *t;
@@ -2203,6 +2209,10 @@
ignore_private = 1;
else if (!strcmp(token, "ignore-auth"))
ignore_auth = 1;
+ else if (!strcmp(token, "ignore-no-store"))
+ ignore_no_store = 1;
+ else if (!strcmp(token, "ignore-revalidate"))
+ ignore_revalidate = 1;
else if (!strcmp(token, "reload-into-ims")) {
reload_into_ims = 1;
refresh_nocache_hack = 1;
@@ -2250,6 +2260,10 @@
t->flags.ignore_private = 1;
if (ignore_auth)
t->flags.ignore_auth = 1;
+ if (ignore_no_store)
+ t->flags.ignore_no_store = 1;
+ if (ignore_revalidate)
+ t->flags.ignore_revalidate = 1;
#endif
t->next = NULL;
while (*head)
--- structs_old.h Fri Jan 11 15:40:48 2008
+++ structs.h Fri Jan 11 15:41:57 2008
@@ -1944,6 +1944,8 @@
unsigned int ignore_no_cache:1;
unsigned int ignore_private:1;
unsigned int ignore_auth:1;
+ unsigned int ignore_no_store:1;
+ unsigned int ignore_revalidate:1;
#endif
} flags;
};
--- http_old.c Thu Jan 10 03:27:25 2008
+++ http.c Fri Jan 11 17:17:15 2008
@@ -241,7 +241,7 @@
return 0;
if (EBIT_TEST(cc_mask, CC_NO_CACHE) && !REFRESH_OVERRIDE(ignore_no_cache))
return 0;
- if (EBIT_TEST(cc_mask, CC_NO_STORE))
+ if (EBIT_TEST(cc_mask, CC_NO_STORE) && !REFRESH_OVERRIDE(ignore_no_store))
return 0;
if (httpState->request->flags.auth_sent) {
/*
--- refresh_old.c Thu Jan 10 03:27:40 2008
+++ refresh.c Fri Jan 11 17:17:43 2008
@@ -229,6 +229,10 @@
else if (request)
uri = urlCanonical(request);

+#define REFRESH_OVERRIDE(flag) \
+ ((R = (R ? R : refreshLimits(entry->mem_obj->url))) , \
+ (R && R->flags.flag))
+
debug(22, 3) ("refreshCheck: '%s'\n", uri ? uri : "");

if (delta > 0)
@@ -248,7 +252,7 @@
debug(22, 3) ("\tcheck_time:\t%s\n", mkrfc1123(check_time));
debug(22, 3) ("\tentry->timestamp:\t%s\n", mkrfc1123(entry->timestamp));

- if (EBIT_TEST(entry->flags, ENTRY_REVALIDATE) && staleness > -1) {
+ if ( !REFRESH_OVERRIDE(ignore_revalidate) && EBIT_TEST(entry->flags, ENTRY_REVALIDATE) && staleness > -1) {
debug(22, 3) ("refreshCheck: YES: Must revalidate stale response\n");
return STALE_MUST_REVALIDATE;
}

========================================

Patch下载地址: http://www.mipang.com/viewatt?attid=61578&disp=att&attkey=c3e9e7fa8a

原文发表在: http://www.mipang.com/groups/tiandi/t.23301.251.htm

评论(0)

freebsd 6.x-amd64下pecl-xdiff导致php crash

xdiff有什么用? 简单来说就是比较2个字符串的差异,例如这个页面

http://www.mipang.com/places/2037/bible/7962/history

这是使用xdiff来比较各个历史版本之间差异的例子.

之前我也安装过多次pecl-xdiff, 经常碰到编译成功,php载入该模块也没问题,但是当使用它提供的xdiff_xxx函数时就导致php崩溃. 尝试过不同的php版本和pecl-xdiff版本的组合,偶尔能正常运行,当时也没彻底解决这个问题.

最近要在一个特定php版本上安装这个pecl-xdiff 又碰到了这个情况.

解决办法:

在 config.h 文件中

/* #undef HAVE_XDL_ALLOCATOR_PRIV */
改成
#define HAVE_XDL_ALLOCATOR_PRIV 1

重新编译安装即可.

* 不确定其他平台有没有类似问题,写出来至少可以给大家一个解决的参考.

评论(4)

用Squid缓存Google Earth/Map数据

其实我本不想写这个标题,我的本意是缓存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的邮件列表里也提到。 ( 看标题进来的朋友,不要急,继续往下读,不会让你空手而回的 :) )

我也没在意,估计人家功力问题 :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-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的图片服务器

评论(4)