More Related Content Similar to Squid安装配置 (20) Squid安装配置1. Squid 安装配置
一.下载并解压缩最新的 squid-2.6
http://www.squid-cache.org/Versions/v2/2.6/
二.编译安装
#cd 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
--disable-arp-acl
--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
2. 三.配置文件 squid.conf
http_port 80 transparent
cache_replacement_policy lru
#磁盘 cache 的置换策略 lru 最近最少使用
cache_dir aufs /data/squid/cache1 8192 16 256
cache_dir aufs /data/squid/cache2 4096 16 256
#定义缓冲区 类型 路径 目录大小 第一层缓冲目录数 每一个目录所包含的目录数
cache_mem 2048 MB
#代理服务器高速缓存;默认 8M;如果有 N 兆内存推荐 N/3
maximum_object_size 2048 KB
#大于该值的对象将不被存储。如果想要提高访问速度,就请降低该值;如果想最大限度地节约带宽,降低
成本,请增加该值。
minimum_object_size 0 KB
maximum_object_size_in_memory 512 KB
#在内存中单个文件最大缓存大小,超过这个大小将不缓存到内存中
visible_hostname cache.comsenz.com
#发生错误时,生成提示所显示的缓存服务器名
client_persistent_connections off
#客户端不持续连接
server_persistent_connections on
#服务器端持续连接 同时后台 apache 服务器也应打开保持连接
#persistent_connection_after_error on
3. memory_pools on
memory_pools_limit 1024 MB
#squid 预先分配一定的内存用于 cache,以提高效率,默认 on
forwarded_for on
#获取用户 IP
log_icp_queries off
#禁止 ICP(Internet 高速缓存协议)查询的日志
#reload_into_ims off
#该选项让squid 在确认请求里,以no-cache 指令传送一个请求。换句话说,squid 在转发请
求之前,对该请求增加一个If-Modified-Since 头部。注意这点仅仅在目标有Last-Modified
时间戳时才能工作。外面进来的请求保留 no-cache 指令,以便它到达原始服务器。
#half_closed_clients off
#有时候由于用户的不正常操作,可能会使与 squid 的 TCP 连接处于半关闭状态,这时候,该 TCP 连接的
发送端已经关闭,而接收端正常工作。
#默认该值设为 on. squid 将一直保持处于半关闭状态的 TCP 连接,直到返回套接字的读写错误才将其关闭。
#如果将该值设为 off,则一旦从客户端 返回“no more data to read”的信息,squid 就立即关闭该连接
cache_mgr cache@discuz.com
#联系 email
via on
httpd_suppress_version_string off
#不显示版本信息
cache_effective_user squid
#缓存用户 UID
4. cache_effective_group squid
#缓存用户组 GID
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
# TCP 缓存区大小
acl all src 0.0.0.0/0.0.0.0
http_access deny all
#其它所有地址禁止
acl localhost src 127.0.0.0/8
#定义 acl 的 localhost 的 ip 地址
acl cctv_ip dst 127.0.0.0/8 192.168.0.0/16
#定义 acl cctv_ip 的目标 ip 地址
acl cctv_Domain dstdomain .cctv.com
#dstdomain 指代理服务器代理的主机. 定义 acl 的 cctv_domain 为所有域名的主机
acl CONNECT method CONNECT
acl HTTP proto HTTP
acl Safe_ports port 80 8080
http_access deny !Safe_ports
#拒绝连接非安全端口
acl PURGE method PURGE
acl Purge_ip src 127.0.0.0/8 192.168.0.0/16
http_access allow PURGE Purge_ip
http_access deny PURGE
5. #定义绝删除缓存规则
acl manager proto cache_object
acl Mgr_ip src 127.0.0.0/8
http_access allow manager Mgr_ip
http_access deny manager
#定义缓存管理规则
http_access allow cctv_Domain
#允许 cctv_Domain 类使用代理服务器
http_access allow cctv_ip
#允许 cctv_ip 使用代理服务器
icp_access deny all
#不允许使用代理服务器(中间件被其它的 squid 调用)
#cache_peer 127.0.0.1 parent 80 0 no-query no-digest no-netdb-exchange originserver
name=BLOG
#acl BLOG_DOMAIN dstdom_regex .*.blog.phpchina.com
#cache_peer_access BLOG allow BLOG_DOMAIN
ipcache_size 1024
ipcache_low 90
ipcache_high 95
#设置 IP 解析缓存大小
memory_replacement_policy lru
#设置缓存替换策略为 lru 最近最少使用
#hosts_file /etc/hosts
request_header_max_size 128 KB
#设置了 Http 请求的包头大小
6. hierarchy_stoplist cgi-bin ? .php .html
acl QUERY urlpath_regex cgi-bin ? .php .html
cache deny QUERY
#以上 3 句不缓存 url 中有 cgi-bin 或.php 或.html 的页面;直接访问主机
request_body_max_size 0 KB
#设置了 Http 请求的数据大小 0 为没限制
#refresh_pattern ^ftp: 1440 20% 10080
#refresh_pattern ^gopher: 1440 0% 1440
#refresh_pattern . 0 50% 1440
#refresh_pattern -i .jpg$ 1440 50% 2880 ignore-reload
#refresh_pattern -i .png$ 1440 50% 2880 ignore-reload
#refresh_pattern -i .bmp$ 1440 50% 2880 ignore-reload
#refresh_pattern -i .gif$ 1440 50% 2880 ignore-reload
#refresh_pattern -i .jpeg$ 1440 50% 2880 ignore-reload
#refresh_pattern -i .swf$ 1440 50% 2880 ignore-reload
#refresh_pattern -i .js$ 1440 50% 2880 ignore-reload
#refresh_pattern -i .css$ 1440 50% 2880 ignore-reload
refresh_pattern -i .zip$ 1440 50% 2880 ignore-reload
#定义指定内容对象的刷新规则。默认正则表达式是大小写敏感的,使用 -i 选项可以使其大小写不敏感。
min 单位是分钟,未显式定义过期时间的内容对象使用 min 作为对象寿命的下限。
percent 百分比,未显式定义过期时间的内容对象使用 Last-Modified 时间的百分比作为对象的寿命。
max 单位是分钟,未显式定义过期时间的内容对象使用 min 作为对象寿命的上限。
quick_abort_min -1 KB
quick_abort_max 32 KB
quick_abort_pct 95
#控制 squid 是否继续传输被用户中断的请求。当用户中断请求时,squid 将检测 quick_abort 的值。
如果剩余部分小于“quick_abort_min”指定的值,squid 将继续完成剩余部分的传输;如果剩余部分大于
“quick_abort_max”指定的值,squid 将终止剩余部分的传输;如果已完成“quick_abort_pct”指定的百分比,
squid 将继续完成剩余部分的传输。缺省的设置为: 16 KB 16 KB 95
7. # error page
#error_map http://www.discuz.net/error/404.html 403
#deny_info http://www.discuz.net/aaa.html cctv_Domain
# timeout
peer_connect_timeout 20 seconds
#连接到上层代理的超时时间
connect_timeout 20 seconds
#连接到其他机器的最大尝试时间
#connect_timeout 1 minute
#negative_ttl 0 minutes
read_timeout 60 seconds
#设置 squid 从被请求服务器读入数据的超时时间
#read_timeout 15 minutes
request_timeout 20 seconds
#返回超时
#request_timeout 30 minutes
pconn_timeout 20 seconds
#设置 squid 在与其他服务器和代理建立连接后,该连接闲置多长时间后被关闭
#pconn_timeout 1 minutes
#collapsed_forwarding on
#persistent_request_timeout 20 minute
#persistent_request_timeout 1 minutes
##client_lifetime 1 day
#client_lifetime 2 minutes
#shutdown_lifetime 30 seconds
shutdown_lifetime 5 seconds
#当收到 SIGTERM 或者 SIGHUP 信号后, squid 将进入一种 shutdown pending 的模式,等待所有活动的套
接字关闭。在过了 shutdown_lifetime 所定义的时间后,所有活动的用户都将收到一个超时信息
strip_query_terms off
8. #使日志记录完整的查询信息
# snmp
#snmp_port 3401
#acl snmppublic snmp_community show
#设置 snmp 监控的共同体有密码为 show; 默认为 public
#snmp_access allow snmppublic localhost
#允许本机访问 snmppublic;默认为 all
#snmp_access deny all
icp_port 0
#icp_port 参数指定 Squid 从邻居服务器缓冲内发送和接收 ICP 请求的端口号。
#这里设置为 0 是因为这里配置 Squid 为内部 Web 服务器的加速器,所以不需要使用邻居服务器的缓冲。
# logfile
emulate_httpd_log on
#打开“emulate_httpd_log”选项,将使 Squid 仿照 Web 服务器的格式创建访问记录。
#如果希望使用 Web 访问记录分析程序,就需要设置这个参数. 默认为 off
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st
"%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
#access_log none
#access_log /log/squid-log/access.log combined
#cache_store_log /log/squid-log/store.log
cache_store_log /dev/null
cache_log /data/log/squid-log/cache.log
logfile_rotate 12
#写日志
# MISCELLANEOUS
store_objects_per_bucket 15
client_db off
9. 四.启动 squid 的脚本
#!/bin/bash
pro_count=`ps waux | grep squid.conf | grep ^squid | wc -l`
dns_count=`ps waux | grep dnsserver | grep ^squid | wc -l`
runcache=`ps waux | grep squid | grep RunCache | wc -l`
# squid process exits.
if [ $pro_count -gt 0 ]; then
echo "squid already starting....."
exit 1
fi
# dnsserver process exist.
if [ $dns_count -gt 0 ];then
echo "squid already starting....."
exit 1
fi
# RunCache process exist.
if [ $runcache -gt 0 ];then
echo "squid already starting....."
exit 1
fi
/usr/local/squid/bin/RunCache &
sleep 1
echo -e "squid starting.....n"
五.停止 Squid 的脚本
#!/bin/bash
pid=`ps waux | grep RunCache | grep "/bin/sh" | awk '{print $2}'`
kill -9 $pid >/dev/null 2>&1
10. for i in `ps waux | grep ^squid | awk '{print $2}'`
do
kill -9 $i >/dev/null 2>&1
done
echo "squid stoped."
RunCache 脚本的修正:
ulimit -HSn 65536
squid -NDsY $conf >> $logdir/squid.out 2>&1
这样能防止 DNS 测试失败无法启动 squid
命中率查看:
/usr/local/squid/bin/squidclient -h 218.241.156.84 -p 80 mgr:info
squidclient -p 80 mgr:5min