<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hello, SA &#187; squid</title>
	<atom:link href="http://blog.helosa.org/tag/squid/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.helosa.org</link>
	<description>Could u fix the problem for me ?</description>
	<lastBuildDate>Sat, 04 Sep 2010 19:51:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>当squid遇上502 bad gateway</title>
		<link>http://blog.helosa.org/2010/02/09/squid-502-bad-gateway.html</link>
		<comments>http://blog.helosa.org/2010/02/09/squid-502-bad-gateway.html#comments</comments>
		<pubDate>Mon, 08 Feb 2010 18:45:46 +0000</pubDate>
		<dc:creator>Chan Cham Chung</dc:creator>
				<category><![CDATA[technology]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[squid]]></category>

		<guid isPermaLink="false">http://lazybug.me/?p=56</guid>
		<description><![CDATA[测试环境： 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 &#160; 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 [...]]]></description>
			<content:encoded><![CDATA[<p>测试环境：</p>
<p>192.168.1.1 (squid) , 192.168.1.2 (nginx proxy) , 192.168.1.3 (nginx web)</p>
<p>squid 作为最外层的代理；nginx proxy 作为第二层代理，通过upstream访问后台；nginx web 真正的数据后台web server。</p>
<p>测试开始：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$curl</span> <span style="color: #660033;">-I</span> <span style="color: #660033;">-H</span> host:blog.lazybug.me http:<span style="color: #000000; font-weight: bold;">//</span>192.168.1.1<span style="color: #000000; font-weight: bold;">/</span>test.html
&nbsp;
HTTP<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.0</span> <span style="color: #000000;">200</span> OK
Server: nginx<span style="color: #000000; font-weight: bold;">/</span>0.8.32
Date: Mon, 08 Feb <span style="color: #000000;">2010</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">11</span>:<span style="color: #000000;">48</span> GMT
Content-Type: text<span style="color: #000000; font-weight: bold;">/</span>html; <span style="color: #007800;">charset</span>=GBK
Expires: Mon, 08 Feb <span style="color: #000000;">2010</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">12</span>:<span style="color: #000000;">58</span> GMT
Cache-Control: max-age=<span style="color: #000000;">70</span>
X-Cache: MISS from proxy.lazybug.me
X-Cache-Lookup: HIT from proxy.lazybug.me:<span style="color: #000000;">80</span>
Via: 192.168.1.3.nginx, <span style="color: #000000;">1.1</span> proxy.lazybug.me:<span style="color: #000000;">80</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>squid<span style="color: #000000; font-weight: bold;">/</span>2.7.STABLE7<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Connection: close</pre></div></div>

<p>此时， user -&gt; squid -&gt; nginx proxy -&gt; nginx web</p>
<p>此时，这个页面已经被缓存住，往后的访问都是TCP_MEM_HIT的了。等到页面过期后，我把nginx web 后台弄挂，我在这里是直接把nginx proxy的upstream给改了，改为 192.168.1.4。此时，访问nginx proxy</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$curl</span> <span style="color: #660033;">-I</span> <span style="color: #660033;">-H</span> host:blog.lazybug.me http:<span style="color: #000000; font-weight: bold;">//</span>192.168.1.2<span style="color: #000000; font-weight: bold;">/</span>test.html
&nbsp;
HTTP<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.1</span> <span style="color: #000000;">502</span> Bad Gateway
Server: nginx<span style="color: #000000; font-weight: bold;">/</span>0.8.32
Date: Mon, 08 Feb <span style="color: #000000;">2010</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">28</span>:06 GMT
Content-Type: text<span style="color: #000000; font-weight: bold;">/</span>html
Content-Length: <span style="color: #000000;">173</span>
Connection: keep-alive</pre></div></div>

<p>因为根本没有192.168.1.4这个ip，肯定bad gateway了。</p>
<p>但此时访问squid</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$curl</span> <span style="color: #660033;">-I</span> <span style="color: #660033;">-H</span> host:blog.lazybug.me http:<span style="color: #000000; font-weight: bold;">//</span>192.168.1.1<span style="color: #000000; font-weight: bold;">/</span>test.html
&nbsp;
HTTP<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.0</span> <span style="color: #000000;">200</span> OK
Server: nginx<span style="color: #000000; font-weight: bold;">/</span>0.8.32
Date: Mon, 08 Feb <span style="color: #000000;">2010</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">11</span>:<span style="color: #000000;">48</span> GMT
Content-Type: text<span style="color: #000000; font-weight: bold;">/</span>html; <span style="color: #007800;">charset</span>=GBK
Expires: Mon, 08 Feb <span style="color: #000000;">2010</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">12</span>:<span style="color: #000000;">58</span> GMT
Cache-Control: max-age=<span style="color: #000000;">70</span>
Age: <span style="color: #000000;">1004</span>
Content-Length: <span style="color: #000000;">0</span>
X-Cache: HIT from proxy.lazybug.me
X-Cache-Lookup: HIT from proxy.lazybug.me:<span style="color: #000000;">80</span>
Via: 192.168.51.3.nginx, <span style="color: #000000;">1.1</span> proxy.lazybug.me:<span style="color: #000000;">80</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>squid<span style="color: #000000; font-weight: bold;">/</span>2.7.STABLE7<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Connection: close</pre></div></div>

<p>squid 仍然返回200，不过并不是立刻返回，而是经过了几秒才返回，因为此时squid认为请求页面过期，正在尝试去后端取新数据，但此时，后端返回一个502 bad gateway，那它的工作机制就是，返回旧的缓存数据给用户。</p>
<p>看一下squid的access.log，写着TCP_REFRESH_FAIL_HIT。查一下手册，如下：</p>
<blockquote><p>
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.
</p></blockquote>
<p>如果 squid 没有缓存到页面，而出现502，squid 会不会返回200呢？？测试下</p>
<p>清缓存：<br />
squidclient -p 80 -m purge http://blog.lazybug.me/test.html</p>
<p>然后再次访问：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$curl</span> <span style="color: #660033;">-I</span> <span style="color: #660033;">-H</span> host:blog.lazybug.me http:<span style="color: #000000; font-weight: bold;">//</span>192.168.1.1<span style="color: #000000; font-weight: bold;">/</span>test.html
&nbsp;
HTTP<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.0</span> <span style="color: #000000;">502</span> Bad Gateway
Server: nginx<span style="color: #000000; font-weight: bold;">/</span>0.8.32
Date: Tue, 09 Feb <span style="color: #000000;">2010</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">17</span>:<span style="color: #000000;">13</span> GMT
Content-Type: text<span style="color: #000000; font-weight: bold;">/</span>html
Content-Length: <span style="color: #000000;">173</span>
X-Cache: MISS from proxy.lazybug.me
X-Cache-Lookup: HIT from proxy.lazybug.me:<span style="color: #000000;">80</span>
Via: <span style="color: #000000;">1.1</span> proxy.lazybug.me:<span style="color: #000000;">80</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>squid<span style="color: #000000; font-weight: bold;">/</span>2.7.STABLE7<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Connection: close</pre></div></div>

<p>呵呵，不出所料。</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>结论：squid 作为前端代理，而后端502的话，如果squid已经缓存住的页面，那squid就返回用户旧的数据，如果squid没有缓存住的话，很遗憾，还是会502的。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.helosa.org/2010/02/09/squid-502-bad-gateway.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
