用Squid3搭建缓存代理服务器

  最近在实验室研究CDN和缓存的一些内容,看了很多Squid缓存服务器的资料,于是想在自己电脑上搭建和配置一个Squid缓存代理,看看它的工作流程,下面是自己做的一些步骤。

  实验环境:

      

  环境很简单,都是在自己的电脑上跑的,客户机win7系统,缓存代理为win7内的ubuntu 12.04虚拟机,win7内chrome浏览器通过缓存代理访问外网资源。

      环境配置:

  1.安装squid3

   sudo apt-get install squid3

  2.修改配置文件

   squid3的配置文件和squid2的位置不太一样,我安装后的配置文件目录为/etc/squid3/squid.conf/

   配置文件很长,用gedit打开方便查找修改。 gedit /etc/squid3/squid.conf

   2.1 找到http_port修改项

   # Squid normally listens to port 3128

   http_port 3128

   http_port 80

   注释显示Squid默认监听3128端口,此时加入监听80端口,因为在之后使用中发现,使用squidclient查看squid状态的请求是通过80端口发向squid服务器的,如果不监听80端口则无法进行一些查看缓存状态的命令。

         2.2 找到http_access allow 项

   acl manager proto cache_object

   acl managercache src 127.0.0.1/32 ::1           允许本地ip进行缓存管理

   acl Purge method PURGE                               允许PURGE

   acl localhost src 127.0.0.1/32 ::1                   

   acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1

        http_access allow managercache Purge

   这里是修改acl访问控制规则,以后需要从本地用PURGE命令来清除Squid缓存,所以加入了红色的三条访问控制规则,其他acl如localhost,to_localhost,manager,在后续配置文件中已经存在http_access allow 项,所以不需要再自己加入。

   2.3 找到cahce_dir

   cache_dir ufs /var/spool/squid3 100 16 256 

   这里是设置缓存的目录,上面的参数是100M缓存空间,一级目录16,二级目录256,后面可以看一下具体的缓存目录情况。

   2.4 其他参数

   还有一些其他参数如mem_cache等等,具体可以参考http://www.php3.cn/squid/chap01.html

   2.5重新加载配置文件

        squid3 -k reconfigure

   一定要重新加载,否则修改无效!

  3.通过缓存代理上网

   3.1浏览器设置

   

   使用Chrome浏览器的SwitchySharp插件,设置代理ip为缓存服务器ip 10.108.140.66 端口 3128

   3.2 查看缓存服务器状态

   浏览器浏览一些网页后,可以用命令查看Squid状态。

   squidclient -p 80 mgr:info

   ==================输出================================         

root@zjf-VirtualBox:/etc/squid3# squidclient -p 80 mgr:info
HTTP/1.0 200 OK
Server: squid/3.1.19
Mime-Version: 1.0
Date: Tue, 28 Oct 2014 07:57:50 GMT
Content-Type: text/plain
Expires: Tue, 28 Oct 2014 07:57:50 GMT
Last-Modified: Tue, 28 Oct 2014 07:57:50 GMT
X-Cache: MISS from localhost
X-Cache-Lookup: MISS from localhost:3128
Via: 1.0 localhost (squid/3.1.19)
Connection: close

Squid Object Cache: Version 3.1.19
Start Time: Tue, 28 Oct 2014 06:14:20 GMT
Current Time: Tue, 28 Oct 2014 07:57:50 GMT
Connection information for squid:
Number of clients accessing cache: 2
Number of HTTP requests received: 1744
Number of ICP messages received: 0
Number of ICP messages sent: 0
Number of queued ICP replies: 0
Number of HTCP messages received: 0
Number of HTCP messages sent: 0
Request failure ratio: 0.00
Average HTTP requests per minute since start: 16.9
Average ICP messages per minute since start: 0.0
Select loop called: 640137 times, 9.700 ms avg
Cache information for squid:
Hits as % of all requests: 5min: 0.0%, 60min: 0.0%
Hits as % of bytes sent: 5min: 100.0%, 60min: 100.0%
Memory hits as % of hit requests: 5min: 0.0%, 60min: 0.0%
Disk hits as % of hit requests: 5min: 0.0%, 60min: 0.0%
Storage Swap size: 35560 KB
Storage Swap capacity: 34.7% used, 65.3% free
Storage Mem size: 8104 KB
Storage Mem capacity: 99.5% used, 0.5% free
Mean Object Size: 14.09 KB
Requests given to unlinkd: 61
Median Service Times (seconds) 5 min 60 min:
HTTP Requests (All): 0.00000 0.00000

......

=================================================================================

也可以查看Squid日志,里面记录了缓存Miss和Hit的一些情况,目录为/var/log/squid3/

或者查看/var/spool/squid3/缓存存储目录,里面有缓存下来的文件,是分级存放的。

     

    经过以上步骤,一个最简单的缓存代理就搭建完了,可以用来做一些实验,了解缓存代理的工作原理。

posted @ 2014-10-28 16:06  buptzjf  阅读(1352)  评论(0编辑  收藏  举报