﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-遨游Code Sky-随笔分类-squid</title><link>http://www.cppblog.com/larryliuqing/category/18446.html</link><description>共享今天Code Sky, 共创明天Code Sky!</description><language>zh-cn</language><lastBuildDate>Tue, 03 Jan 2012 04:22:20 GMT</lastBuildDate><pubDate>Tue, 03 Jan 2012 04:22:20 GMT</pubDate><ttl>60</ttl><item><title>【转】squid简介</title><link>http://www.cppblog.com/larryliuqing/archive/2012/01/03/163474.html</link><dc:creator>刘远清</dc:creator><author>刘远清</author><pubDate>Tue, 03 Jan 2012 03:54:00 GMT</pubDate><guid>http://www.cppblog.com/larryliuqing/archive/2012/01/03/163474.html</guid><wfw:comment>http://www.cppblog.com/larryliuqing/comments/163474.html</wfw:comment><comments>http://www.cppblog.com/larryliuqing/archive/2012/01/03/163474.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/larryliuqing/comments/commentRss/163474.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/larryliuqing/services/trackbacks/163474.html</trackback:ping><description><![CDATA[<div style="color: #333333; font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 16px; line-height: 18px; font-weight: bold; height: 48px; padding-left: 30px; padding-top: 10px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dotted; border-right-style: dotted; border-bottom-style: dotted; border-left-style: dotted; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; overflow-x: hidden; overflow-y: hidden; text-align: -webkit-left; ">squid简介</div><div style="color: #999999; font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; line-height: 18px; margin-top: 5px; margin-right: 0px; margin-bottom: 8px; margin-left: 0px; text-align: -webkit-left; ">2008年12月11日 星期四 14:16</div><table style="table-layout: fixed; font-family: Arial; width: 958px; "><tbody><tr><td style="word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: 18px; color: #333333; "><div id="blog_text" style="color: #666666; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 14px; line-height: 30px; background-image: url(http://imgsrc.baidu.com/css/pic/item/8334ad430aed590772f05d38.jpg); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; padding-bottom: 80px; overflow-x: hidden; overflow-y: hidden; position: relative !important; border-image: initial; background-position: 100% 100%; background-repeat: no-repeat no-repeat; "><p style="line-height: normal; "><a name="top" style="text-decoration: none; color: rgb(81, 191, 224); "></a><a href="http://www.linux.gov.cn/netweb/squid.htm#top" style="text-decoration: none; color: #4d4d4d; ">简介</a><br /><a href="http://www.linux.gov.cn/netweb/squid.htm#conf" style="text-decoration: none; color: #4d4d4d; ">普通代理</a><br /><a href="http://www.linux.gov.cn/netweb/squid.htm#transconf" style="text-decoration: none; color: #4d4d4d; ">透明代理</a><br /><a href="http://www.linux.gov.cn/netweb/squid.htm#reverseconf" style="text-decoration: none; color: #4d4d4d; ">反向代理</a><br /><a href="http://www.linux.gov.cn/netweb/squid.htm#troubleshoot" style="text-decoration: none; color: #4d4d4d; ">故障处理</a><br /><a href="http://www.linux.gov.cn/netweb/squid.htm#example1" style="text-decoration: none; color: #4d4d4d; ">一个范例</a><br /><a href="http://www.linux.gov.cn/netweb/squid.htm#example2" style="text-decoration: none; color: #4d4d4d; ">一个限制访问的范例</a></p><p style="line-height: normal; ">官方网站：http://www.squid-cache.org/</p><p style="line-height: normal; ">相关技术网站：http://www.visolve.com/</p><h2><a name="intro" style="text-decoration: none; color: rgb(81, 191, 224); "></a>一. 代理服务简介</h2><a href="http://www.linux.gov.cn/netweb/squid.htm#top" style="text-decoration: none; color: #4d4d4d; line-height: normal; ">返回</a><h3>1. 什么是代理服务器(Proxy Server)</h3><p style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp; 代理服务可以代表其它计算机传递数据包或信息,这些数据包和信息包括网页、电子邮件、多媒体文件和其它网络应用程序等。通过它的文件缓存和访问控制等功能，可以实现快速浏览和对用户访问的有效管理。</p><p style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp; Web代理服务器(通常所说的代理服务器)是介于浏览器和Web服务器之间的一台服务器,当你通过代理服务器上网浏览时，浏览器不是直接到Web服务器去取回网页而是向代理服务器发出请求，由代理服务器来取回浏览器所需要的信息并传送给你的浏览器。 而且，大部分代理服务器都具有缓冲的功能，就好象一个大的Cache，它有很大的存储空间，它不断将新取得数据储存到它本机的存储器上，如果浏览器所请求的数据在它本机的存储器上已经存在而且是最新的，那么它就不重新从Web服务器取数据，而直接将存储器上的数据传送给用户的浏览器，这样就能显著提高浏览速度和效率。</p><p style="line-height: normal; ">　更重要的是：代理服务器是 Internet链路级网关所提供的一种重要的安全功能，它的工作主要在开放系统互联 (OSI) 模型的对话层。主要的功能有：<br />　　&nbsp;<br />　　1、连接Internet与Intranet 充当firewall（防火墙）：因为所有内部网的用户通过代理服务器访问外界时，只映射为一个IP地址，所以外界不能直接访问到内部网；同时可以设置 IP地址过滤，限制内部网对外部的访问权限；另外，两个没有互联的内部网，也可以通过第三方的代理服务器进行互联来交换信息。</p><p style="line-height: normal; ">　　2、共享因特网连接，节省IP开销：如前面所讲，所有用户对外只占用一个IP，所以不必租用过多的IP地址，降低网络的维护成本。这样，局域局内没有与外网相连的众多机器就可以通过内网的一台代理服务器连接到外网，大大减少费用。当然也有它不利的一面，如许多网络黑客通过这种方法隐藏自己的真实IP地址，而逃过监视。</p><p style="line-height: normal; ">　　3、提高访问速度，节约通信带宽。而且通常代理服务器都设置一个较大的硬盘缓冲区（可能高达几个GB或更大），当有外界的信息通过时，同时也将其保存到缓冲区中，当其他用户再访问相同的信息时，则直接由缓冲区中取出信息，传给用户，从而达到提高访问速度的目的。</p><h3>2. 代理服务器的硬件需求</h3><p style="line-height: normal; ">代理服务器对于硬件的要求相当的高！因为我们架设代理服务器的目的就是希望能够加快网络的传输速度因此，虽然代理服务器几乎在任何的 Linux 系统上面都能跑，但是代理服务器最好还是有较高的硬件配置：</p><p style="line-height: normal; ">CPU 足够好；</p><p style="line-height: normal; ">RAM 足够大，这也是很重要的一个硬件参数</p><p style="line-height: normal; ">Hard Disk 最好能用 SCSI 接口的，因为速度与稳定度都比较好！如果不能的话，那么 IDE 接口的硬盘由于目前速度也越来越快，所以使用 IDE 也没有问题，但是最好是『多块硬盘』的架构，例如我总共需要 30 GB 的硬盘空间，那么最好是 10 GB 的硬盘三块这样的架构较佳。 最好在架设代理服务器时，将整体主机的规划做好，并且让代理服务器主机的服务单纯一点，最好只负责代理服务</p><p style="line-height: normal; ">每颗硬盘的容量不需要太大，此外，最好将硬盘分割一下，一个分区在 2~4 GB 之间即可，因为分太大的话资料的搜寻耗费时间较长，但是分区太小又可能造成空间的浪费</p><p style="line-height: normal; ">cache 是放置在某个目录下的，而最好一个目录底下就是独立的一个partition 。此外，由于 cache 所在的硬盘常常会有资料的存取，因此可能此一硬盘的损耗率会比较大，所以这个 cache 所在的硬盘最好不要跟重要数据文件，例如 /， /etc， /usr， /home 等等重要的系统文件放在一起！</p><p style="line-height: normal; ">也由于 cache 所在的硬盘资料存取太密集了，所以，硬盘的选择上面需要 (1)转速不能太低； (2)磁头的机械臂需要可以忍受频繁的动作；(3)发热量不可太大，或者可以考虑加装硬盘用风扇。</p><p style="line-height: normal; ">网络卡与网络周边最好使用 GBytes 的网络卡</p><h3>3. 使用squid的好处</h3><p style="line-height: normal; ">squid是开源软件，性能优秀。并仍在世界各地的squid开发者的共同努力下，不断发展。</p><p style="line-height: normal; ">快速响应，减少网络阻塞,Squid将远程Internet对象保存为本地拷贝。当本地用户再次访问这些对象时，Squid可以直接快速地提供对这些对象的访问，而不必再次占用带宽访问远程服务器上的对象。</p><p style="line-height: normal; ">增强访问控制，提高安全性。可以针对特定的的网站、用户、网络、数据类型实施访问控制</p><p style="line-height: normal; ">squid可以工作在普通代理模式、透明代理模式各反向代理模式</p><h3>4. squid的结构</h3><p style="line-height: normal; ">多个squid代理服务器可以通过icp协议相互沟通，形成树形层次关系（父代理、兄弟代理、子代理），构建代理服务器群。</p><h2><a name="conf" style="text-decoration: none; color: rgb(81, 191, 224); "></a>二. squid.conf的典型配置（普通代理）</h2><p style="line-height: normal; "><a href="http://www.linux.gov.cn/netweb/squid.htm#top" style="text-decoration: none; color: #4d4d4d; ">返回</a></p><h3>1. 配置</h3><p style="line-height: normal; ">#vi /etc/squid/squid.conf</p><p style="line-height: normal; ">#http_port指令告诉squid在哪个端口侦听HTTP请求。默认端口是3128,除下面的形式外,也可以是http_port 192.168.63.50:3128<br />http_port 3128<br />icp_port 3130</p><p style="line-height: normal; ">#缓存目录的设置,可以设置多个缓存目录,语法为:&lt;cache_dir&gt; &lt;aufs|ufs&gt; &lt;目录所在&gt; &lt;MBytes大小&gt; &lt;dir1&gt; &lt;dir2&gt;<br />cache_dir ufs /var/spool/squid 1000 64 1024</p><p style="line-height: normal; ">#下面是关于日志文件的放置目录与文件名！<br />cache_access_log /var/log/squid/access.log<br />cache_log /var/log/squid/cache.log<br />cache_store_log /var/log/squid/store.log<br />pid_filename /var/run/squid.pid</p><p style="line-height: normal; ">#关闭认证机制，有些版本的　squid 会自动的加入代理认证机制，而普通情况下是不需要的,故找到包括auth_param的行，给它们加上注释<br />#auth_param basic children 5<br />#auth_param basic realm Squid proxy-caching web server<br />#auth_param basic credentialsttl 2 hours</p><p style="line-height: normal; ">#设置squid用户及用户组、管理员账号<br />cache_effective_user squid<br />cache_effective_group squid&nbsp;<br />cache_mgr youraccount@your.e.mail</p><p style="line-height: normal; "># 与内存有关的配置：因为我的系统内存很小，所以只给 8 MB！如果您的物理内存很大的情况下，例如 512 MB，可以考虑加大到 64 或 128 MB。<br />cache_mem 128 MB</p><p style="line-height: normal; "># 与磁盘容量有关的配置(注：下列的 90 与 95 是百分比 )，如果您的 cache_dir 所在磁盘很大时，可以考虑将 4096 改成 32768 KB<br />cache_swap_low 90<br />cache_swap_high 95<br />maximum_object_size 4096 KB</p><p style="line-height: normal; "># 与内存保存资料有关的配置<br />maximum_object_size_in_memory 8 KB</p><p style="line-height: normal; ">#定义acl(访问控制列表), 语法为:acl&lt;acl&gt; &lt;acl名称&gt; &lt;acl类型&gt; &lt;配置的内容&gt;<br />#黑体为用户自定义部分</p><p style="line-height: normal; ">acl All src 0/0&nbsp;<br />acl Manager proto cache_object&nbsp;<br />acl Localhost src 127.0.0.1/32&nbsp;<br />acl Safe_ports port 80 21 443 563 70 210 280 488 591 777 1025-65535&nbsp;<br />acl SSL_ports 443 563&nbsp;<br />acl CONNECT method CONNECT&nbsp;<br /><strong>acl MyNetwork src 192.168.0.0/16</strong><br /><br />#利用前面定义的acl,定义访问控制规则<br />http_access allow Manager Localhost<br />http_access deny Manager<br />http_access deny !Safe_ports<br />http_access deny CONNECT !SSL_ports<br /><strong>http_access allow MyNetwork</strong><br />http_access deny All</p><p style="line-height: normal; ">#定义与其它代理服务器的关系,语法: &lt;cache_peer&gt; &lt;主机名称&gt; &lt;类别&gt; &lt;http_port&gt; &lt;icp_port&gt; &lt;其它参数&gt;<br />cache_peer 192.168.60.6 parent 4480 7 no-query default</p><p style="line-height: normal; ">#设置与其它代理服务器的关系:<br /># &lt;cache_peer_access&gt; &lt;上层 Proxy &gt; &lt;allow|deny&gt; &lt;acl名称&gt;<br />#cache_peer_access 192.168.60.6 allow aclxxx&nbsp;<br />#cache_peer_access 192.168.60.6 deny !aclxxx</p><p style="line-height: normal; ">coredump_dir /var/spool/squid</p><h3>2. 激活squid</h3><p style="line-height: normal; ">1). 在开启squid之前，你应该验证其配置文件是否正确。运行如下命令即可：</p><p style="line-height: normal; "># squid -k parse</p><p style="line-height: normal; ">假如你看不到输出，配置文件有效，你能继续后面的步骤。然而，如果配置文件包含错误，squid会告诉你：</p><p style="line-height: normal; ">2). 初始化cache目录.即建立缓存目录的存储格式</p><p style="line-height: normal; ">只需在第一次启动squid服务之前执行(在初次运行squid之前，或者无论何时你增加了新的cache_dir，你必须初始化cache目录。)</p><p style="line-height: normal; "># squid -z</p><p style="line-height: normal; ">cache目录初始化可能花费一些时间，依赖于cache目录的大小和数量，以及磁盘驱动器的速度。假如你想观察这个过程，请使用-X选项：</p><p style="line-height: normal; "># squid -zX</p><p style="line-height: normal; ">3). 启动squid服务</p><p style="line-height: normal; "># service squid start</p><p style="line-height: normal; ">假定squid安装在/usr/local/squid目录下,也可以</p><p style="line-height: normal; "># /usr/local/squid/sbin/squid -sD</p><p style="line-height: normal; ">4). 停止squid</p><p style="line-height: normal; ">最安全的停止squid的方法是使用squid -k shutdown命令：</p><p style="line-height: normal; "># squid -k shutdown</p><p style="line-height: normal; ">5). 重配置运行中的squid进程</p><p style="line-height: normal; ">在你了解了更多关于squid的知识后，你会发现对squid.conf文件做了许多改动。为了让新设置生效，你可以关闭和重启squid,或者在squid运行时，重配置它。</p><p style="line-height: normal; ">重配置运行中的squid最好的方法是使用squid -k reconfigure命令：</p><p style="line-height: normal; "># squid -k reconfigure</p><p style="line-height: normal; ">6). 滚动日志文件</p><p style="line-height: normal; ">除非你在squid.conf里禁止，squid会写大量的日志文件。你必须周期性的滚动日志文件，以阻止它们变得太大。squid将大量的重要信息写入日志，假如写不进去了，squid会发生错误并退出。为了合理控制磁盘空间消耗，在cron里使用如下命令：</p><p style="line-height: normal; ">%squid -k rotate</p><p style="line-height: normal; ">例如，如下任务接口在每天的早上4点滚动日志：</p><p style="line-height: normal; ">0 4 * * * /usr/local/squid/sbin/squid -k rotate</p><p style="line-height: normal; ">该命令做两件事。首先，它关闭当前打开的日志文件。然后，通过在文件名后加数字扩展名，它重命名cache.log,store.log,和 access.log。例如，cache.log变成cache.log.0,cache.log.0变成cache.log.1,如此继续，滚动到 logfile_rotate选项指定的值。</p><p style="line-height: normal; ">squid仅仅保存每个日志文件的最后logfile_rotate版本。更老的版本在重命名过程中被删除。假如你想保存更多的拷贝，你需要增加logfile_rotate限制，或者编写脚本用于将日志文件移动到其他位置。 请见13.7章关于滚动日志的其他信息。</p><h3>3. 访问控制示例</h3><p style="line-height: normal; ">1) 禁止访问某个网站</p><p style="line-height: normal; ">在squid配置文件中添加以下acl名称及访问规则,并重新加载配置文件</p><p style="line-height: normal; ">acl sina dstdomain .sina.com.cn .sina.com<br />http_access deny sina</p><p style="line-height: normal; ">或</p><p style="line-height: normal; ">acl sina dst 58.63.236.26 58.63.236.27 58.63.236.28 58.63.236.29 58.63.236.30 58.63.236.31 58.63.236.32 58.63.236.33 58.63.236.34 58.63.236.35 58.63.236.36 58.63.236.37 58.63.236.38 58.63.236.39 58.63.236.49 58.63.236.50<br />http_access deny sina</p><p style="line-height: normal; ">或</p><p style="line-height: normal; ">acl sina dst www.sina.com.cn<br />http_access deny sina</p><p style="line-height: normal; ">2) 禁止来自某些IP地址的访问</p><p style="line-height: normal; ">在squid配置文件中添加以下acl名称及访问规则,并重新加载配置文件</p><p style="line-height: normal; ">acl zhang src 192.168.63.6/32<br />http_access deny zhang</p><p style="line-height: normal; ">3) 禁止在某些时段访问</p><p style="line-height: normal; ">acl Working_hours MTWHF 08:00-17:00<br />http_access allow Working_hours<br />http_access deny !Working_hours</p>4) 禁止某个代理客户建立过多连接<p style="line-height: normal; ">acl OverConnLimit maxconn 4&nbsp;<br />http_access deny OverConnLimit</p><h2><a name="transconf" style="text-decoration: none; color: rgb(81, 191, 224); "></a>三. 透明代理</h2><p style="line-height: normal; "><a href="http://www.linux.gov.cn/netweb/squid.htm#top" style="text-decoration: none; color: #4d4d4d; ">返回</a></p><p style="line-height: normal; ">让我们现在来想象一个联机状态，就是你有一整组内部网络，而这个内部网络都是透过 NAT 主机联机出去的。那么我们谈过，就是在一个内部网很大的情况下，使用 Proxy 是一个很不错的选择，因为至少他可以减轻带宽负荷！不过，遗憾的是，架设 Proxy 的时候，也要使用者在浏览器上面设置代理！那么有没有办法在『使用者不需要在浏览器上面进行任何配置，就可以实现以 Proxy 帮助使用者联接Internet？当然有啦！那就是 Transparent Proxy 啦！也有人翻译成『透明代理服务器』，其原理是：</p><p style="line-height: normal; ">当使用者经过 NAT 服务器来联机进入 Internet 时，假如使用的 Internet 协议为 80 (也就是 WWW) ，那么就将这个要求交给 Proxy 来工作，以达到代理服务器的功能。</p><p style="line-height: normal; "><br />呵呵！也就是说，当使用者是经过 NAT 主机联机出去时，只要让 NAT 主机发现『咦！你是要去读取 www 的资料对吧！好！那么这个动作由 Proxy 主机帮你搞定！』如此一来，使用者根本就不需要在浏览器上面配置 Proxy 的相关资料，因为这个动作是『由 NAT 主机自己决定的』，所以只要在 NAT 主机上面配置妥当即可，使用者不必配置任何资料呢！那么要怎么进行呢？只要两个步骤即可：</p><h3>1. 配置 Proxy 主机：</h3><p style="line-height: normal; ">1) 如果是squid 2.5,需要更改squid配置文件中的下列指令:</p><p style="line-height: normal; ">[root@test root]# vi /etc/squid/squid.conf&nbsp;<br />！&nbsp;<br />httpd_accel_host virtual # 告诉web加速器,针对所有的URL</p><p style="line-height: normal; ">httpd_accel_port 80 # 告诉web加速器，要监听的端口是80！</p><p style="line-height: normal; ">httpd_accel_with_proxy on # 这个很重要！因为配置 httpd_accel_host 之后， cache 的配置会自动被终止，必须要加上这个配置为 on 之后，才能提供 cache 的功能！<br />httpd_accel_uses_host_header on</p><p style="line-height: normal; ">2) 如果是squid 2.6,则只需要更改squid配置文件中的一个指令:</p><p style="line-height: normal; ">http_port 192.168.63.50:3128 transparent #192.168.63.50 就是squid服务器的地址</p><p style="line-height: normal; ">[root@test root]# squid -k reconfigure</p><h3>2. 配置 NAT 主机的 port map ：</h3><p style="line-height: normal; ">再来让我们到 NAT 主机上面看看先，因为需要将 80 这个 port 交给 Proxy 的 3128 来帮忙协助，所以你的防火墙 script 必须要加入这一段才行：　</p><p style="line-height: normal; ">#iptables -t nat -A PREROUTING -i eth0 -p tcp -s 192.168.62.0/24 --dport 80 -j REDIRECT --to-ports 3128</p><p style="line-height: normal; ">注意一下，那个 eth0 是『你的 NAT 对内的网卡』，至于 192.168.62.0/24 则是你的内部网域，请依照你的主机实际状态来配置！这样一来，您的 client 端完全不需要进行任何的配置，立刻就可以使用 Proxy 的好处啰</p><h3>3. 配置转发</h3><p style="line-height: normal; ">#echo 1 &gt; /proc/sys/net/ipv4/ip_forward</p><p style="line-height: normal; ">或：</p><p style="line-height: normal; ">#vi /etc/sysctl.conf</p><p style="line-height: normal; ">net_ipv4_forword=1</p><p style="line-height: normal; ">#vi /etc/sysconfig/network</p><p style="line-height: normal; ">FORWARD_IPV4=yes</p><p style="line-height: normal; ">#service network restart</p><h3>4. 配置客户端</h3><p style="line-height: normal; ">将另外一台计算机作为客户机进行测试，需要将网关、首选DNS服务器指向设置透明代理的服务器，清除客户端的浏览器代理设置。</p><h2><a name="reverseconf" style="text-decoration: none; color: rgb(81, 191, 224); "></a>四. 反向代理</h2><p style="line-height: normal; "><a href="http://www.linux.gov.cn/netweb/squid.htm#top" style="text-decoration: none; color: #4d4d4d; ">返回</a></p><p style="line-height: normal; ">代理服务器是使用非常普遍的一种将局域网主机联入互联网的一种方式，使用代理上网可以节约紧缺的IP地址资源，而且可以阻断外部主机对内部主机的访问，使内部网主机免受外部网主机的攻击。但是，如果想让互联网上的主机访问内部网的主机资源（例如：Web站点），又想使内部网主机免受外部网主机攻击，一般的代理服务是不能实现的，需要使用反向代理来实现。</p><p style="line-height: normal; ">什么是反向代理呢？其实，反向代理也就是通常所说的WEB服务器加速，它是一种通过在繁忙的WEB服务器和Internet之间增加一个高速的WEB缓冲服务器（即：WEB反向代理服务器）来降低实际的WEB服务器的负载。典型的结构如下图所示：</p><p style="line-height: normal; ">Web服务器加速（反向代理）是针对Web服务器提供加速功能的。它作为代理Cache，但并不针对浏览器用户，而针对一台或多台特定Web服务器（这也是反向代理名称的由来）。实施反向代理（如上图所示），只要将Reverse Proxy Cache设备放置在一台或多台Web服务器前端即可。当互联网用户访问某个WEB服务器时，通过DNS服务器解析后的IP地址是Reverse Proxy Server的IP地址,而非原始Web服务器的IP地址,这时Reverse Proxy Server设备充当Web服务器，浏览器可以与它连接，无需再直接与Web服务器相连。因此，大量Web服务工作量被卸载到反向代理服务上。不但能够防止外部网主机直接和web服务器直接通信带来的安全隐患，而且能够很大程度上减轻web服务器的负担，提高访问速度。</p><p style="line-height: normal; ">1． Squid反向代理单个后台WEB服务器</p><p style="line-height: normal; "><span style="font-size: 9pt; "><img height="260" src="http://www.linux.gov.cn/netweb/images/reverse_typical.gif" width="230" border="0"  alt="" /></span></p><p style="line-height: normal; ">1) 如果WEB服务器和反向代理服务器是两台单独的机器（一般的反向代理应该有两块网卡分别连接了内外部网络）。那么，应该修改下面的内容来设置反向代理服务。</p><p style="line-height: normal; ">http_port 80 #squid监听的端口<br />httpd_accel_host 172.16.250.250 #内部WEB服务器的IP地址<br />httpd_accel_port 80 #WEB服务的端口号<br />httpd_accel_single_host on #转发没有缓冲的请求到一台单独的机器，squid被设置成仅对单一的web服务器作反向代理<br />httpd_accel_with_proxy on #如果希望squid既作反向代理服务器又作本地机器的上网代理，需要将此设为on，默认是off<br />httpd_accel_uses_host_header off　#在HTTP协议1.1中，HTTP请求包括一个主机头信息，指定URL的主机名或者主机的IP地址。这个选项可以用来完成多个后台WEB服务器的反向代理功能。</p><p style="line-height: normal; ">2) 如果WEB服务器和反向代理服务器是同一台机器。那么，应该设置WEB服务器的监听端口为非80端口（比如：81端口）。要修改的内容如下：</p><p style="line-height: normal; ">http_port 80 #squid监听的端口<br />httpd_accel_host localhost #内部WEB服务器的IP地址<br />httpd_accel_port 81 #WEB服务器的端口号<br />httpd_accel_single_host on<br />httpd_accel_with_proxy on&nbsp;<br />httpd_accel_uses_host_header off</p><p style="line-height: normal; ">2．Squid反向代理多个后台WEB服务器</p><p style="line-height: normal; ">我们可以用Squid反向代理多个后台WEB服务器。例如：我们可以配置squid同时反向代理www.abc.com, www.xyz.com, www.lmn.com三个后台WEB服务器，示意图如下：</p><p style="line-height: normal; "><span style="font-size: 9pt; "><img height="260" src="http://www.linux.gov.cn/netweb/images/reverse_mult.gif" width="230" border="0"  alt="" /></span></p><p style="line-height: normal; ">Squid的配置如下：</p><p style="line-height: normal; ">httpd_accel_host virtual<br />httpd_accel_port 80&nbsp;<br />httpd_accel_single_host off&nbsp;<br />httpd_accel_uses_host_header on</p><p style="line-height: normal; ">（注意:编译Squid时需激活Internal DNS选项）</p><p style="line-height: normal; ">然后设置设置反响代理需要的域名解析（Internet用户通过这里解析三个网站的域名）如下：</p><p style="line-height: normal; ">www.abc.com 202.102.240.74<br />www.xyz.com 202.102.240.74<br />www.lmn.com 202.102.240.74</p><p style="line-height: normal; ">使三个域名都指向反向代理服务器的IP地址202.102.240.74。</p><p style="line-height: normal; ">下面设置反向代理所需要的DNS入口信息（即设置内部DNS，仅仅是squid在内部使用，Internet用户不可见）。有两种方法可以设置内部DNS，使用内部DNS服务器来解析或者使用/etc/hosts文件来实现。</p><p style="line-height: normal; ">使用内部DNS服务器的资源记录如下：<br />www.abc.com IN A 192.168.62.2<br />www.xyz.com IN A 192.168.62.3<br />www.lmn.com IN A 192.168.62.4</p><p style="line-height: normal; ">如果使用/etc/hosts文件来实现内部DNS（编译时应使用disable internal dns选项）,编辑/etc/hosts文件添加如下条目：&nbsp;<br />192.168.62.2 www.abc.com<br />192.168.62.3 www.xyz.com<br />192.168.62.4 www.lmn.com</p><p style="line-height: normal; ">结论：通过本文中的论述和相关实例，可以看出，反向代理方式不单是一种WEB服务器加速器，而且使也一种对外提供Web发布时使用的有效的防火墙技术，使用它不但能节约紧缺的IP地址资源，加速WEB服务器的访问速度，而且能够保护WEB主机，因此能够适应多种应用场合。</p><h2><a name="troubleshoot" style="text-decoration: none; color: rgb(81, 191, 224); "></a>五. 故障处理</h2><p style="line-height: normal; "><a href="http://www.linux.gov.cn/netweb/squid.htm#top" style="text-decoration: none; color: #4d4d4d; ">返回</a></p><p style="line-height: normal; ">1. 如果在启动squid时出现下述信息:</p><p style="line-height: normal; ">% squid -Nd1<br /><br />FATAL: Could not determine fully qualified hostname. Please set 'visible_hostname'</p><p style="line-height: normal; ">则需要在squid的配置文件中设置visible_hostname,如:</p><p style="line-height: normal; ">visible_hostname squid.packet-pushers.net</p><p style="line-height: normal; ">2. 如果在配置squid代理后,客户访问出现以下提示:</p><p style="line-height: normal; ">ERROR<br />The requested URL could not be retrieved</p><p style="line-height: normal; ">While trying to retrieve the URL: http://.../</p><p style="line-height: normal; ">可能需要在squid配置文件中加入以下指令:</p><p style="line-height: normal; ">always_direct allow all</p><h2><a name="example1" style="text-decoration: none; color: rgb(81, 191, 224); "></a>六. 一个范例</h2><p style="line-height: normal; "><a href="http://www.linux.gov.cn/netweb/squid.htm#top" style="text-decoration: none; color: #4d4d4d; ">返回</a></p><p style="line-height: normal; ">http_port 3128<br />icp_port 0</p><p style="line-height: normal; ">cache_mem 64 MB<br />cache_swap_low 80<br />cache_swap_high 85</p><p style="line-height: normal; ">maximum_object_size 32000 KB</p><p style="line-height: normal; ">cache_dir ufs /var/spool/squid 500 64 1024<br />cache_access_log /var/log/squid/access.log<br />cache_log /var/log/squid/cache.log<br />cache_store_log /var/log/squid/store.log</p><p style="line-height: normal; ">pid_filename /var/run/squid.pid</p><p style="line-height: normal; ">acl all src 0.0.0.0/0.0.0.0<br />acl alldst dst 0.0.0.0/0.0.0.0<br />acl manager proto cache_object<br />acl localhost src 127.0.0.1/255.255.255.255<br />acl to_localhost dst 127.0.0.0/8<br />acl 163 url_regex 163.com<br />acl sina url_regex sina.com.cn<br />acl myclient src 192.168.6.0/24</p><p style="line-height: normal; ">http_access allow 163<br />http_access allow sina<br />http_access deny !sina<br />http_access deny !163</p><p style="line-height: normal; ">cache_peer 192.168.1.8 parent 8080 7 no-query default</p><p style="line-height: normal; ">cache_effective_user squid<br />cache_effective_group squid<br />cache_mgr root@net03.org<br />coredump_dir /var/spool/squid</p><h2><a name="example2" style="text-decoration: none; color: rgb(81, 191, 224); "></a>七. 一个限制访问的范例</h2><p style="line-height: normal; "><a href="http://www.linux.gov.cn/netweb/squid.htm#top" style="text-decoration: none; color: #4d4d4d; ">返回</a></p><p style="line-height: normal; ">http_port 3128<br />icp_port 0<br />cache_mem 64 MB<br />cache_swap_low 80<br />cache_swap_high 85<br />maximum_object_size 32000 KB<br />cache_dir ufs /var/spool/squid 500 64 1024<br />cache_access_log /var/log/squid/access.log<br />cache_log /var/log/squid/cache.log<br />cache_store_log /var/log/squid/store.log<br />pid_filename /var/run/squid.pid<br />acl all src 0.0.0.0/0.0.0.0<br />acl manager proto cache_object<br />acl localhost src 127.0.0.1/255.255.255.255<br />acl to_localhost dst 127.0.0.0/8<br />acl SSL_ports port 443 563<br />acl Safe_ports port 80 # http<br />acl Safe_ports port 21 # ftp<br />acl Safe_ports port 443 563 # https, snews<br />acl Safe_ports port 70 # gopher<br />acl Safe_ports port 210 # wais<br />acl Safe_ports port 1025-65535 # unregistered ports<br />acl Safe_ports port 280 # http-mgmt<br />acl Safe_ports port 488 # gss-http<br />acl Safe_ports port 591 # filemaker<br />acl Safe_ports port 777 # multiling http<br />acl CONNECT method CONNECT<br />acl myclient src 192.168.6.0/24<br />acl 163 url_regex 163.com<br />acl sina url_regex sina.com.cn<br /># Only allow cachemgr access from localhost<br />http_access allow manager localhost<br />http_access deny manager<br /># Deny requests to unknown ports<br />http_access deny !Safe_ports<br /># Deny CONNECT to other than SSL ports<br />http_access deny CONNECT !SSL_ports</p><p style="line-height: normal; ">cache_peer 192.168.1.8 parent 8080 7 no-query default</p><p style="line-height: normal; ">acl ip_acl src 192.168.6.0/24<br />acl time_acl time M T W H F A S 9:00-19:00<br />http_access allow ip_acl time_acl</p><p style="line-height: normal; ">cache_peer_access 192.168.1.8 allow 163<br />cache_peer_access 192.168.1.8 deny !163<br />cache_peer_access 192.168.1.8 allow sina<br />cache_peer_access 192.168.1.8 deny !sina</p><p style="line-height: normal; ">cache_effective_user squid<br />cache_effective_group squid<br />cache_mgr root@net03.org<br />coredump_dir /var/spool/squid</p><p style="line-height: normal; ">#acl pl800_arp arp 00:08:c7:9f:34:41<br />#http_access allow pl800_arp</p></div></td></tr></tbody></table><img src ="http://www.cppblog.com/larryliuqing/aggbug/163474.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/larryliuqing/" target="_blank">刘远清</a> 2012-01-03 11:54 <a href="http://www.cppblog.com/larryliuqing/archive/2012/01/03/163474.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>