<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[squid2.6加速WEB支持虚拟主机配置心得体会[转]]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[服务器技术]]></category>
<pubDate>Thu, 28 Apr 2011 05:39:29 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	本人一台web服务器，日流量约10万，上面有好几个虚拟主机，近日装上Squid 2.6进行WEB加速，Squid 和Apache均在同一台服务器上面，效果非常明显，看到论坛上好多人问如何配置squid2.6支持,虚拟主机<br/>现在将安装过程贴出和大家一起分享，给菜鸟们一个学习机会和老鸟们一个批评指正的机会<br/><br/>主机配置为： CPU: AMD64 Sempron 3100&nbsp;&nbsp;内存： 2GB RAM<br/><br/>下载：wget&nbsp;&nbsp;<a href="http://www.squid-cache.org/Versi" target="_blank">http://www.squid-cache.org/Versi</a> ... 2.6.STABLE6.tar.bz2<br/><br/>tar jxvf squid-2.6.STABLE6.tar.bz2<br/><br/>安装： ./configure --with-maxfd=65536<br/><br/>这个--with-maxfd参数是增大squid文件描述符到65536<br/><br/>安装完毕后开始配置/usr/local/squid/etc/squid.conf<br/><div class="code"><br/>visible_hostname &#91;url&#93;www.yoursite.com&#91;/url&#93;<br/>http_port xx.xx.xx.xx:80 vhost vport<br/><br/>#xx.xx.xx.xx为这台服务器的IP地址<br/><br/>icp_port 0<br/><br/>cache_mem 400 MB<br/><br/>#设置Squid所能使用的内存共400MB，这个值因人而异<br/><br/>cache_swap_low 90<br/>cache_swap_high 95<br/><br/>maximum_object_size 20000 KB<br/><br/>#最大缓存文件大小，超过这个值则不缓存，这个值因人而异<br/><br/>maximum_object_size_in_memory 4096 KB<br/><br/>#装入内存缓存的文件大小，这个值对Squid的性能影响比较大，因为默认值是8K,超过8K的文件都不装入内存，而实际应用中很多网页和图片等都超过 8KB, 个人认为如果缓存不装入内存而存在磁盘上，性能和apache直接读取磁盘文件没什么区别，甚至不如直接访问apache，现在设置成小于4兆的文件通通装入内存缓存.<br/><br/>cache_dir ufs /tmp1 10000 16 256<br/><br/>#磁盘缓存的类型和目录，大小，一二级目录的设置，这里磁盘缓存大小是10G<br/><br/>cache_store_log none<br/><br/>#这个设置是不记录store.log<br/><br/>emulate_httpd_log on<br/><br/>#打开emulate_httpd_log选项,将使Squid仿照Aapche的日志格式<br/><br/>logformat combined %&gt;a %ui %un &#91;%tl&#93; &quot;%rm %ru HTTP/%rv&quot; %Hs %&lt;st &quot;%&#123;Referer&#125;&gt;h&quot; &quot;%&#123;User-Agent&#125;&gt;h&quot; %Ss:%Sh<br/><br/>#日志格式combined的设置<br/><br/>pid_filename /var/log/squid/squid.pid<br/>cache_log /var/log/squid/cache.log<br/>access_log /var/log/squid/access.log combined<br/><br/>#这里是设置pid和日志文件的位置，因人而异，同时日志格式是combined，awstats可以直接调用分析了<br/><br/>acl all src 0.0.0.0/0.0.0.0<br/><br/>acl QUERY urlpath_regex cgi-bin .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip .exe<br/>cache deny QUERY<br/><br/>#设置不想缓存的目录或者文件类型<br/><br/><br/>acl picurl url_regex -i &#92;.bmp$ &#92;.png$ &#92;.jpg$ &#92;.gif$ &#92;.jpeg$<br/>acl mystie1 referer_regex -i aaa<br/>http_access allow mystie1 picurl<br/>acl mystie2 referer_regex -i bbb<br/>http_access allow mystie2 picurl<br/><br/>#设置防图片盗链的，其中aaa,和bbb分别是虚拟主机的域名，referer中必须包含有aaa或者bbb的域名才能访问图片<br/><br/>acl nullref referer_regex -i ^$<br/>http_access allow nullref<br/>acl hasref referer_regex -i .+<br/>http_access deny hasref picurl<br/><br/>#设置允许直接访问图片和拒绝referer中没有包含aaa或着bbb的访问图片<br/><br/>cache_peer xx.xx.xx.xx parent 81 0 no-query originserver login=PASS<br/><br/>#xx.xx.xx.xx还是本机服务器的IP，81则是apache的端口，如果你的虚拟主机有用户名和密码保护起来的目录必须设置login=PASS，否则认证会失效<br/><br/>cache_effective_user nobody<br/>cache_effective_group nobody<br/><br/>#squid使用的用户组和用户名<br/><br/>squid配置完成！<br/><br/>建立缓存和日志目录，并改变权限使squid能写入<br/>mkdir /tmp1<br/>mkdir /var/log/squid<br/>chown -R nobody:nobody /tmp1<br/>chmod 666 /tmp1<br/>chown -R nobody:nobody /var/log/squid<br/>－－－－－－－－－－－－－－－－－－－－－－－<br/></div><br/>Apache需要改动的配置<br/><br/>Port 81<br/><br/>#要把端口改为81<br/><div class="code"><br/>NameVirtualHost xx.xx.xx.xx:81<br/>#本台主机IP和端口<br/><br/>虚拟主机配置<br/>&lt;VirtualHost xx.xx.xx.xx&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ServerAdmin &#91;email&#93;xxx@yahoo.com&#91;/email&#93;<br/>&nbsp;&nbsp;&nbsp;&nbsp;DocumentRoot /home/aaa/www<br/>&nbsp;&nbsp;&nbsp;&nbsp;ServerName aaa.com<br/>&nbsp;&nbsp;&nbsp;&nbsp;ServerAlias &#91;url&#93;www.aaa.com&#91;/url&#93;<br/>&nbsp;&nbsp;&nbsp;&nbsp;ScriptAlias /cgi-bin/ &quot;/home/aaa/cgi-bin/&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;Directory /&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Options Includes FollowSymLinks<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AllowOverride All<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/Directory&gt;<br/>&lt;/VirtualHost&gt;<br/></div><br/>如果还有别的虚拟主机请参照上面设置<br/><br/>---------------------------------------------------------<br/><br/>重启apache : apachectl restart<br/><br/>----------------------------------------------------------<br/>首次运行squid要先建立缓存<br/><br/>/usr/local/squid/sbin/squid -z<br/><br/>启动squid<br/><br/><div class="code"><br/>echo &quot;65535&quot; &gt; /proc/sys/fs/file-max<br/>ulimit -HSn 65535<br/>/usr/local/squid/sbin/squid<br/></div><br/>大家最好把这几句话放到squid启动脚本里面，这样才会获得65536文件描述符<br/><br/>最好还编辑/etc/hosts 文件<br/>添加以下内容<br/><br/><div class="code"><br/>xx.xx.xx.xx aaa.com www.aaa.com bbb.com&nbsp;&nbsp;www.bbb.com <br/></div><br/>这样免去查询DNS，速度也快一些<br/><br/><br/>现在大家肯定急着要打开浏览器访问你的网站看看效果吧，其实没啥变化，要等到有流量访问，squid把文件都装到内存后，效果才明显。可以用top命令观察squid的内存使用情况或者用<br/><br/>cat /var/log/squid/access.log &#124;grep TCP_MEM_HIT<br/><br/>如果看到很多的TCP_MEM_HIT ，这表明该文件是从内存缓存读取的，squid已经起作用了！你再用浏览器打开该文件，应该是快如闪电了。。呵呵，大功告成了！还有其他类型的HIT，如TCP_HIT等等，这些是从磁盘读取的，我觉得加速的意义不大，只不过缓解了apache的压力而已。<br/><br/>后记： 我的服务器因流量很大，大多都是静态的网页，日访问量常常上10W, apache常常不堪负重，苟延残存，任务数常到达300甚至400，后来安装squid2.6接管了大部分的apache，服务器如释重负，不但速度有提升，而且系统负载也低了很多，任务数稳定在100~120之间，任他流量波涛汹涌，服务器依然屹立不倒。不过squid就是比较吃内存，如果服务器能的内存加到4GB那就爽多了。<br/>Tags - <a href="http://www.dzhope.com/tags/squid/" rel="tag">squid</a> , <a href="http://www.dzhope.com/tags/apache/" rel="tag">apache</a> , <a href="http://www.dzhope.com/tags/%25E8%25B4%259F%25E8%25BD%25BD%25E4%25BC%2598%25E5%258C%2596/" rel="tag">负载优化</a>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] squid2.6加速WEB支持虚拟主机配置心得体会[转]]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>