配置 squid 服务

什么是squid

Squid是比较知名的代理软件,它不仅可以跑在linux上还可以跑在windows以及Unix上,它的技术已经非常成熟。目前使用Squid的用户也是十分广泛的。SquidLinux下其它的代理软件如ApacheSocksTIS FWTKdelegate相比,下载安装简单,配置简单灵活,支持缓存和多种协议。

Squid的缓存功能相当好用,不仅可以减少带宽的占用,同样也大大降低了后台的WEB服务器的磁盘I/O的压力。Squid接收用户的下载申请,并自动处理所下载的数据。也就是说,当一个用户象要下载一个主页时,它向Squid发出一个申请,要Squid替它下载,然后Squid 连接所申请网站并请求该主页,接着把该主页传给用户同时保留一个备份,当别的用户申请同样的页面时,Squid把保存的备份立即传给用户,使用户觉得速度相当快。

Squid将数据元缓存在内存中,同时也缓存DNS查寻的结果,除此之外,它还支持非模块化的DNS查询,对失败的请求进行消极缓存。Squid支持SSL,支持访问控制。由于使用了ICPSquid能够实现重叠的代理阵列,从而最大限度的节约带宽。

Squid对硬件的要求是内存一定要大,不应小于128M,硬盘转速越快越好,最好使用服务器专用SCSI 硬盘,处理器要求不高,400MH以上既可。

安装squid

wget http://www.squid-cache.org/Versions/v2/2.6/squid-2.6.STABLE20.tar.gz

tar zxvf squid-2.6.STABLE20.tar.gz

cd squid-2.6.STABLE20ulimit -HSn 65535

useradd squid编译参数

./configure —prefix=/usr/local/squid \—disable-dependency-tracking \—enable-dlmalloc \—enable-gnuregex \—disable-carp \—enable-async-io=240 \—with-pthreads \—enable-storeio=ufs,aufs,diskd,null \—disable-wccp \—disable-wccpv2 \—enable-kill-parent-hack \—enable-cachemgr-hostname=localhost \—enable-default-err-language=Simplify_Chinese \—with-build-environment=POSIX_V6_ILP32_OFFBIG \—with-maxfd=65535 \—with-aio \—disable-poll \—enable-epoll \—enable-linux-netfilter \—enable-large-cache-files \—disable-ident-lookups \—enable-default-hostsfile=/etc/hosts \—with-dl \—with-large-files \—enable-removal-policies=heap,lru \—enable-delay-pools \—enable-snmp \—disable-internal-dnsmake && make install关于squid的版本,有必要提一下,目前squid最新版本已经到了3.1了,但是笔者认为2.6版本比较好用,如果你有兴趣可以研究一下3.1squid配置编辑配置文件 /usr/local/squid/etc/squid.conf把原来配置文件删除,替换成:http_port 80 transparentcache_replacement_policy lru #如果有多个(下面两行)缓存目录,则需要写这个参数cache_dir aufs /cache1 8192 16 256 #缓存目录1 /cache1 大小为8Gcache_dir aufs /cache2 4096 16 256 #缓存目录2 /cache2 大小为4G## 上面两行定义了缓存目录,这个缓存目录可以只有一个,也可以定义很多个。cache_mem 2048 MB #分配多少内存给squid,建议留至少512M给系统,如果你是虚拟机内存很小,只作为试验用的话,那就分一半内存给squidmaximum_object_size 2048 KB #缓存的文件最大不能超过2Mmaximum_object_size_in_memory 512 KB #缓存在内存中的文件最大不超过512kvisible_hostname cache.example.com #显示给用户的主机名client_persistent_connections off #client端关闭长连接server_persistent_connections on #server端打开长连接memory_pools onmemory_pools_limit 1024 MBforwarded_for onlog_icp_queries offcache_mgr cache@example.com #定义管理员的mailcache@example.comvia onhttpd_suppress_version_string offcache_effective_user squid #定义以squid用户的身份运行squidcache_effective_group squiderror_directory /usr/local/squid/share/errors/Simplify_Chineseicon_directory /usr/local/squid/share/iconsmime_table /usr/local/squid/etc/mime.confie_refresh offtcp_recv_bufsize 32 KBacl all src 0.0.0.0/0.0.0.0acl localhost src 127.0.0.0/8acl Mgr_ip src 127.0.0.0/8acl allow_ip dst 127.0.0.0/8 192.168.0.0/16 #定义允许代理的webIP或者IPacl PURGE method PURGEacl Safe_ports port 80 8080acl CONNECT method CONNECTacl manager proto cache_objectacl HTTP proto HTTPhttp_access allow allow_iphttp_access allow manager Mgr_iphttp_access deny managerhttp_access deny PURGEhttp_access deny !Safe_portshttp_access deny allicp_access deny allipcache_size 1024ipcache_low 90ipcache_high 95memory_replacement_policy lruhosts_file /etc/hostsrequest_header_max_size 128 KBhierarchy_stoplist cgi-bin ? .php .htmlacl QUERY urlpath_regex cgi-bin \? .php .htmlcache deny QUERYquick_abort_min -1 KBquick_abort_max 32 KBquick_abort_pct 95# error page#error_map http://www.92csz.com/404.html 403#deny_info http://www.92csz.com/error.html cctv_Domain# timeoutpeer_connect_timeout 20 secondsconnect_timeout 20 secondsread_timeout 60 secondsrequest_timeout 20 secondspconn_timeout 20 secondsshutdown_lifetime 5 secondsstrip_query_terms officp_port 0# logfileemulate_httpd_log onlogformat combined %>a %ui %un [%tl] “%rm %ru HTTP/%rv” %Hs %h" "%>h" %Ss:%Sh #access_log /log/squid-log/access.log combined cache_store_log /dev/null cache_log /var/log/squid/cache.log logfile_rotate 12 # MISCELLANEOUS store_objects_per_bucket 15 client_db off修改完配置文件后保存,然后初始化squidmkdir /cache1 /cache2 /var/log/squidchown -R squid:squid /cache1 /cache2 /var/log/squid/usr/local/squid/sbin/squid -z

# 用来生成cache目录,如果你的配置文件配置出错,往往会在初始化的时候报错,错误信息会直接显示在屏幕上。初始化成功后,就可以启动squid了,启动命令为:

nohup /usr/local/squid/bin/RunCache &

启动后,可以去看看cache.log 在这个日志中,你可以看到很多关于squid的信息,当然也包括一些错误日志。

如果想开机启动则需要在/etc/rc.d/rc.local中最后加入一行

/usr/local/bin/RunCache &

到这里算是配置完成了,但是还有一个问题,就是如何定义被代理的web以及域名?单单看配置文件并没有说代理的web是哪一个。确实,这个配置文件其实可以代理多台web,只要你在/etc/hosts中定义要代理的域名以及IP即可,hosts格式在前面已经介绍过。笔者要提醒你的是,如果是一台web上的多个域名,请不要写一行,虽然hosts是允许的,但是如果写成一个IP对应多个域名,squid代理时就会出错。所以有几个域名就要写几行。

更改/etc/hosts后要重启squid才能生效:

/usr/local/squid/sbin/squid -krec

在重启前可以先检测一下,是否有错,命令为:

/usr/local/squid/sbin/squid –kcheck

如果没有错,则不会显示任何信息,否则会显示一些信息出来。