<?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>Linux System Administrator</description>
	<lastBuildDate>Tue, 31 Jan 2012 08:28:25 +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>亚运赛事系统架构探索</title>
		<link>http://blog.helosa.org/2010/12/20/info-2010-163-com-architecture.html</link>
		<comments>http://blog.helosa.org/2010/12/20/info-2010-163-com-architecture.html#comments</comments>
		<pubDate>Sun, 19 Dec 2010 16:35:09 +0000</pubDate>
		<dc:creator>Chan Cham Chung</dc:creator>
				<category><![CDATA[technology]]></category>
		<category><![CDATA[haproxy]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[squid]]></category>

		<guid isPermaLink="false">http://blog.helosa.org/?p=368</guid>
		<description><![CDATA[广州亚运亚残在今天落幕了，一班亚运的同事将要离职，真让人伤感。随便写篇东西纪念一下我为亚运付出的一个月。 亚运赛事系统最初的系统架构，非常简单，就是前端用一堆组了lvs 的 squid 作为cache server ，后端跑nginx+resin 。 这个架构，优点就是够简单，如果cache server 不够，就横向扩展，同样简单。 但是，当整个系统的瓶颈不在缓存端时，缺点也很明显了。我发现，后端的load 很容易跑高，特点就是，缓存时间比较短（1 分钟），然后前端的穿透较多，再且，前端的访问很散列，并没有特别的热点。这样，如果作横向扩展，只会对后端的压力越大，因为穿透更多了。 之前去听SACC 2010 的，其实很多公司都用了一层haproxy 做 url hash 。于是，就对现有的架构作一次调整，希望对当时的负载情况有所帮助。 调整后的架构是这样的：最前端是一堆组了lvs 的haproxy ，作用是url hash 到后端的cache 。nginx 其实也是可以做url hash 的，在这里，选择nginx 还是 haproxy 呢，功能上nginx 确实稍胜一筹，但性能上 haproxy 也略优，选择什么也可以按个人爱好来定。呵呵。cache server 端，我也做了小调整，因为不需要只使用一个80 端口了，我一台服务器上跑两个squid 。用taskset 把2 个squid 绑定到cpu1 和 cpu2（默认都是cpu0 ） ，让cpu0 处理系统调用，cpu3 处理网络io 。一台4G 的服务器，在这个情况下已经算是把服务器使用得很尽了。cachedir 一律使用内存分区，不要出现磁盘io 。 但这个架构也不是绝对无敌，url hash [...]]]></description>
			<content:encoded><![CDATA[<p>广州亚运亚残在今天落幕了，一班亚运的同事将要离职，真让人伤感。随便写篇东西纪念一下我为亚运付出的一个月。</p>
<p>亚运赛事系统最初的系统架构，非常简单，就是前端用一堆组了lvs 的 squid 作为cache server ，后端跑nginx+resin 。</p>
<p>这个架构，优点就是够简单，如果cache server 不够，就横向扩展，同样简单。</p>
<p>但是，当整个系统的瓶颈不在缓存端时，缺点也很明显了。我发现，后端的load 很容易跑高，特点就是，缓存时间比较短（1 分钟），然后前端的穿透较多，再且，前端的访问很散列，并没有特别的热点。这样，如果作横向扩展，只会对后端的压力越大，因为穿透更多了。</p>
<p>之前去听SACC 2010 的，其实很多公司都用了一层haproxy 做 url hash 。于是，就对现有的架构作一次调整，希望对当时的负载情况有所帮助。</p>
<p>调整后的架构是这样的：最前端是一堆组了lvs 的haproxy ，作用是url hash 到后端的cache 。nginx 其实也是可以做url hash 的，在这里，选择nginx 还是 haproxy 呢，功能上nginx 确实稍胜一筹，但性能上 haproxy 也略优，选择什么也可以按个人爱好来定。呵呵。cache server 端，我也做了小调整，因为不需要只使用一个80 端口了，我一台服务器上跑两个squid 。用taskset 把2 个squid 绑定到cpu1 和 cpu2（默认都是cpu0 ） ，让cpu0 处理系统调用，cpu3 处理网络io 。一台4G 的服务器，在这个情况下已经算是把服务器使用得很尽了。cachedir 一律使用内存分区，不要出现磁盘io 。</p>
<p>但这个架构也不是绝对无敌，url hash 最怕遇到热点，特别热那种，就像110米跨栏决赛和男篮决赛。因为单一的url 并发过高，而导致其中一个cache server 跑满了一个cpu（url hash 把这个url 都调度到这个cache ） ，众所周知，squid 只能使用一个cpu ，这个cpu 跑满载了，请求就会开始阻塞了。然后连接数不断彪高，直接导致了前端的haproxy 也挂了，同时骨牌效应，所有的haproxy 都挂了。。。无奈，只好针对这个url 做特殊处理，rr 分配到后端多个squid ，而不走url hash 了。真是害人不浅阿。。。如果可以预先知道将会是热点的url ，这个问题将会更好解决，而不是要到haproxy 挂了才去处理。</p>
<p>末了，贴一下haproxy 的url hash 配置吧：</p>
<p>global<br />
log 127.0.0.1  local3 notice<br />
ulimit-n 409600<br />
maxconn 102400<br />
chroot /home/haproxy<br />
pidfile /home/haproxy/var/haproxy.pid<br />
user haproxy<br />
group haproxy<br />
nbproc  4<br />
daemon<br />
quiet</p>
<p>defaults<br />
log     global<br />
mode    http<br />
option  httplog<br />
option  dontlognull<br />
option  redispatch<br />
option  forwardfor<br />
option  httpclose<br />
option  log-separate-errors<br />
monitor-uri /do_not_delete/monitor.txt<br />
retries 3<br />
stats   uri     /haproxy-status<br />
maxconn         102400<br />
contimeout      5000<br />
clitimeout      50000<br />
srvtimeout      50000<br />
stats   auth    admin:123456</p>
<p>frontend http_server<br />
bind :80<br />
default_backend info_cache<br />
acl url_static path_end BKM400101.json<br />
use_backend info_cache_temp if url_static</p>
<p>backend info_cache<br />
option httpchk HEAD /live.js HTTP/1.1\r\nHost:\ info.2010.163.com<br />
balance uri len 15                            # url hash<br />
server  inst1 192.168.51.1:3128 check inter 5000 fall 3<br />
server  inst2 192.168.51.1:3129 check inter 5000 fall 3<br />
server  inst3 192.168.51.2:3128 check inter 5000 fall 3<br />
server  inst4 192.168.51.2:3129 check inter 5000 fall 3<br />
server  inst5 192.168.51.3:3128 check inter 5000 fall 3<br />
server  inst6 192.168.51.3:3129 check inter 5000 fall 3</p>
<p>backend info_cache_temp<br />
option httpchk HEAD /live.js HTTP/1.1\r\nHost:\ info.2010.163.com<br />
balance roundrobin                            # rr<br />
server  inst1 192.168.51.1:3128 check inter 5000 fall 3<br />
server  inst2 192.168.51.1:3129 check inter 5000 fall 3<br />
server  inst3 192.168.51.2:3128 check inter 5000 fall 3<br />
server  inst4 192.168.51.2:3129 check inter 5000 fall 3<br />
server  inst5 192.168.51.3:3128 check inter 5000 fall 3<br />
server  inst6 192.168.51.3:3129 check inter 5000 fall 3</p>
<p>其实这个架构只是一个调优，如果整套系统需要优化的话，还是需要从最初的设计入手，很多细节都要顾及。</p>
<p>细节包括，那些数据传输的地址，不要使用163.com 的，因为带了很大串的cookie ，用户每一次访问都带有这么一大串，是很占带宽的。还有根据不同的文件设置不同的缓存，这个最初没有设计好的话，js , css 等文件就混入到163.com 的域名中难以隔离。还需要根据具体的业务去配置一下缓存。业务数据，一定要从一开始就要有系统地收集统计，调优后对比分析。等等等等。。。</p>
<p>在这个架构下，前面的haproxy 只是纯代理，后面的cache server ，可以任意选择，经典一点的，可以用 squid 。或者上varnish ，再或者试用 ATS 。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.helosa.org/2010/12/20/info-2010-163-com-architecture.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>

