squid3.0快速缓存实现
一、编译安装#tar zxvf squid-3.0.STABLE11.tar.gz //稳定版
#cd squid-3.0.STABLE11
#./configure --prefix=/usr/local/squid \
--enable-arp-acl \
--enable-linux-netfilter \
--enable-pthreads \
--enable-err-language="Simplify_Chinese" \
--enable-default-err-language="Simplify_Chinese" \
--enable-underscores \
--disable-internal-dns \
--enable-pf-transparent \
--enable-referer-log
#make
#make instlal
注:--enable-internal-dns 做多个网站反向代理时使用,允许使用内部dns,也可改/etc/hosts
====================================================
二、初始化
#adduser squid
#addgroup squid
#mkdir /usr/local/squid/var/cache
#mkdir /usr/local/squid/var/logs
#chown -R squid:squid /usr/local/squid/var/cache //默认nobody运行
#chown -R squid:squid /usr/local/squid/var/logs
#squid -z //初始化cache目录 #squid -zX 可查看初始化过程
在/etc/hosts中:加入内部的DNS解析,比如:
192.168.1.65 www.testsquid.com
三、配置/usr/local/squid/etc/squid.conf
#vim /usr/local/squid/etc/squid.conf
(4700多行配置….. 大多是注释,不管它,光标移到最后加入以下配置)
#==============================cut here===================================
# 主机名(3.0加入配置),无此项无法启动
visible_hostname www.testsquid.com
# 管理员邮箱,会显示在错误信息页面上,方便发生错误时联系!
cache_mgr root@localhost.localdomain
# 实现透明代理(squid2.7以上),其中vhost是必须的
http_port 80 vhost vport
# 缓存设置
cache_mem 256 MB
maximum_object_size_in_memory 2 MB
# 替换机制(lru叫做“最近不常用的单元”unit一般就是常说object, 也就是当cache
# 中的内容比内存或硬盘达到上限时就需要进行数据的换进和换出工作)
memory_replacement_policy lru
# 缓存目录512M,其中一级目录16个,二级256个(每个一级下16个二级)
cache_dir ufs /usr/local/squid/var/cache 512 16 256
max_open_disk_fds 0
#缓存内容大小控制,当cache目录被占用到97%时,内容将被清空20%
cache_swap_low 80
cache_swap_high 97
# 错误信息目录
error_directory /usr/local/squid/share/errors/Simplify_Chinese
# 最大和最小缓存对象
minimum_object_size 0 KB
maximum_object_size 30 MB
# 日志格式
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs % 设置access_log中日志存储的格式
access_log /usr/local/squid/var/logs/page_zs_access_log combined
access_log为客户端请求的日志
pid_filename /usr/local/squid/var/logs/squid.pid
设置PID
cache_log /usr/local/squid3/var/logs/cache.log
为squid自身的运行日志
# 不记录store.log
cache_store_log none
# 指定代理服务器IP,2.5版本以上都是以cache_peer指定
# 需要将apache端口改成81(包括其虚拟机端口)
cache_peer 192.168.1.65 parent 81 0 no-query no-digest originserver name=www
squid2.5以上都是用的cache_peer来指定所需要代理的服务器的IP 这一点很重要!
cache_peer_domain www www.testsquid.com
cache_peer_access www allow all
# 允许客户端所有请求(这里可以设置拦截url,格式如下面两行缓存设置)
http_access allow all
# 设置不缓存url类型(空格隔开)
acl QUERY urlpath_regex .php .jsp .asp .pl .cgi
cache deny QUERY
#设置运行squid用户,一般不能以root运行
cache_effective_user squid
cache_effective_group squid
#==============================cut here================================
四、创建缓存目录
#/usr/local/squid/sbin/squid –z
启动squid
#/usr/local/squid/sbin/squid
停止squid:
#/usr/local/squid/sbin/squid -k shutdown
测试配置是否正确
# /usr/local/squid2/sbin/squid -k parse
出现ERROR:
WARNING: Cannot write log file: /usr/local/squid3/var/logs/cache.log
/usr/local/squid3/var/logs/cache.log: Permission denied
messages will be sent to 'stderr'.
2008/03/13 12:53:09| Creating Swap Directories
FATAL: Failed to make swap directory /usr/local/squid3/var/cache: (13) Permission denied
很明显权限不对,修改后测试通过!
使用新配置启动:
#/usr/local/squid/sbin/squid -k reconfigure
通过crontab每天0点截断/轮循日志:
0 0 * * * (/usr/local/squid/sbin/squid -k rotate)
可以用以下命令寻找已经缓存的文件
#cd /usr/local/squid/var/cache
#find -type f |xargs ls –l
补充说明
端口使用情况:
apache 81
squid 80
这样设置的原因是方便squid直接拦截客户端发来的http请求。如果不更改apache默认的80端口,squid设置其他可用端口时,例如3128,需要将80请求转发到3128,命令如下:
#iptables -t nat -A PREROUTING -s 192.168.1.65 -p tcp --dport 80 -j REDIRECT 3128
注:此命令必须每次开机后运行,可以写成开机执行任务脚本
所以,本人感觉修改apache端口比较简单,修改步骤:
#vim /usr/local/apache/conf/httpd.conf
修改:Listen 81
如有虚拟主机设置,则还需要修改虚拟主机配置:
#vim /usr/local/apache/conf/extra/httpd-vhosts.conf
修改:NameVirtualHost *:81
4.启动squid
/usr/local/squid3/sbin/squid -Nd1
ps aux |grep squid 查看是否启动
5.测试访问WEB站点
本机hosts 文件指向squid代理服务器
出现ERROR:
Access Denied.
访问拒绝
Access control configuration prevents your request from being allowed at this time. Please contact your service provider if
you feel this is incorrect.
当前的存取控制设定禁止您的请求被接受,如果您觉得这是错误的,请与您网路服务的提供者联系。
本缓存服务器管理员:webmaster
配置文件设置不对:
acl all src 0.0.0.0/0.0.0.0
http_access allow all
重新测试:OK 一切正常~~~
查看相应日志一切正常。
总体来说,squid 配置不是很麻烦,主要是配置文件里的每一项,对服务器的产生的影响非常的重要。细节决定成功~
页:
[1]