当squid遇上502 bad gateway

测试环境:

192.168.1.1 (squid) , 192.168.1.2 (nginx proxy) , 192.168.1.3 (nginx web)

squid 作为最外层的代理;nginx proxy 作为第二层代理,通过upstream访问后台;nginx web 真正的数据后台web server。

测试开始:

$curl -I -H host:blog.lazybug.me http://192.168.1.1/test.html
 
HTTP/1.0 200 OK
Server: nginx/0.8.32
Date: Mon, 08 Feb 2010 18:11:48 GMT
Content-Type: text/html; charset=GBK
Expires: Mon, 08 Feb 2010 18:12:58 GMT
Cache-Control: max-age=70
X-Cache: MISS from proxy.lazybug.me
X-Cache-Lookup: HIT from proxy.lazybug.me:80
Via: 192.168.1.3.nginx, 1.1 proxy.lazybug.me:80 (squid/2.7.STABLE7)
Connection: close

此时, user -> squid -> nginx proxy -> nginx web

此时,这个页面已经被缓存住,往后的访问都是TCP_MEM_HIT的了。等到页面过期后,我把nginx web 后台弄挂,我在这里是直接把nginx proxy的upstream给改了,改为 192.168.1.4。此时,访问nginx proxy

$curl -I -H host:blog.lazybug.me http://192.168.1.2/test.html
 
HTTP/1.1 502 Bad Gateway
Server: nginx/0.8.32
Date: Mon, 08 Feb 2010 18:28:06 GMT
Content-Type: text/html
Content-Length: 173
Connection: keep-alive

因为根本没有192.168.1.4这个ip,肯定bad gateway了。

但此时访问squid

$curl -I -H host:blog.lazybug.me http://192.168.1.1/test.html
 
HTTP/1.0 200 OK
Server: nginx/0.8.32
Date: Mon, 08 Feb 2010 18:11:48 GMT
Content-Type: text/html; charset=GBK
Expires: Mon, 08 Feb 2010 18:12:58 GMT
Cache-Control: max-age=70
Age: 1004
Content-Length: 0
X-Cache: HIT from proxy.lazybug.me
X-Cache-Lookup: HIT from proxy.lazybug.me:80
Via: 192.168.51.3.nginx, 1.1 proxy.lazybug.me:80 (squid/2.7.STABLE7)
Connection: close

squid 仍然返回200,不过并不是立刻返回,而是经过了几秒才返回,因为此时squid认为请求页面过期,正在尝试去后端取新数据,但此时,后端返回一个502 bad gateway,那它的工作机制就是,返回旧的缓存数据给用户。

看一下squid的access.log,写着TCP_REFRESH_FAIL_HIT。查一下手册,如下:

An expired copy of the requested object was in the cache. Squid attempted to make an If-Modified-Since request, but it failed. The old (stale) object was delivered to the client.

如果 squid 没有缓存到页面,而出现502,squid 会不会返回200呢??测试下

清缓存:
squidclient -p 80 -m purge http://blog.lazybug.me/test.html

然后再次访问:

$curl -I -H host:blog.lazybug.me http://192.168.1.1/test.html
 
HTTP/1.0 502 Bad Gateway
Server: nginx/0.8.32
Date: Tue, 09 Feb 2010 18:17:13 GMT
Content-Type: text/html
Content-Length: 173
X-Cache: MISS from proxy.lazybug.me
X-Cache-Lookup: HIT from proxy.lazybug.me:80
Via: 1.1 proxy.lazybug.me:80 (squid/2.7.STABLE7)
Connection: close

呵呵,不出所料。

———————————————————————

结论:squid 作为前端代理,而后端502的话,如果squid已经缓存住的页面,那squid就返回用户旧的数据,如果squid没有缓存住的话,很遗憾,还是会502的。

Submit a Comment