系统环境:centos 5.5 64位


一、安装篇:

添加squid运行账户
groupadd squid
useradd -g squid -s /bin/false -M squid

添加日志目录、设置缓存和日志目录的权限
mkdir /data/{squidlog,cache}
chown -R squid.squid /data
2.7下载地址:
wget http://www.squid-cache.org/Versions/v2/2.7/squid-2.7.STABLE9.tar.gz
3.X下载地址:
wget http://www.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE26.tar.gz
wget http://www.squid-cache.org/Versions/v3/3.1/squid-3.1.12.3.tar.gz

这里我选择3.X版本进行安装:
tar zxvf http://www.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE26.tar.gz
cd squid-3.0.STABLE26
./configure --prefix=/usr/local/squid3 --enable-async-io=100 --with-pthreads --enable-storeio="aufs,diskd,ufs" --enable-removal-policies="heap,lru" --enable-icmp --enable-delay-pools --enable-useragent-log --enable-referer-log --enable-kill-parent-hack --enable-arp-acl --enable-default-err-language=Simplify_Chinese --enable-err-languages="Simplify_Chinese English" --disable-poll --disable-wccp --disable-wccpv2 --disable-ident-lookups --disable-internal-dns --enable-basic-auth-helpers="NCSA" --enable-stacktrace --with-large-files --disable-mempools --with-filedescriptors=65535 --enable-ssl --enable-x-accelerator-var
make
make install
make install-pinger

创建配置文件:
vi /usr/local/squid3/etc/squid.conf
加入内容见本文最后
+++++++++++++++++++++++
+++++++++++++++++++++++

初始化cache目录
/usr/local/squid3/sbin/squid -z

然后运行squid,注:-D参数用来跳过DNS检测
/usr/local/squid3/sbin/squid -D

最后设置squid开机自动启动
echo "/usr/local/squid3/sbin/squid -D " >>/etc/rc.local

二、维护篇:

以下是squid常用的命令:
如果修改过配置文件,则用下面的命令重读配置文件(即时生效)
/usr/local/squid3/sbin/squid -k reconfigure

检查配置文件配置的语法是否正确
/usr/local/squid3/sbin/squid -k parse

查看squid的日志和进程,看squid运行是否正常
ps -ef |awk '/^squid/'
cat /data/squidlog/cache.log
/usr/local/squid3/sbin/squid -k check ;echo $?
lsof -n |grep squid
lsof -i:80

关闭squid
发出关闭信号,等会话结束后彻底关闭
/usr/local/squid3/sbin/squid -k shutdown
关闭squid(更高优先级,直接关闭squid)
/usr/local/squid3/sbin/squid -k interrupt
关闭squid(最高优先级,直接杀死squid进程)
/usr/local/squid3/sbin/squid -k kill

滚动日志文件
/usr/local/squid3/sbin/squid -k rotate
设置每周二凌晨四点自动滚动日志
echo "00 4 * * 2 /usr/local/squid3/sbin/squid -k rotate " >>/etc/crontab

目前应用中的squid.conf文件,由于日志文件增长很快,所以直接输出到/dev/null丢弃掉了
#basic
cache_vary on
cache_effective_user squid
cache_effective_group squid
pid_filename /usr/local/squid3/var/logs/squid.pid
visible_hostname squid.12306.com
cache_mgr 250621008@qq.com
error_directory /usr/local/squid3/share/errors/Simplify_Chinese
icon_directory /usr/local/squid3/share/icons
mime_table /usr/local/squid3/etc/mime.conf
hosts_file /etc/hosts

acl DEIpadd dstdom_regex [0-9]$
http_access deny DEIpadd

cache_replacement_policy lru
memory_replacement_policy lru

http_port 80 vhost vport

cache_mem  4024 MB
maximum_object_size_in_memory 5120 KB

icp_port 0

#.cache_dir
cache_dir aufs /data/cache 50000 64 128

max_open_disk_fds 0
maximum_object_size 20 MB

#.cache_peer
cache_peer 192.168.8.20 parent 80 0 no-query originserver no-digest name=web1
cache_peer 192.168.8.21 parent 80 0 no-query originserver no-digest name=web2
cache_peer_domain web1 .cjzg.com
cache_peer_domain web2 .cjzg.com


#acl

