Squid cache(简称为Squid)是一个流行的自由软件,它符合GNU通用公共许可证。Squid作为网页服务器的前置cache服务器,可以代理用户向web服务器请求数据并进行缓存,也可以用在局域网中,使局域网用户通过代理上网 1、squid安装与配置,下载文件 http://www.squid-cache.org/Versions/v3/3.5/squid-3.5.1.tar.gz 2、添加squid用户 /usr/sbin/groupadd squid -g 23 /usr/sbin/useradd -u 23 -g squid -s /sbin/nologin squid 3、源码编译安装: tar zxf squid-3.5.1.tar.gz cd squid-3.5.1 ./configure --prefix=/usr/local/squid \ --enable-epoll \ --enable-htcp \ --enable-stacktraces \ --enable-storeio=ufs,aufs,diskd \ --enable-removal-policies=lru,heap \ --enable-icmp \ --enable-default-err-language=Simplify_Chinese \ --enable-err-languages="Simplify_Chinese English" \ --enable-cache-digests \ --enable-auth \ --enable-auth-basic="NCSA" \ --enable-useragent-log \ --enable-referer-log \ --enable-linux-netfilter \ --enable-delay-pools \ --enable-follow-x-forwarded-for \ --enable-kill-parent-hack \ --enable-gnuregex \ --enable-underscore \ --enable-arp-acl \ --enable-x-accelerator-vary \ --disable-ident-lookups \ --disable-ssl \ --disable-wccp \ --disable-internal-dns \ --disable-mempools \ --with-default-user=squid \ --with-pthreads \ --with-aio \ --with-large-files \ --with-filedescriptors=65535 make && make install 编译参数解释: ./configure --prefix=/usr/local/squid \ --enable-epoll 指定使用epoll()函数,提升性能 --enable-htcp HTCP 是超文本缓存协议--类似于ICP的内部缓存协议 --enable-stacktraces 系统支持在程序崩溃时,自动产生数据追踪,数据追踪信息被写到cache.log文件 --enable-storeio=ufs,aufs,diskd Squid支持大量的不同存储模块,可以在这里指定 --enable-removal-policies=lru,heap Squid缓存替换策略 --enable-icmp 加入icmp支持 --enable-default-err-language=Simplify_Chinese \ --enable-err-languages="Simplify_Chinese English" \ --enable-cache-digests 使能缓存摘要,加快检索缓存的速度 --enable-auth 启用模块身份认证 --enable-auth-basic="NCSA" 持NCSA身份验证 --enable-useragent-log 激活来自客户请求的HTTP用户代理头的日志 --enable-referer-log 激活来自客户请求的HTTP referer日志 --enable-linux-netfilter 可以支持透明代理 --enable-delay-pools 支持限制带宽配置 --enable-follow-x-forwarded-for 当一个请求被另一些代理服务器转发时通过从http头中寻找X-Forwarded-For来发现直接或间接的客户端的IP地址 --enable-kill-parent-hack 掉suqid的时候,要不要连同父进程一起关掉 --enable-gnuregex 支持GNU正则表达式。 --enable-underscore 允许解析的URL中出现下划线 --enable-arp-acl 可在规则中设置通过MAC地址进行管理,防止IP欺骗 --enable-x-accelerator-vary 该功能squid被配置成加速器时使用,在响应请求时,从后台原始服务中读取X-Accelerator-Vary头信息。 --disable-ident-lookups 防止系统使用RFC931规定的身份识别方法 --disable-ssl 禁用squid的SSL/TLS 安全连接 --disable-wccp 用于阻止或分发HTTP请求到一个或多个caches --disable-internal-dns 禁用内部DNS查询 --disable-mempools 关闭内存池,以防止squid运行时间长了会变慢 --with-default-user=squid 默认用户,应首先创建 --with-pthreads 支持POSIX线程 --with-aio 支持POSIX AIO --with-large-files 支持大文件,如日志文件等 --with-filedescriptors=65535 文件描述符个数,必须是64的整数倍 4、/usr/local/squid/etc/squid.conf 配置文件 # 官网推荐配置,允许访问的IP和端口的ACL acl localnet src 10.0.0.0/8 # RFC1918 possible internal network acl localnet src 172.16.0.0/12 # RFC1918 possible internal network acl localnet src 192.168.0.0/16 # RFC1918 possible internal network acl localnet src fc00::/7 # RFC 4193 local private network range acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines #定义了Safe_ports所代表的端口 acl SSL_ports port 443 acl Safe_ports port 80 # http acl Safe_ports port 21 # ftp acl Safe_ports port 443 # https acl Safe_ports port 70 # gopher acl Safe_ports port 210 # wais acl Safe_ports port 1025-65535 # unregistered ports acl Safe_ports port 280 # http-mgmt acl Safe_ports port 488 # gss-http acl Safe_ports port 591 # filemaker acl Safe_ports port 777 # multiling http #定义CONNECT代表http里的CONNECT请求方法 acl CONNECT method CONNECT acl PURGE method PURGE http_access deny !Safe_ports http_access deny CONNECT !SSL_ports http_access allow localhost manager http_access allow PURGE localhost http_access allow PURGE localnet http_access deny PURGE all http_access deny manager http_access allow localnet http_access allow localhost #错误页面缓存设置 acl badurl http_status 403-404 500- http_access deny badurl #自定义允许访问域名的ACL,协议,端口 acl denyHost dstdomain app.go-news.com acl accessHostA dstdomain .go-news.com acl accessHostB dstdomain .test.com acl accessProtocol proto HTTP acl accessPort port 80 #设定对ACL的访问策略 http_access allow accessProtocol accessPort accessHostA http_access allow accessProtocol accessPort accessHostB http_access deny accessProtocol accessPort denyHost #拒绝其他所有请求 http_access deny all ###########反向代理相关设置################################# #设置反向代理服务器监听的端口为80,accel表示开启squid的accel加速模式, #vhost和vport表示支持虚拟主机和虚拟端口,如果再加上transparent表示支持透明代理 http_port 80 accel vhost vport #cache_peer表示如果本机缓存中找不到客户端请求的数据,将与主机www.zcu.com以parent类型进行联系,no-query表示不使用ICP协议进行联系, #而是使用HTTP协议进行联系,no-digest表示代理服务器之间不做摘要表查询,直接用ICP协议沟通(同级代理)。端口是80,orginserver表示此服务器是源服务器,name表示别名。 cache_peer 192.168.4.156 parent 80 0 no-query no-digest originserver name=Server_go cache_peer 192.168.5.80 parent 80 0 no-query no-digest originserver name=Server_te #设置别名所对应的域名,如果cache_peer中使用域名而不是IP的话,那么cache_peer_domain中一定要用相同的域名,否则无法访问 cache_peer_domain Server_te image.go-news.com cache_peer_domain Server_go www.go-news.com #################缓存相关参数设置################ #设置缓存内存大小,最大使用内存为512M, #当使用超过95%时开始对缓存进行替换,直到下降到90%后停止 cache_mem 512 MB cache_swap_low 90 cache_swap_high 95 #设置内存中最大可缓存的最大文件大小 maximum_object_size_in_memory 128 KB #设置磁盘中可缓存的最大文件大小 maximum_object_size 65536 KB minimum_object_size 0 KB #设置缓存文件夹的路径和参数,10240表示10G大小,目录下面分为16级,每级又有256个目录 cache_dir ufs /data/cache 10240 16 256 #内存和硬盘cache的替换策略 memory_replacement_policy lru cache_replacement_policy lru #设置缓存日志文件路径 logformat main %>a %{%Y-%m-%d %H:%M:%S}tl %>Hs %<st %ru %{Referer}>h %Ss:%Sh access_log /usr/local/squid/var/logs/access.log main cache_log /dev/null cache_store_log none #关闭该项,这样就可以显示用户的整个请求内容 strip_query_terms off #禁止squid的出错页面总会在网页的最下方显示主机相关信息和squid的版本信息 httpd_suppress_version_string on #设置针对错误status代码缓存,如403、404等。如果不缓存则设置0 #negative_ttl 10 second #设置用户请求的HTTP头大小 request_header_max_size 128 KB #设置用户的真实IP地址通过X-Forwarded-For中传递下去 forwarded_for on #http1.0的vary信息里带有过期时间的话,squid还是不能缓存住。打开此选项提高缓存命中率 vary_ignore_expire on #修改默认缓存时间,默认是60s,解决当Expires设置为60秒以内时都不会Cache但当设置成61秒时就能Cache minimum_expiry_time 10 seconds #合并回源,多个回源流量变成一个回源,这个在大文件时,也比较有用。 collapsed_forwarding on #refresh_pattern 控制文件过期的,percent与Min、Max(都是分钟)两个值是完全没有关系 #1.缓冲对象在squid的cache缓冲的时间大于refresh_pattern定义的max,该响应过期 #2.缓冲对象在squid的cache缓冲的时间大于(原始数据进入squid的缓冲的时间-原始web数据所规定的Last-Modified时间)*percent,该响应过期 #开头是ftp的话,那么在一天(1440分钟)后,如果proxy 再次取用这个档案时,则cache内的数据会被更新 refresh_pattern ^ftp: 1440 20% 10080 refresh_pattern ^gopher: 1440 0% 1440 refresh_pattern (cgi-bin|\?) 0 0% 0 refresh_pattern . 0 20% 4320 #设置squid 用户和组 cache_effective_user squid cache_effective_group squid #设置代理服务器名称 visible_hostname go-news.com #设置缓存服务器管理员邮箱 cache_mgr [email protected] #squid的UDP端口,用来接收和发送ICP消息 icp_port 3130 #squid挂掉后,错误信息的存放目录 coredump_dir /usr/local/squid/var/cache 5、启动Squid #建立缓存目录: mkdir /data/cache && chown squid.squid /data/cache/ chown squid.squid /usr/local/squid/var -R #检查配置文件 /usr/local/squid/sbin/squid -k parse #初始化缓存目录 /usr/local/squid/sbin/squid -z 在前台启动 squid ,并输出启动过程。 #/usr/local/squid/sbin/squid -N -d1 6、Squid启动脚本/etc/init.d/squid,加入系统服务 #!/bin/bash # chkconfig: - 90 25 # pidfile: /usr/local/squid/var/run/squid.pid # config: /usr/local/squid/etc/squid.conf # ### BEGIN INIT INFO # Provides: squid # Short-Description: starting and stopping Squid Internet Object Cache # Description: Squid - Internet Object Cache. Internet object caching is \ # a way to store requested Internet objects (i.e., data available \ # via the HTTP, FTP, and gopher protocols) on a system closer to the \ # requesting site than to the source. Web browsers can then use the \ # local Squid cache as a proxy HTTP server, reducing access time as \ # well as bandwidth consumption. ### END INIT INFO # Source function library. . /etc/rc.d/init.d/functions PATH=/usr/bin:/sbin:/bin:/usr/sbin:/usr/local/squid/sbin export PATH # Source networking configuration. . /etc/sysconfig/network # Time to wait for Squid to shut down when asked. Should not be necessary # most of the time. SQUID_SHUTDOWN_TIMEOUT=100 # default squid conf file SQUID_CONF="/usr/local/squid/etc/squid.conf" # don't raise an error if the config file is incomplete # set defaults instead: SQUID_OPTS=${SQUID_OPTS:-""} SQUID_PIDFILE_TIMEOUT=${SQUID_PIDFILE_TIMEOUT:-20} SQUID_SHUTDOWN_TIMEOUT=${SQUID_SHUTDOWN_TIMEOUT:-100} SQUID_CONF=${SQUID_CONF:-"/usr/local/squid/etc/squid.conf"} # determine the name of the squid binary [ -f /usr/local/squid/sbin/squid ] && SQUID=squid prog="$SQUID" # determine which one is the cache_swap directory CACHE_SWAP=`sed -e 's/#.*//g' $SQUID_CONF | \ grep cache_dir | awk '{ print $3 }'` RETVAL=0 probe() { # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 1 [ `id -u` -ne 0 ] && exit 4 # check if the squid conf file is present [ -f $SQUID_CONF ] || exit 6 } start() { probe parse=`$SQUID -k parse -f $SQUID_CONF 2>&1` RETVAL=$? if [ $RETVAL -ne 0 ]; then echo -n $"Starting $prog: " echo_failure echo echo "$parse" return 1 fi for adir in $CACHE_SWAP; do if [ ! -d $adir/00 ]; then echo -n "init_cache_dir $adir... " $SQUID -z -F -f $SQUID_CONF >> /usr/local/squid/var/logs/squid.out 2>&1 fi done echo -n $"Starting $prog: " $SQUID $SQUID_OPTS -f $SQUID_CONF >> /usr/local/squid/var/logs/squid.out 2>&1 RETVAL=$? if [ $RETVAL -eq 0 ]; then timeout=0; while : ; do [ ! -f /usr/local/squid/var/run/squid.pid ] || break if [ $timeout -ge $SQUID_PIDFILE_TIMEOUT ]; then RETVAL=1 break fi sleep 1 && echo -n "." timeout=$((timeout+1)) done fi [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$SQUID [ $RETVAL -eq 0 ] && echo_success [ $RETVAL -ne 0 ] && echo_failure echo return $RETVAL } stop() { echo -n $"Stopping $prog: " $SQUID -k check -f $SQUID_CONF >> /usr/local/squid/var/logs/squid.out 2>&1 RETVAL=$? if [ $RETVAL -eq 0 ] ; then while : ; do if ps aux|grep squid.conf|grep -v grep >/dev/null 2>&1 ;then $SQUID -k shutdown else rm -f /usr/local/squid/var/run/squid.pid break fi done rm -f /var/lock/subsys/$SQUID timeout=0 while : ; do [ -f /usr/local/squid/var/run/squid.pid ] || break if [ $timeout -ge $SQUID_SHUTDOWN_TIMEOUT ]; then echo return 1 fi sleep 2 && echo -n "." timeout=$((timeout+2)) done echo_success echo else echo_failure if [ ! -e /var/lock/subsys/$SQUID ]; then RETVAL=0 fi echo fi return $RETVAL } reload() { $SQUID $SQUID_OPTS -k reconfigure -f $SQUID_CONF } restart() { stop start } condrestart() { [ -e /var/lock/subsys/squid ] && restart || : } rhstatus() { status $SQUID && $SQUID -k check -f $SQUID_CONF } case "$1" in start) start ;; stop) stop ;; reload|force-reload) reload ;; restart) restart ;; condrestart|try-restart) condrestart ;; status) rhstatus ;; probe) probe ;; *) echo $"Usage: $0 {start|stop|status|reload|force-reload|restart|condrestart|try-restart|probe}" exit 2 esac exit $? 加入系统服务: #chmod 755 /etc/init.d/squid && chkconfig --add squid && chkconfig --level 345 squid on 7、常用帮助命令 1)初始化你在 squid.conf 里配置的 cache 目录 #/usr/local/squid/sbin/squid -z //第一次启动squid服务时必须输入此命令 如果有错误提示,请检查你的 cache目录的权限。 2)对你的squid.conf 排错,即验证 squid.conf 的 语法和配置 #/usr/local/squid/sbin/squid -k parse 如果squid.conf 有语法或配置错误,这里会返回提示你。 3)在前启动squid,并输出启台动过程 #/usr/local/squid/sbin/squid -N -d1 如果有到 ready to server reques,恭喜,启动成功 然后 ctrl + c,停止squid,并以后台运行的方式启动它。 4)启动squid在后台运行 #/usr/local/squid/sbin/squid -D 这时候可以 ps -A 来查看系统进程,可以看到两个个 squid 进程。 5)停止 squid #/usr/local/squid/sbin/squid -k shutdown 6)重引导修改过的 squid.conf #/usr/local/squid/sbin/squid -k reconfigure 这个估计用的时候比较多,当你发现你的配置有不尽你意的时候,可以随时修改squid.conf,然后别忘记对你的 squid.conf排错,然后再执行此指令,即可让运行中squid重新按照你的squid.conf 来运行。 7)清除某个url的缓存 /usr/local/squid/bin/squidclient -p 80 -m PURGE -h 192.168.5.77 http://image.go-news.com/a.html 8)查看Squid运行状态 /usr/local/squid/bin/squidclient -p 80 mgr:info /usr/local/squid/bin/squidclient -p 80 mgr:menu (menu可以查看支持的命令)。 |
Squid 3.5安装和配置
猜你喜欢
转载自blog.csdn.net/yizhuanlu9607/article/details/78436847
今日推荐
周排行