详解用squid3.0搭建Web缓存和反向代理服务器

作者:网络医生 发布于:2011-5-26 13:55 Thursday 分类:Linux技术

1、介绍

  Squid是一个非常出名的代理软件,免费开源,不仅可以做正向代理、反向代理和透明代理还可以做为Web缓存服务器来加速度点访问,但Squid不支持多CPU,这一点是个遗憾。

2、下载软件,这里采用squid3.0 版本

  [root@WebServer home]# wget http://www.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE25.tar.gz

3、解压

  tar zxvf squid-3.0.STABLE25.tar.gz     /*解压出来*/

  cd squid-3.0.STABLE25        /*进入源码文件目录*/

 4、配置并安装

[root@WebServer squid-3.0.STABLE25]# ./configure --prefix=/home/squid \
 --sysconfdir=/etc \
 --enable-gnuregex \
 --with-filedescriptors=65536 \
 --enable-icmp \
 --enable-kill-parent-hack \
 --enable-cache-digests \
 --enable-arp-acl \
 --with-large-files \
 --enable-linux-netfilter \
 --enable-linux-tproxy \
 --enable-auth="basic" \
 --enable-basic-auth-helpers="NCSA" \
 --enable-storeio=ufs,aufs  \
 --with-default-user=squid \
 --with-pthreads \
 --with-aio \
 --enable-default-err-language="Simplify_Chinese" \
 --enable-err-languages="Simplify_Chinese" \
 --enable-ssl \
 --enable-x-accelerator-vary \

 --disable-mempools

make;make install

squid3.0配置参数说明:
--enable-dlmalloc    不再需要
--enable-epoll    系统会自己选择最优的I/O Loop Method
--enable-truncate     不再需要
--enable-follow-x-forwarded-for    没有此项配置了

--prefix=/home/squid    /*指定安装路径*/
--sysconfdir=/etc     /*指定配置文件的路径*/
--enable-gnuregex       /*增加对字符串的判断处理能力*/
--with-filedescriptors=65536  /*文件描述符个数,必须是64的整数倍*/
--enable-icmp          /*增加对icmp的支持*/
--enable-kill-parent-hack   /*关掉squid时连同父进程一起关掉*/
--enable-cache-digests    /*加快检索缓存的速度*/
--enable-arp-acl         /*可在规则中设置通过MAC地址进行管理,防止IP欺骗*/
--with-large-files       /*支持大文件,如日志文件等*/
--enable-linux-netfilter  /*支持linux透明代理*/
--enable-linux-tproxy   /*支持透明代理*/
--enable-auth="basic"     /*支持basic模块身份认证*/
--enable-basic-auth-helpers="NCSA" /*支持NCSA身份验证*/
--enable-storeio=ufs,aufs        /*支持的存储模块*/
--with-default-user=squid        /*以squid用户身份运行,需要先创建squid用户*/
--with-pthreads                 /*支持POSIX线程*/
--with-aio                         /*支持POSIX AIO*/
--enable-default-err-language="Simplify_Chinese"    /*默认的错误提示语言*/
--enable-err-languages="Simplify_Chinese"            /*支持简体中文*/
--enable-ssl     /*支持openssl加密*/
--enable-x-accelerator-vary

--disable-mempools   /*关闭内存池,以防止squid运行时间长了会变慢*/

make;make install          /*编译并安装*/

5、配置squid.conf

##################  基本信息配置 ##################
#设置代理服务器名称
visible_hostname WebCacheProxy
#设置缓存服务器管理员邮箱
cache_mgr mail@zcu.edu.cn
#设置运行squid的用户和组
cache_effective_user squid
cache_effective_group squid

#################缓存相关参数设置################
#设置缓存内存大小,最大使用内存为600M,

#当使用超过95%时开始对缓存进行替换,直到下降到90%后停止。
cache_mem 600 MB
cache_swap_low 90
cache_swap_high 95

#设置磁盘中可缓存的最大文件大小
maximum_object_size 12000 KB
#设置内存中最大可缓存的最大文件大小
maximum_object_size_in_memory 1024 KB
#设置缓存文件夹的路径和参数,10000表示10G大小,目录下面分为16级,每级又有256个目录
cache_dir ufs /home/squidcache 10000 16 256
#设置缓存日志文件路径
cache_access_log /home/squidlogs/access.log
cache_log /home/squidlogs/cache.log
cache_store_log /home/squidlogs/store.log


###########反向代理相关设置#################################
#设置反向代理服务器监听的端口为80,accel表示开启squid的accel加速模式,

#vhost和vport表示支持虚拟主机和虚拟端口,如果再加上transparent表示支持透明代理
http_port 80 accel vhost vport
#cache_peer表示如果本机缓存中找不到客户端请求的数据,将与主机www.zcu.com以parent类型进行联系,no-query表示不使用ICP协议进行联系,而是使用HTTP协议进行联系,联系端口是80,orginserver表示此服务器是源服务器,name表示别名。
cache_peer www.zcu.com parent 80 0 no-query originserver name=www
cache_peer ao.zcu.com parent 80 0 no-query originserver name=ao
cache_peer stu.zcu.com parent 80 0 no-query originserver name=stu

#设置别名所对应的域名,如果cache_peer中使用域名而不是IP的话,那么cache_peer_domain中一定要用相同的域名,否则无法访问。
cache_peer_domain www www.zcu.com
cache_peer_domain ao ao.zcu.com
cache_peer_domain stu stu.zcu.com


#设置访问控制,允许所有客户端访问上面设置的三个网站
http_access allow all

6、配置/etc/hosts文件

设置/etc/hosts文件,使用反向代理可以通过域名找到内部真实的服务器,其中www和ao在同一台服务器上放置
192.168.3.10  www.zcu.com
192.168.3.10  ao.zcu.com
192.168.3.11  stu.zcu.com

7、设置运行权限

要想让squid顺利运行必须权限设置正确,我们在配置文件中已经指定了运行squid的用户名和用户属组。把squid所有的目录属主和属组都改为squid
chown -R squid:squid /home/squid
chown -R squid:squid /home/squidlogs
chown -R squid:squid /home/squidcache

8、常用命令

(1) 初始化你在 squid.conf 里配置的 cache 目录,如果有错误提示,请检查你的 cache 目录的权限,第一次运行会在指定的缓存目录下面生成很多目录文件夹。
#squid/sbin/squid -z
(2) 对你的 squid.conf 排错,即验证 squid.conf 的 语法和配置。
#squid/sbin/squid -k parse
(3) 在前台启动 squid ,并输出启动过程。
#squid/sbin/squid -N -d1
如果有到 ready to server reques ,恭喜,启动成功。
然后 ctrl + c ,停止 squid 。
(4) 启动 squid 在后台运行。
#squid/sbin/squid -s
这时候可以 ps -A 来查看系统进程,可以看到俩个 squid 进程。
(5) 停止 squid
#squid/sbin/squid -k shutdown
这个不用解释吧。
(6) 重引导修改过的 squid.conf
#squid/sbin/squid -k reconfigure
(7) 把 squid 添加到系统启动项
编辑 /etc/rc.d/rc.local
添加如下行: /home/squid/sbin/squid -s

9、日志轮转

每3天转存一下日志:

0 0 */3 * * /home/squid/sbin/squid -k rotate

日志最多可保留10个文件。

标签: squid 反向代理 squid3.0 Web缓存

发表评论:

  • 5
  • 3
  • 7
  • 9
  • 0

Powered by emlog