安装squid

ZephyrXT 发布于 阅读:28

wget http://www.squid-cache.org/Versions/v3/3.1/squid-3.1.14.tar.gz
tar zxvf squid-3.1.14.tar.gz

yum install -y make gcc-gcc+ autoconf sudo wget libtool-ltdl-devel gd-devel freetype-devel libxml2-devel libjpeg-devel libpng-devel openssl-devel curl-devel patch libmcrypt-devel libmhash-devel ncurses-devel

cd /root/squid-3.1.14

./configure --prefix=/usr/local/squid --enable-follow-x-forwarded-for --enable-storeio=aufs,ufs --with-maxfd=65536 --with-pthreads --enable-dlmalloc --enable-poll --enable-stacktraces --enable-removal-policies=heap,lru --enable-delay-pools

make&make install

groupadd squid
useradd -s /sbin/nologin -M -g squid squid

/usr/local/squid/sbin/squid -z

chown -R squid:squid /usr/local/squid/var/cache
chown -R squid:squid /usr/local/squid/var/logs

配置 squid 配置文件 编辑 squid.conf 文件,vi /usr/local/squid/etc/squid.conf (如果是yum install squid
/etc/squid/squid.conf)

# squid conf etc/squid.conf
# Last Updated 2010.11.20

###
cache_effective_user squid
cache_effective_group squid
tcp_recv_bufsize 65535 bytes
icp_port 0
visible_hostname cdncache(为squid缓存服务器名称,每台服务器建议取不同的名字,便于区别)
http_port 80 vhost vport

#domains(以下为加速的站点)
cache_peer zephyrxt.com parent 80 0 no-query originserver name=site1

#peer_domain
cache_peer_domain site1 zephyrxt.com

#acl
acl local src 127.0.0.1
acl Manager proto cache_object
acl purge method PURGE
http_access allow Manager local purge
http_access deny Manager purge
acl alls src all
http_access allow alls

###cache,dir
cache_log /usr/local/squid/var/logs/cache.log
access_log /usr/local/squid/var/logs/access.log squid
cache_dir ufs /usr/local/squid/var/cache/ 10000 16 256
maximum_object_size 1024 KB
maximum_object_size_in_memory 1024 KB
cache_mem 512 MB
cache_swap_low  80
cache_swap_high 90
ipcache_size 512
ipcache_low 90
ipcache_high 95
fqdncache_size 512

### timeout
connect_timeout 1 minute
peer_connect_timeout 30 seconds
request_timeout 2 minutes
persistent_request_timeout 30 seconds

hierarchy_stoplist cgi-bin ?

#refresh_pattern
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
refresh_pattern -i \.jpg$ 43200 50% 86400
refresh_pattern -i \.png$ 43200 50% 86400
refresh_pattern -i \.css$ 43200 50% 86400
refresh_pattern -i \.js$ 43200 50% 86400
refresh_pattern -i \.xml$ 43200 50% 86400
refresh_pattern -i \.htm$ 43200 50% 86400
refresh_pattern -i \.html$ 43200 50% 86400
refresh_pattern .               0       20%     4320

#header off
via off
reply_header_access Via deny all
reply_header_access Server deny all

四、创建缓存目录
#/usr/local/squid/sbin/squid –z
启动squid
#/usr/local/squid/sbin/squid
停止squid:
#/usr/local/squid/sbin/squid -k shutdown
使用新配置启动:

#/usr/local/squid/sbin/squid -k reconfigure
通过crontab每天0点截断/轮循日志:
0 0 * * * (/usr/local/squid/sbin/squid -k rotate)
可以用以下命令寻找已经缓存的文件
#cd /usr/local/squid/var/cache


2. 激活squid
1). 在开启squid之前,你应该验证其配置文件是否正确。运行如下命令即可:
# squid -k parse
假如你看不到输出,配置文件有效,你能继续后面的步骤。然而,如果配置文件包含错误,squid会告诉你:
2). 初始化cache目录.即建立缓存目录的存储格式
只需在第一次启动squid服务之前执行(在初次运行squid之前,或者无论何时你增加了新的cache_dir,你必须初始化cache目录。)
# squid -z
cache目录初始化可能花费一些时间,依赖于cache目录的大小和数量,以及磁盘驱动器的速度。假如你想观察这个过程,请使用-X选项:
# squid -zX
3). 启动squid服务
# service squid start
假定squid安装在/usr/local/squid目录下,也可以
# /usr/local/squid/sbin/squid -sD
4). 停止squid
最安全的停止squid的方法是使用squid -k shutdown命令:
# squid -k shutdown
5). 重配置运行中的squid进程
在你了解了更多关于squid的知识后,你会发现对squid.conf文件做了许多改动。为了让新设置生效,你可以关闭和重启squid,或者在squid运行时,重配置它。
重配置运行中的squid最好的方法是使用squid -k reconfigure命令:
# squid -k reconfigure
6). 滚动日志文件
除非你在squid.conf里禁止,squid会写大量的日志文件。你必须周期性的滚动日志文件,以阻止它们变得太大。squid将大量的重要信息写入日志,假如写不进去了,squid会发生错误并退出。为了合理控制磁盘空间消耗,在cron里使用如下命令:
%squid -k rotate
例如,如下任务接口在每天的早上4点滚动日志:
0 4 * * * /usr/local/squid/sbin/squid -k rotate
该命令做两件事。首先,它关闭当前打开的日志文件。然后,通过在文件名后加数字扩展名,它重命名cache.log,store.log,和 access.log。例如,cache.log变成cache.log.0,cache.log.0变成cache.log.1,如此继续,滚动到 logfile_rotate选项指定的值。
squid仅仅保存每个日志文件的最后logfile_rotate版本。更老的版本在重命名过程中被删除。假如你想保存更多的拷贝,你需要增加logfile_rotate限制,或者编写脚本用于将日志文件移动到其他位置。 请见13.7章关于滚动日志的其他信息。

 

把squid添加到系统启动项
编辑 /etc/rc.d/rc.local
添加如下行: /usr/local/squid/sbin/squid -s
当然,并不是每个人都喜欢这种启动方式,你可以用你最习惯的方式;或者把它安装为服务。

高负载下增加文件描述符

查看squid具体用了多少文件描述符的方法
#squidclient -p 80 -U manager -W squidstats mgr:

ulimit -HSn 65535

配置文件中加入“max_filedesc 8192”,重启squid

 

参考:

squid工作原理及3.1.4版本源代码安装配置实例指南
https://bbs.zephyrxt.com/thread-6076-1-1.html
Squid 3的多域名加速基本配置
https://bbs.zephyrxt.com/thread-5405-1-1.html
Squid3反向代理安装与配置
https://bbs.zephyrxt.com/thread-5643-1-1.html
利用 squid 反向代理提高网站性能
https://bbs.zephyrxt.com/thread-6077-1-1.html
CentOS 5.6 下Squid 安装
https://bbs.zephyrxt.com/thread-6078-1-1.html
Squid反向代理的缓存(加速)服务器配置笔记
https://bbs.zephyrxt.com/thread-6079-1-1.html
squid
https://bbs.zephyrxt.com/thread-6080-1-1.html
Squid安装配置及调试
https://bbs.zephyrxt.com/thread-6081-1-1.html

ulimit命令优化linux进程连接数等限制 不指定
https://bbs.zephyrxt.com/thread-5030-1-1.html