您的位置:首页 > 运维架构 > 反向代理

Web缓存(配置squid)

2015-04-08 15:56 666 查看
配置squid

转载
http://www.92csz.com/study/linux/21.htm
Squid是比较知名的代理软件,它不仅可以跑在linux上还可以跑在windows以及Unix上,它的技术已经非常成熟。

目前使用Squid的用户也是十分广泛的。Squid与Linux下其它的代理软件如Apache、Socks、TIS FWTK和delegate相比,

下载安装简单,配置简单灵活,支持缓存和多种协议。

Squid的缓存功能相当好用,不仅可以减少带宽的占用,同样也大大降低了后台的WEB服务器的磁盘I/O的压力。

Squid接收用户的下载申请,并自动处理所下载的数据。

也就是说,当一个用户象要下载一个主页时,它向Squid发出一个申请,要Squid替它下载,

然后Squid 连接所申请网站并请求该主页,接着把该主页传给用户同时保留一个备份,当别的用户申请同样的页面时,Squid把保存的备份立即传给用户,使用户觉得速度相当快。

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

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.STABLE20

ulimit -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-dns

make && make install

关于squid的版本,有必要提一下,目前squid最新版本已经到了3.1了,但是笔者认为2.6版本比较好用,如果你有兴趣可以研究一下3.1。

【squid配置】

编辑配置文件 /usr/local/squid/etc/squid.conf

把原来配置文件删除,替换成:

http_port 80 transparent

cache_replacement_policy lru  #如果有多个(下面两行)缓存目录,则需要写这个参数

cache_dir aufs  /cache1 8192 16 256  #缓存目录1 /cache1 大小为8G

cache_dir aufs /cache2 4096 16 256  #缓存目录2 /cache2 大小为4G

## 上面两行定义了缓存目录,这个缓存目录可以只有一个,也可以定义很多个。

cache_mem 2048 MB  #分配多少内存给squid,建议留至少512M给系统,如果你是虚拟机内存很小,只作为试验用的话,那就分一半内存给squid

maximum_object_size 2048 KB  #缓存的文件最大不能超过2M

maximum_object_size_in_memory 512 KB #缓存在内存中的文件最大不超过512k

visible_hostname cache.example.com  #显示给用户的主机名

client_persistent_connections off  #client端关闭长连接

server_persistent_connections on  #server端打开长连接

memory_pools on

memory_pools_limit 1024 MB

forwarded_for on

log_icp_queries off

cache_mgr  cache@example.com  #定义管理员的mail为cache@example.com

via on

httpd_suppress_version_string off

cache_effective_user squid   #定义以squid用户的身份运行squid

cache_effective_group squid

error_directory /usr/local/squid/share/errors/Simplify_Chinese

icon_directory /usr/local/squid/share/icons

mime_table /usr/local/squid/etc/mime.conf

ie_refresh off

tcp_recv_bufsize 32 KB

acl all src 0.0.0.0/0.0.0.0

acl localhost src 127.0.0.0/8

acl Mgr_ip src 127.0.0.0/8

acl allow_ip dst 127.0.0.0/8  192.168.0.0/16  #定义允许代理的web的IP或者IP段

acl PURGE method PURGE

acl Safe_ports port 80 8080

acl CONNECT method CONNECT

acl manager proto cache_object

acl HTTP proto HTTP

http_access allow allow_ip

http_access allow manager Mgr_ip

http_access deny manager

http_access deny PURGE

http_access deny !Safe_ports

http_access deny all

icp_access deny all

ipcache_size 1024

ipcache_low 90

ipcache_high 95

memory_replacement_policy lru

hosts_file /etc/hosts

request_header_max_size 128 KB

hierarchy_stoplist cgi-bin ? \.php \.html

acl QUERY urlpath_regex cgi-bin \? \.php \.html

cache deny QUERY

quick_abort_min -1 KB

quick_abort_max 32 KB

quick_abort_pct 95

# error page

#error_map http://www.92csz.com/404.html 403

#deny_info http://www.92csz.com/error.html cctv_Domain

# timeout

peer_connect_timeout 20 seconds

connect_timeout 20 seconds

read_timeout 60 seconds

request_timeout 20 seconds

pconn_timeout 20 seconds

shutdown_lifetime 5 seconds

strip_query_terms off

icp_port 0

# logfile

emulate_httpd_log on

logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>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

修改完配置文件后保存,然后初始化squid

mkdir /cache1  /cache2 /var/log/squid

chown -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

如果没有错,则不会显示任何信息,否则会显示一些信息出来。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  web缓存 squid