acl src 0.0.0.0/0.0.0.0
acl Safe_ports port 80
acl SSL_ports port 443
acl LanSrc src 192.168.1.0/24
acl webdomain dstdomain .cjzg.com
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl CONNECT method CONNECT
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow LanSrc
http_access allow webdomain
http_access allow all

#refresh_pattern
refresh_pattern -i /$ 15 90% 600 reload-into-ims
refresh_pattern -i .html$ 15 90% 600 reload-into-ims
refresh_pattern -i .htm$ 15 90% 600 reload-into-ims
refresh_pattern -i .shtml$ 15 90% 600 reload-into-ims
refresh_pattern -i .hml$ 15 90% 600 reload-into-ims
refresh_pattern -i .gif$ 1440 90% 129600 reload-into-ims
refresh_pattern -i .swf$ 1440 90% 129600 reload-into-ims
refresh_pattern -i .jpg$ 1440 90% 129600 reload-into-ims
refresh_pattern -i .png$ 1440 90% 129600 reload-into-ims
refresh_pattern -i .bmp$ 1440 90% 129600 reload-into-ims
refresh_pattern -i .js$ 120 90% 129600 reload-into-ims
refresh_pattern -i .css$ 120 90% 129600 reload-into-ims
refresh_pattern -i .wma$ 1440 90% 21600 reload-into-ims
refresh_pattern -i .zip$ 1440 90% 21600 reload-into-ims
refresh_pattern -i .mp3$ 1440 90% 21600 reload-into-ims
refresh_pattern -i .rar$ 1440 90% 21600 reload-into-ims
refresh_pattern -i .rm$ 1440 90% 21600 reload-into-ims
refresh_pattern -i .flv$ 1440 90% 21600 reload-into-ims
refresh_pattern -i .rar$ 1440 90% 21600 reload-into-ims
refresh_pattern -i .rm$ 1440 90% 21600 reload-into-ims
refresh_pattern -i .avi$ 1440 90% 21600 reload-into-ims
refresh_pattern -i .3gp$ 1440 90% 21600 reload-into-ims
refresh_pattern -i .mp4$ 1440 90% 21600 reload-into-ims
refresh_pattern -i .wmv$ 1440 90% 21600 reload-into-ims

#keepalived
client_persistent_connections off
server_persistent_connections on

#log
emulate_httpd_log on
logformat combind %{X-Forwarded-For}>h %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
cache_log /data/squidlog/cache.log
cache_access_log /dev/null combind
cache_store_log /dev/null
strip_query_terms off
logfile_rotate 10

#other
forwarded_for on
log_icp_queries off
via off
httpd_suppress_version_string off
ie_refresh off
tcp_recv_bufsize 32 KB
ipcache_size 1024
ipcache_low 90
ipcache_high 95
cache_swap_low 80
cache_swap_high 90
request_header_max_size 128 KB

quick_abort_min 20 KB
quick_abort_max 20 KB
quick_abort_pct 95

connect_timeout 1 minute
negative_ttl 0 minutes

read_timeout 30 seconds
pconn_timeout 120 seconds
half_closed_clients off
client_lifetime 10 minutes
shutdown_lifetime 5 seconds

hierarchy_stoplist cgi-bin ?
access_log /dev/null squid


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

squid 日常操作

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

squidclient -p 80 mgr:info   #squid运行状态信息
squidclient -p 80 mgr:mem    #squid内存使用情况
squidclient -p 80 mgr:objects   #quid已经缓存的列表
squidclient -p 80 mgr:diskd  #squid的磁盘使用情况

PURGE指令,它会强制更新某一url缓存信息:
squidclient -p 80 -m PURGE http://x.x.x.x/css/dnt.css


squid 日志切割
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/bin/bash
YEAR=`/bin/date +%Y`
DATA=`date +%m%d`
PATH=/data/squidlog
/usr/local/squid/sbin/squid -k rotate
cd $PATH
/bin/mkdir -p $YEAR
for ((i=0;i<=9;i++));
do
/bin/mv  access.log.$i ./$YEAR/access.log.$DATA.$i
/bin/mv  cache.log.$i ./$YEAR/cache.log.$DATA.$i
/bin/mv  store.log.$i ./$YEAR/store.log.$DATA.$i
done
cd $PATH/$YEAR
/usr/bin/find  ./ -type f -mtime 30 -exec gzip {} \;

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++