Squid 缓存服务

1、缓存服务器结束

缓存服务器(cache server),即用来存储(介质为内存及硬盘)用户访问的网页、图片、文件等等信息的专用服务器,这种服务器不仅可以使用户可以最快的得到他们想要的信息,而且可以大大减少服务端网络传输的数据量,缓存服务器往往也是代理服务器,对于网站的用户来说,缓存服务器和代理是不可见的,即在用户看来所有的网站信息都是来自其正在访问的网站,而实际上可能是缓存服务器在提供访问数据

目前国内互联网公司常用的缓存服务器有:squid、varnish、nginx、ats。

第一次用户访问网站,先到squiq,但是squid没有数据,所以sqquid代理用户去服务端访问,之后数据也会保留在squid,所以下次用户访问的时候,可以直接从squid中获得,而不必再到服务器中取得数据。

官网:http://www.squid-cache.org/

2、web缓存的相关概念

a、cache命中

cache命中是在cache server每次从它的缓存里满足客户端http请求发送。cache命中率,是所有客户端HTTP请求中的命中比例,web缓存典型的cache命中率在30%到60%之间,另一个相似的度量单位叫做字节命中率,描述了cache提供服务的数据容量。

提供cache命中率的方法:

1、nginx/apache expries,cache-control缓存头

2、动静分离,静态化,对静态走CDN

3、设置数据库的cache等,如设置mysql 的cache,让缓存靠前

4、4XX/5XX之类的错误页面,死链无法缓存。

5、动态不走缓存

b、cache丢失

cache丢失在cache server不能从它的缓存里面满足客户端HTTP请求时发生,cache丢失的原因有很多种:

1、当cache server第一次接受到对第一个新资源的请求时,就会产生一个cache丢失,如何解决第一次命中?

先预热或者预取

a、内部先请求访问,可以通过脚本实现(但是这个思想不太靠谱)。

b、后端生成数据之后,统一推到前端cache server,即预取、预热

2、存储空间慢,或者对象自身过期,cache server会清除这些缓存对象,以释放空间给新对象

如果解决问题2:

a、增大内存或者磁盘。

b、增大对象缓存时间

c、参数设置,缓存参数设置打一下,最大缓存对象2M(想办法多缓存热门的数据)

d、分资源缓存,如1M/10M/100M不同大小的分开缓存。(分拆服务器,acl正则匹配抛给不同的pools)

3、还有可能是客户访问的资源不可到达,原始服务器指示cache server怎样处理用户响应,如:他会提示数据不能被缓存,或在有限的时间内才被重复使用等等。

a、

c、cache确认

cache确认保证cache server不对访问的用户返回过期的数据,在重复使用缓存对象时,cache server需要经常从原始服务器确认它,假如服务器指示squid的拷贝仍旧有效,数据就发送出去,否则,squid更新它的存储拷贝,并且转发给客户

当用户更新了数据到数据库或者存储服务器的时候,可以从业务角度主动调用接口清楚该对象缓存的指令。

对应缓存来讲,数据的一致性是一个特别头疼的问题,特别是memcached。

CDN删除数据一般需要5-15分钟才能删除完成。

图片放到CDN一般是不要更新了,图片修改算更新,这种业务就要推送。

删除图片再上传,这种不算修改,而是一个新的资源。老是数据,CDN会有相应的算法,在一定的时间内自动删除。

网站改版:在CDN上推送js、css(改名推送)等程序。

3、squid服务介绍

squid是一个高性能的代理和缓存服务器,squid支持FTP/gopher和HTTP协议,和一般的代理缓存软件不同,squid用一个单独的,非模块化的、I/O驱动的进程来处理所有的客户端请求。(gopher是internet上一个非常有名的信息查询系统,它将Internet上的文件组织成某种索引,很方便地将用户从Internet的一处带到另外一处,在WWW出现之前,GopherSHI Internet上主要的信息检索工具)

Squid将数据元缓存到内存或硬盘中,同时也缓存DNS查询结果,Squid只是SSL,支持访问控制,由于使用ICP(轻量Internet缓存协议),Squid能够实现层叠的代理阵列,从而最大限度的节约带宽

Squid Cache简称Squid,是一个流行的代理服务器和WEB缓存服务器软件,Squid服务有相当多的用途。

1、用于放置在WEB服务器的前面,缓存网站WEB服务器的相关数据。这样用户请求缓存服务器就可以直接返回数据给用户了,从而提升了用户的访问网站体验,从另外一方面也减轻了web服务器、数据服务器、图片文件存储服务器等业务服务器的压力。这种应用被称之为反向代理服务

2、用于放置在企业内部关键的出网位置或者某些共享的网络前端,缓存内部上网用户的数据,域名系统和其他网络搜索数据等,这样用户上网请求的数据,就可以有缓存服务器放回给内部用户,而不需要上网了,从而使得内部用户上网更快,更安全,也会大大节约公司的带宽,这种应用被称之为正向代理服务(分为普通代理或者透明代理)。(现在很少用,5年前,带宽很小的时候用得挺多)

3、通过放在网络的关键位置过滤网络流量和访问数据,提升整个网络安全,例如:可以监控及限制内部企业员工的上网行为,可以和iptables配合作为办公网的网关。

4、用作局域网通过代理上网

只要说是一台就可以上网的机器就可以,位置随便,让所有的用户的浏览器设置这个服务器代理上网即可。

Squid代理服务器主要用于类UNIX系统中运行,其发展历史相对悠久,功能也相对完善,除了对HTTP支持很好外,对于FTP和HTTPS的支持也很好,在3.0测试版中也支持了IPV6,Squid的主页在http://www.squid-cache.org/。目前业界主流的CDN都基本是Squid进行二次开发作为cache缓存服务器的

4、三种代理服务器的原理

普通代理(传统代理)

传统的代理服务器就是通过浏览器设置代理的方法。

用户访问网站,先到squid代理服务器,如果squid代理服务器有数据,那么squid就直接从缓存中放回给用户,如果squid缓存中没有,那么squid就代替用户去访问网站,把数据返回的给用户的同时留一份到缓存中,一遍下次用户(或者其他用户)访问的时候,直接从缓存中返回给用户

透明代理:

所谓的透明代理,是相对于普通代理服务而言,客户端不需要做任何和代理服务器相关的设置,对用户而言,感觉不到代理服务器的存在,所以称之为透明代理,即把代理服务器部署在核心的上网出口,当用户上网浏览页面时,会交给代理服务器向外请求,如果结合iptables可以实现代理+网关+内容过滤+流量安全控制等完整的上网解决方案。

透明代理流程说明:

用户A发送一个访问请求到防火墙,由防火墙将该用户的访问请求转发给Squid,squid再先检查自身缓存中有无该用户请求的访问内容,如果没有,则请求远端目的的服务器,获取该用户的访问内容,再放回给用户的同时,在自身缓存保留一份记录以备下次调用,当用户B发送一个和用户A相同的请求到防火墙时,有防火墙转发该用户请求到squid,squid检查到自身缓存发现有相同的内容,直接将该内容放回给用户B。而无需再次去访问请求的服务器。

普通代理和透明代理的区别就是,普通代理需要在客户端浏览器设置指定代理服务器,而透明代理不需要。

反向代理服务原理:

普通代理方式是代理内部网络用户请求internet上服务器的连接请求,客户端必须指定代理服务器,并将本来要直接发送到internet上的服务器的连接请求发送给代理服务器处理,

反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从内部服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外表现为一个服务器。

反向代理流程说明:

squid做反向代理服务器,通常工作在一个服务器集群的前端,在用户看来,squid服务器就是他所要访问的服务器,而实际意义上,squid只是接受用户的请求,同时将用户请求转发给内部真正的WEB服务器,如果squid本身有用户要访问的内容,则squid直接将数据返回给用户,起到了缓存数据的作用,减少了后端服务器的压力

三种代理的区别:

5、什么时候需要用squid(CDN)

a、想节省带宽及服务器成本

b、想提高访问速度,提升用户体验

c、源站扛不住了

6、haproxy和squid代理的区别

有了haproxy,后面还需要squid代理吗?

squid缓存及代理功能,一般用squid是用来作为缓存服务器

haproxy做动态及静态的代理,及负载均衡

squid分拆动态和静态进行缓存。

常见架构:

动态数据======>静态化=====>CDN

很多CDN现在逐渐开始支持动态加速业务。

7、如何选择squid服务的版本

squid2.5:有点老,不支持epoll,内存管理也不完善,例如可能常常会引起重新

squid2.6-2.7:建议使用,性能不错,2.7很建议使用,以为基本有了3.0的所以特性

squid3.0:不建议使用,因为使用C++全部重写,现在性能不如2.6和2.7,以后观察

8、部署squid需要的硬件环境

a、第一重要资源:内存

squid对硬件的要求最主要就是内存资源,内存资源短缺会严重影响性能,因为所有的对象都会尽可能的被缓存到内存中,这样才能更快提升用户的响应及返回数据

b、第二资源:磁盘

磁盘空间也是另外一个squid能够高效运行的重要因素,更多的磁盘空间意外着更多的缓存目标和更高的命中率,快速的磁盘介质也是很有必要的,例如用ssd,sas替代sata磁盘,除了使用raid外,可以指定多个磁盘路径缓存

9、squid的编译和安装

部署前环境准备

环境 :

  1. squid server IP:10.0.0.4
  2. WEB server IP:10.0.0.3
 
  1. 1.更改yum源:
  2. wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
  3. yum install tree -y
  4. grep keepcache /etc/yum.conf
  5. sed -i 's/keepcache=0/keepcache=1/g' /etc/yum.conf
  6. grep keepcache /etc/yum.conf
  7. 2.关闭selinux
  8. setenforce 0 #临时生效
  9. sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config #永久生效
  10. 3.关闭防火墙
  11. /etc/init.d/iptables stop
  12. chkconfig iptables off
  13. chkconfig --list iptables
  14. 4.时间同步
  15. /usr/sbin/ntpdate pool.ntp.org
  16. echo '#time sync by gao at 2010-2-1'>>/var/spool/cron/root
  17. echo '*/10 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1'>>/var/spool/cron/root
  18. crontab -l
  19. 5.修改主机名
  20. =======================================
  21. hostname Squid
  22. cp /etc/hosts /etc/hosts.bak
  23. cp /etc/sysconfig/network /etc/sysconfig/network.bak
  24. sed -i '$a 10.0.0.4 WEB' /etc/hosts
  25. sed -i '$a 10.0.0.3 Squid' /etc/hosts
  26. sed -i -e '/HOSTNAME=/d' -e '1aHOSTNAME=WEB' /etc/sysconfig/network
  27. logout
  28. =======================================
  29. hostname WEB
  30. cp /etc/hosts /etc/hosts.bak
  31. cp /etc/sysconfig/network /etc/sysconfig/network.bak
  32. sed -i '$a 10.0.0.4 WEB' /etc/hosts
  33. sed -i '$a 10.0.0.3 Squid' /etc/hosts
  34. sed -i -e '/HOSTNAME=/d' -e '1aHOSTNAME=Squid' /etc/sysconfig/network
  35. logout
  36. =======================================
  37. 6.调整文件描述符
  38. ulimit -Hn 20480
  39. echo "ulimit -Hn 20480" >>/etc/rc.local
  40. tail -1 /etc/rc.local
  41. ulimit -n
  42. echo "* - nofile 20480">>/etc/security/limits.conf
  43. tail -2 /etc/security/limits.conf
  44. 7.调整临时端口
  45. cat /proc/sys/net/ipv4/ip_local_port_range
  46. echo "net.ipv4.ip_local_port_range = 4000 65000">>/etc/sysctl.conf
  47. sysctl -p|grep port_range
 

###记得一定要做时间同步

##开始编译安装Squid

在10.0.0.3操作

  1. 创建放置软件的目录
  2. mkdir -p /home/lvnian/tools
  3. cd /home/lvnian/tools
  4. 下载并安装squid
  5. #wget http://www.it.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE20.tar.gz
  6. #wget http://down1.chinaunix.net/distfiles/squid-3.0.STABLE20.tar.gz
  7. #http://www.it.squid-cache.org/Versions/v3/3.0
  8. wget http://www.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE20.tar.gz
  9. tar xf squid-3.0.STABLE20.tar.gz
  10. cd squid-3.0.STABLE20
  11. ./configure --prefix=/application/squid3.0 \
  12. --enable-asyno-io=100 \
  13. --with-pthreads \
  14. --enable-storeio="aufs,diskd,ufs" \
  15. --enable-removal-policies='heap,lru' \
  16. --enable-icmp \
  17. --enable-delay-pools \
  18. --enable-useragent-log \
  19. --enable-referer-log \
  20. --enable-kill-parent-hack \
  21. --enable-cachemgr-hostname=localhost \
  22. --enable-arp-acl \
  23. --enable-default-err-language=English \
  24. --enable-err-language="Simplify_Chinese" \
  25. --disable-poll \
  26. --disable-wccp \
  27. --disable-ident-lookups \
  28. --disable-internal-dns \
  29. --enable-basic-auth-helpers="NCSA" \
  30. --enable-stacktrace \
  31. --with-filedescriptors=64000 \
  32. --enable-ssl \
  33. --enable-x-accelerator-very \
  34. --disable-snmp \
  35. --with-aio \
  36. --enable-linux-netfilter \
  37. --enable-linux-tproxy
  38. ==========================================================
  39. make && make install
  40. ##############################################
  41. make 报错解决
  42. structs.h:738: error: ISO C++ forbids declaration of SSL_CTX with no type
  43. structs.h:738: error: expected ‘;’ before ‘*’ token
  44. structs.h:1160: error: ISO C++ forbids declaration of SSL_CTX with no type
  45. structs.h:1160: error: expected ‘;’ before ‘*’ token
  46. structs.h:1161: error: ISO C++ forbids declaration of SSL_SESSION with no type
  47. structs.h:1161: error: expected ‘;’ before ‘*’ token
  48. make[1]: *** [cf_gen.o] Error 1
  49. make[1]: Leaving directory `/home/lvnian/tools/squid-3.0.STABLE20/src'
  50. make: *** [all-recursive] Error 1
  51. [root@Squid squid-3.0.STABLE20]#
  52. 安装openssl*
  53. yum install openssl* -y
  54. cd /home/lvnian/tools
  55. rm -rf squid-3.0.STABLE20
  56. tar xf squid-3.0.STABLE20.tar.gz
  57. cd squid-3.0.STABLE20
  58. ##############################################
 

#制作软连接

  1. ln -s /application/squid3.0 /application/squid
 

到这来安装squid完毕:

squid 目录结构介绍:

  1. 文件名/目录名功能描述
  2. sbin squid主从程序的目录,正常只能被root启动
  3. sbin/squid squid的主程序
  4. bin bin目录包含对所有用户可用的程序
  5. bin/RunAccel RunAccelRunCache几乎一致,唯一不同时他增加了一个命令行参数,告诉squid在哪里侦听HTTP请求
  6. bin/RunCache RunCache是一个脚本,你能用它来启动squid,假如squid死掉,该脚本自动重启它,除非它检测到经常的重启
  7. bin/squidclient squidclient是一个简单的HTTP客户端程序,你能用它来测试squid,他也有一些特点的功能,用以对运行的squid进程发起管理请求
  8. libexec libexec目录包含了辅助程序,有一些命令你不能正常启动,然而,这程序通常被其他程序启动
  9. libexec/unlinkd unlinkd是一个辅助程序,它从cache目录里删除文件
  10. libexec/cachemgr.cgi cachemgr.cgisquid管理功能的CGI接口,为了使用它,你需要拷贝该程序到你的web服务的cgi-bin目录下
  11. libexec/diskd diskd假如你知道了 --enable-storeio=diskd,你才能看到它
  12. libexec/pinger pinger假如你知道了 --enable-icmp,你才能看到它
  13. etc etc包含squid的主配置文件
  14. etc/squid.conf 这是squid的主配置文件
  15. var var目录包含了不是很重要的和经常变化的文件,这些文件不必正常备份他们
  16. var/logs var/logs目录是squid不同日志文件的默认位置,当你第一次安装squid时,他是空的,一旦squid开始运行,你能在这里看到名字为access.logcache.logstore.log这样的文件
  17. var/cache 假如你不在squid.conf文件里指定,这是默认的cache缓存目录(cache_dir
 

squid.conf 语法介绍

  1. [root@Squid etc]# pwd
  2. /application/squid/etc
  3. [root@Squid etc]# ls
  4. cachemgr.conf cachemgr.conf.default mime.conf mime.conf.default squid.conf squid.conf.default
  5. [root@Squid etc]#
 

上面中以default为后缀的都是对于配置文件的备份

#看去掉注销和空行之后的配置文件

  1. [root@Squid etc]# cat squid.conf.default|egrep -v '^#|^$'
  2. acl manager proto cache_object
  3. acl localhost src 127.0.0.1/32
  4. acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
  5. acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
  6. acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
  7. acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
  8. acl SSL_ports port 443
  9. acl Safe_ports port 80 # http
  10. acl Safe_ports port 21 # ftp
  11. acl Safe_ports port 443 # https
  12. acl Safe_ports port 70 # gopher
  13. acl Safe_ports port 210 # wais
  14. acl Safe_ports port 1025-65535 # unregistered ports
  15. acl Safe_ports port 280 # http-mgmt
  16. acl Safe_ports port 488 # gss-http
  17. acl Safe_ports port 591 # filemaker
  18. acl Safe_ports port 777 # multiling http
  19. acl CONNECT method CONNECT
  20. http_access allow manager localhost
  21. http_access deny manager
  22. http_access deny !Safe_ports
  23. http_access deny CONNECT !SSL_ports
  24. http_access allow localnet
  25. http_access deny all
  26. icp_access allow localnet
  27. icp_access deny all
  28. htcp_access allow localnet
  29. htcp_access deny all
  30. http_port 3128
  31. hierarchy_stoplist cgi-bin ?
  32. access_log /application/squid3.0/var/logs/access.log squid
  33. refresh_pattern ^ftp: 1440 20% 10080
  34. refresh_pattern ^gopher: 1440 0% 1440
  35. refresh_pattern (cgi-bin|\?) 0 0% 0
  36. refresh_pattern . 0 20% 4320
  37. icp_port 3130
  38. coredump_dir /application/squid3.0/var/cache
  39. [root@Squid etc]#
 

#创建加用户

  1. useradd squid -s /sbin/nologin -M
  2. id squid
 

#调整配置文件

  1. vi /etc/squid.conf
  2. #############################################
  3. 修改下面的用户为squid,注意不能是rootroot无法执行
  4. 2941 # cache_effective_user nobody
  5. 2941 cache_effective_user squid
  6. 或者用sed修改
  7. grep "cache_effective_user " squid.conf
  8. sed -i "s/# cache_effective_user nobody/cache_effective_user squid/g" squid.conf
  9. grep "cache_effective_user " squid.conf
  10. ============================================
 

#添加组

  1. sed -i "/cache_effective_user squid/a cache_effective_group squid" squid.conf
  2. grep "cache_effective" squid.conf
  3. ============================================
  4. [root@Squid etc]# grep "cache_effective" squid.conf
  5. # TAG: cache_effective_user
  6. # see also; cache_effective_group
  7. cache_effective_user squid
  8. cache_effective_group squid
  9. # TAG: cache_effective_group
  10. [root@Squid etc]#
  11. ============================================
 

Squid日志文件

squid默认的日志你是是squid安装位置下的logs目录,例如,假如你在./configure中没有使用--prefix=选项,那么默认的日志文件路径是/usr/local/squid/var/logs,必须确认日志文件所存在的磁盘位置空间足够多,在squid写日志如果接受到错误,它会退出和重启。该行为的主要理由应引起你的注意,squid想确认你不会丢失任何重要的日志信息,特别你的系统被滥用或者被攻击时。

squid有三个主要的日志文件:cache.log access.log store.log

cache.log日志文件

cache.log 包含多种消息,例如Squid的配置信息,性能警告,以及严重错误,如下是cache.log的输出样本,主要的错误和异常条件最可能报告在cache.log里。

刚开始运行squid时,需要密切关注该文件,加入squid拒绝运行,原因也行会出现在cache.log文件的结尾处,在正常条件下,该文件不会变化很大,加入你以-s选型来运行squid,重要的cache.log消息也会被发送到你的syslog进程,通过使用cache_log指令,你可以修改配置文件squid.conf来改变该日志文件路径。

转发cache.log消息到系统日志

为了让squid发送到cache.log消息的拷贝到系统日志,请使用-s命令选型,仅仅在debug级别0和1的消息被转发,级别0的消息以syslog级别LOG_WARGING记录,级别为1的消息以syslog级别为LOG_NOTICE记录,所有消息使用LOCAL4的syslog设备,如下配置syslogd的一个方法,以便这些消息能保存下来

  1. local4.warning/var/log/squid.log
 

在维护多个squid主机时,使用syslog来记录cache.log特别方便,可以配置每个本机syslog进程,转发这些消息到中央日志主机,这样就可以在一个地方统一浏览所有cache日志,例如。可以在/etc/sysconfigd.conf 里使用如下接口

  1. local4.notice@192.168.1.123
 

access.log日志文件

Squid把关于HTTP事务的关键信息存在access.log里,该文件是基于行,也就说每行对应一个客户端请求,squid记录客户端IP(或主机名)、请求URL、响应size、和其他信息

Squid 在access.log 里记录所有HTTP访问,除了那些在还没有发送数据前就断开的连接,squid也记录所有ICP事务,除非你使用log_icp_querise指令关闭了这个功能,

store.log日志文件

store.log记录Squid关于存储或删除cache目标的决定,对每个存在cache里的目标,每个不可cache的目标,已经每个被轮换策略删除的目标,Squid都会创建相关的日志条目,该日志文件内容包含了内存cache,又包含了磁盘cache。

squid的日志文件增加没有限制,为了保证日志文件大小合理,应创建计划规律的重命名和打包日志,squid有内建的日志回滚功能,也可以避免单个日志过滤。

squid的访问控制

理解squid如何搜索ACL元素去匹配是很重要的,当ACL元素有多个值时,任何单个值都能导致匹配,换句话说,squid在检查ACL元素值时使用OR逻辑,当squid找到第一个值匹配时,他停止搜索,这意味着把最可能匹配的值放在列表的开头出,能减少延时。

重点强调:

a、squid在搜索ACL元素时使用或逻辑,在acl里的任何单值都可以导致匹配。

b、而应用访问规则恰好相反,对http_access和其他规则设置,squid使用与逻辑。

squid默认的配置文件拒绝每一个客户请求,在任何人能使用代理之前,你必须在squid.conf文件里加入附加的访问控制规则,最简单的一个方式,就是定义一个针对客户IP地址的ACL和一个访问规则,告诉Squid运行来自这些地址的HTTP请求,squid有许多不同的ACL类型,src类型匹配客户端IP,squid会针对客户HTTP请求检查http_access规则。

如:

  1. acl mynet src 10.0.0.0/24
  2. htcp_access allow mynet
 

这两行需要放在正确的位置,http_access的顺序非常重要,但是ACL行的顺序不必介意,squid默认的配置文件包含了一些重要的访问控制,最好不要改变她或者删除它们,除非你完全理解它们的意思。

##访问日志,保存默认,修改为如下

  1. access_log /application/squid3.0/var/logs/access.log squid
  2. ##修改存储日志打开
  3. ============================================
  4. sed -i 's/# cache_store_log/cache_store_log/g' squid.conf
  5. grep "cache_store" squid.conf
  6. ============================================
  7. [root@Squid etc]# grep "cache_store" squid.conf
  8. # TAG: cache_store_log
  9. cache_store_log /application/squid3.0/var/logs/store.log
  10. ============================================
 

#打开缓存log

  1. ============================================
  2. grep cache_log squid.conf
  3. sed -i 's/# cache_log/cache_log/g' squid.conf
  4. grep cache_log squid.conf
  5. ============================================
  6. [root@Squid etc]# grep cache_log squid.conf
  7. # TAG: cache_log
  8. cache_log /application/squid3.0/var/logs/cache.log
  9. ============================================
 

#####修改缓存目录,也可以配置多个

  1. grep cache_dir squid.conf
  2. sed -i 's%# cache_dir ufs /application/squid3.0/var/cach%cache_dir ufs /application/squid3.0/var/cach%g' squid.conf
  3. grep cache_dir squid.conf
  4. ============================================
  5. [root@Squid etc]# grep cache_dir squid.conf
  6. # Applies to any cache_dir lines listed below this.
  7. # TAG: cache_dir
  8. # cache_dir Type Directory-Name Fs-specific-data [options]
  9. # You can specify multiple cache_dir lines to spread the
  10. # cache_dir ufs Directory-Name Mbytes L1 L2 [options]
  11. # cache_dir aufs Directory-Name Mbytes L1 L2 [options]
  12. # cache_dir diskd Directory-Name Mbytes L1 L2 [options] [Q1=n] [Q2=n]
  13. # block-size=n defines the "block size" for COSS cache_dir's.
  14. # leads to a maximum cache_dir size of 512<<24, or 8 GB. Note
  15. # has written some objects to the cache_dir.
  16. # no-store, no new objects should be stored to this cache_dir
  17. # the cache_dir lines with the smallest max-size value first and the
  18. cache_dir ufs /application/squid3.0/var/cache 100 16 256
  19. # 'cache_dir' directory, but you may specify an alternate
  20. # a representation of the cache_dir name where each / is replaced
  21. # with '.'. This is needed to allow adding/removing cache_dir
  22. # If have more than one 'cache_dir', and %s is not used in the name
  23. # corresponds to the order of the 'cache_dir' lines in this
  24. # configuration file. If you change the order of the 'cache_dir'
  25. # the correct 'cache_dir' entry (unless you manually rename
  26. # better to keep these index files in each 'cache_dir' directory.
  27. [root@Squid etc]#
  28. ============================================
 

##编辑可见主机名,如果不配置,可能无法启动squid

  1. sed -n '/visible_hostname/p' squid.conf
  2. sed -i '/# TAG: visible_hostname/avisible_hostname img01.etiantian.org ' squid.conf
  3. sed -n '/visible_hostname/p' squid.conf
  4. ============================================
  5. [root@Squid etc]# sed -n '/visible_hostname/p' squid.conf
  6. # TAG: visible_hostname
  7. visible_hostname img01.etiantian.org
  8. # 'visible_hostname' you must give each machine a different
  9. [root@Squid etc]#
  10. ============================================
 

##配置配置管理员联系信息cache_mgr配置这个,在网站出问题的时候,客户可以找到我

  1. sed -n '/cache_mgr /p' squid.conf
  2. sed -i 's/# cache_mgr webmaster/cache_mgr 75685538@qq.com/g' squid.conf
  3. sed -n '/cache_mgr /p' squid.conf
  4. ============================================
  5. [root@Squid etc]# sed -n '/cache_mgr /p' squid.conf
  6. cache_mgr 75685538@qq.com
  7. [root@Squid etc]#
  8. ============================================
 

对比修改哪些内容:

  1. ##############################################################################
  2. ##############################################################################
  3. [root@Squid etc]# diff squid.conf squid.conf.default
  4. 1710c1710
  5. < cache_dir ufs /application/squid3.0/var/cache 100 16 256
  6. ---
  7. > # cache_dir ufs /application/squid3.0/var/cache 100 16 256
  8. 1889c1889
  9. < cache_log /application/squid3.0/var/logs/cache.log
  10. ---
  11. > # cache_log /application/squid3.0/var/logs/cache.log
  12. 1899c1899
  13. < cache_store_log /application/squid3.0/var/logs/store.log
  14. ---
  15. > # cache_store_log /application/squid3.0/var/logs/store.log
  16. 2912c2912
  17. < cache_mgr 75685538@qq.com
  18. ---
  19. > # cache_mgr webmaster
  20. 2941,2942c2941
  21. < cache_effective_user squid
  22. < cache_effective_group squid
  23. ---
  24. > # cache_effective_user nobody
  25. 2971d2969
  26. < visible_hostname img01.etiantian.org
  27. [root@Squid etc]#
  28. ##############################################################################
 

修改后的配置文件

  1. [root@Squid etc]# egrep -v '^#|^$' squid.conf
  2. acl manager proto cache_object
  3. acl localhost src 127.0.0.1/32
  4. acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
  5. acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
  6. acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
  7. acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
  8. acl SSL_ports port 443
  9. acl Safe_ports port 80 # http
  10. acl Safe_ports port 21 # ftp
  11. acl Safe_ports port 443 # https
  12. acl Safe_ports port 70 # gopher
  13. acl Safe_ports port 210 # wais
  14. acl Safe_ports port 1025-65535 # unregistered ports
  15. acl Safe_ports port 280 # http-mgmt
  16. acl Safe_ports port 488 # gss-http
  17. acl Safe_ports port 591 # filemaker
  18. acl Safe_ports port 777 # multiling http
  19. acl CONNECT method CONNECT
  20. http_access allow manager localhost
  21. http_access deny manager
  22. http_access deny !Safe_ports
  23. http_access deny CONNECT !SSL_ports
  24. http_access allow localnet
  25. http_access deny all
  26. icp_access allow localnet
  27. icp_access deny all
  28. htcp_access allow localnet
  29. htcp_access deny all
  30. http_port 3128
  31. hierarchy_stoplist cgi-bin ?
  32. cache_dir ufs /application/squid3.0/var/cache 100 16 256
  33. access_log /application/squid3.0/var/logs/access.log squid
  34. cache_log /application/squid3.0/var/logs/cache.log
  35. cache_store_log /application/squid3.0/var/logs/store.log
  36. refresh_pattern ^ftp: 1440 20% 10080
  37. refresh_pattern ^gopher: 1440 0% 1440
  38. refresh_pattern (cgi-bin|\?) 0 0% 0
  39. refresh_pattern . 0 20% 4320
  40. cache_mgr 75685538@qq.com
  41. cache_effective_user squid
  42. cache_effective_group squid
  43. visible_hostname img01.etiantian.org
  44. icp_port 3130
  45. coredump_dir /application/squid3.0/var/cache
  46. [root@Squid etc]#
  47. ##############################################################################
 

##启动squid

  1. [root@Squid etc]# /application/squid/sbin/squid -h
  2. Usage: squid [-cdhvzCDFNRVYX] [-s | -l facility] [-f config-file] [-[au] port] [-k signal]
  3. -a port Specify HTTP port number (default: 3128).
  4. -d level Write debugging to stderr also.
  5. -f file Use given config-file instead of
  6. /application/squid3.0/etc/squid.conf
  7. -h Print help message.
  8. -k reconfigure|rotate|shutdown|interrupt|kill|debug|check|parse
  9. Parse configuration file, then send signal to
  10. running copy (except -k parse) and exit.
  11. -s | -l facility
  12. Enable logging to syslog.
  13. -u port Specify ICP port number (default: 3130), disable with 0.
  14. -v Print version.
  15. -z Create swap directories
  16. -C Do not catch fatal signals.
  17. -D Disable initial DNS tests.
  18. -F Don't serve any requests until store is rebuilt.
  19. -N No daemon mode.
  20. -R Do not set REUSEADDR on port.
  21. -S Double-check swap during rebuild.
  22. -X Force full debugging.
  23. -Y Only return UDP_HIT or UDP_MISS_NOFETCH during fast reload.
 

简单命令说明:

  1. /application/squid/sbin/squid -z 初始化缓存空间
  2. /application/squid/sbin/squid 启动
  3. /application/squid/sbin/squid -k shutdown 停止
  4. /application/squid/sbin/squid -k reconfigure 重新载入配置文件
  5. /application/squid/sbin/squid -k rotate 轮循日志
  6. /application/squid/sbin/squid -k parse检查语法
 

#检查配置文件语法

  1. /application/squid/sbin/squid -k parse
  2. chown -R squid.squid /application/squid3.0/var/logs/
  3. /application/squid/sbin/squid -k parse
  4. =====================================================================
  5. [root@Squid etc]# /application/squid/sbin/squid -k parse
  6. 2015/09/08 11:23:45| Processing Configuration File: /application/squid3.0/etc/squid.conf (depth 0)
  7. 2015/09/08 11:23:45| Initializing https proxy context
  8. WARNING: Cannot write log file: /application/squid3.0/var/logs/cache.log
  9. /application/squid3.0/var/logs/cache.log: Permission denied 《=============语法问题。这个文件没有出现
  10. messages will be sent to 'stderr'.
  11. [root@Squid etc]# chown -R squid.squid /application/squid3.0/var/logs/
  12. [root@Squid etc]# /application/squid/sbin/squid -k parse
  13. 2015/09/08 11:24:49| Processing Configuration File: /application/squid3.0/etc/squid.conf (depth 0)
  14. 2015/09/08 11:24:49| Initializing https proxy context
  15. [root@Squid etc]#
  16. =====================================================================
 

###设置环境变量

  1. echo 'export PATH=$PATH:/application/squid/sbin:=/application/squid/bin '>>/etc/profile
  2. . /etc/profile
  3. echo $PATH
 

###初始化squid

  1. grep cache_dir squid.conf
  2. ll /application/squid3.0/var/cache
  3. ll /application/squid3.0/var/
  4. squid -z /application/squid3.0/var/
  5. chown -R squid.squid /application/squid3.0/var
  6. squid -z /application/squid3.0/var/
  7. tree /application/squid3.0/var/ |wc -l
  8.  
  9. =====================================================================
  10. [root@Squid etc]# grep cache_dir squid.conf
  11. # Applies to any cache_dir lines listed below this.
  12. # TAG: cache_dir
  13. # cache_dir Type Directory-Name Fs-specific-data [options]
  14. # You can specify multiple cache_dir lines to spread the
  15. # cache_dir ufs Directory-Name Mbytes L1 L2 [options]
  16. # cache_dir aufs Directory-Name Mbytes L1 L2 [options]
  17. # cache_dir diskd Directory-Name Mbytes L1 L2 [options] [Q1=n] [Q2=n]
  18. # block-size=n defines the "block size" for COSS cache_dir's.
  19. # leads to a maximum cache_dir size of 512<<24, or 8 GB. Note
  20. # has written some objects to the cache_dir.
  21. # no-store, no new objects should be stored to this cache_dir
  22. # the cache_dir lines with the smallest max-size value first and the
  23. cache_dir ufs /application/squid3.0/var/cache 100 16 256
  24. # 'cache_dir' directory, but you may specify an alternate
  25. # a representation of the cache_dir name where each / is replaced
  26. # with '.'. This is needed to allow adding/removing cache_dir
  27. # If have more than one 'cache_dir', and %s is not used in the name
  28. # corresponds to the order of the 'cache_dir' lines in this
  29. # configuration file. If you change the order of the 'cache_dir'
  30. # the correct 'cache_dir' entry (unless you manually rename
  31. # better to keep these index files in each 'cache_dir' directory.
  32. [root@Squid etc]# ll /application/squid3.0/var/cache
  33. ls: cannot access /application/squid3.0/var/cache: No such file or directory
  34. [root@Squid etc]#
  35. [root@Squid etc]#
  36. [root@Squid etc]# ll /application/squid3.0/var/
  37. total 4
  38. drwxr-xr-x. 2 squid squid 4096 Nov 10 00:15 logs
  39. [root@Squid etc]# squid -z /application/squid3.0/var/
  40. 2015/11/10 10:09:45| Creating Swap Directories
  41. FATAL: Failed to make swap directory /application/squid3.0/var/cache: (13) Permission denied
  42. [root@Squid etc]# chown -R squid.squid /application/squid3.0/var
  43. [root@Squid etc]# squid -z /application/squid3.0/var/
  44. 2015/11/10 10:10:09| Creating Swap Directories
  45. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/00
  46. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/01
  47. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/02
  48. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/03
  49. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/04
  50. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/05
  51. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/06
  52. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/07
  53. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/08
  54. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/09
  55. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0A
  56. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0B
  57. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0C
  58. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0D
  59. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0E
  60. 2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0F
  61. [root@Squid etc]# tree /application/squid3.0/var/ |wc -l
  62. 4118
  63. [root@Squid etc]#
  64. =====================================================================
 

###启动squid

  1. squid -N -d1
  2. 初始化cache目录后,就可以在终端窗口里运行squid,将日志记录到标准输出,这样就可以轻易的定位任何错误或者问题,并且确认squid是否成功启动,使用-N参数,会持续squid在前台运行。-d1选项在标准错误里显示1级错误的调试信息,假如你看到错误信息,你该首先修正它,一旦你见到“Ready to serve requests. ”消息,就可用一些http请来测试squid
  3. =====================================================================
  4. [root@Squid etc]# squid -N -d1
  5. 2015/09/08 11:37:46| Starting Squid Cache version 3.0.STABLE20 for x86_64-unknown-linux-gnu...
  6. 2015/09/08 11:37:46| Process ID 51053
  7. 2015/09/08 11:37:46| With 20480 file descriptors available
  8. 2015/09/08 11:37:46| Performing DNS Tests...
  9. 2015/09/08 11:37:46| Successful DNS name lookup tests...
  10. 2015/09/08 11:37:46| helperOpenServers: Starting 5/5 'dnsserver' processes
  11. 2015/09/08 11:37:46| User-Agent logging is disabled.
  12. 2015/09/08 11:37:46| Referer logging is disabled.
  13. 2015/09/08 11:37:47| Unlinkd pipe opened on FD 14
  14. 2015/09/08 11:37:47| Swap maxSize 102400 + 8192 KB, estimated 8507 objects
  15. 2015/09/08 11:37:47| Target number of buckets: 425
  16. 2015/09/08 11:37:47| Using 8192 Store buckets
  17. 2015/09/08 11:37:47| Max Mem size: 8192 KB
  18. 2015/09/08 11:37:47| Max Swap size: 102400 KB
  19. 2015/09/08 11:37:47| Version 1 of swap file without LFS support detected...
  20. 2015/09/08 11:37:47| Rebuilding storage in /application/squid3.0/var/cache (CLEAN)
  21. 2015/09/08 11:37:47| Using Least Load store dir selection
  22. 2015/09/08 11:37:47| Set Current Directory to /application/squid3.0/var/cache
  23. 2015/09/08 11:37:47| Loaded Icons.
  24. 2015/09/08 11:37:47| Accepting HTTP connections at 0.0.0.0, port 3128, FD 16.
  25. 2015/09/08 11:37:47| Accepting ICP messages at 0.0.0.0, port 3130, FD 17.
  26. 2015/09/08 11:37:47| HTCP Disabled.
  27. 2015/09/08 11:37:47| Pinger socket opened on FD 19
  28. 2015/09/08 11:37:47| Ready to serve requests. <=================表示成功
  29. 2015/09/08 11:37:47| Done reading /application/squid3.0/var/cache swaplog (0 entries)
  30. 2015/09/08 11:37:47| Finished rebuilding storage from disk.
  31. 2015/09/08 11:37:47| 0 Entries scanned
  32. 2015/09/08 11:37:47| 0 Invalid entries.
  33. 2015/09/08 11:37:47| 0 With invalid flags.
  34. 2015/09/08 11:37:47| 0 Objects loaded.
  35. 2015/09/08 11:37:47| 0 Objects expired.
  36. 2015/09/08 11:37:47| 0 Objects cancelled.
  37. 2015/09/08 11:37:47| 0 Duplicate URLs purged.
  38. 2015/09/08 11:37:47| 0 Swapfile clashes avoided.
  39. 2015/09/08 11:37:47| Took 0.02 seconds ( 0.00 objects/sec).
  40. 2015/09/08 11:37:47| Beginning Validation Procedure
  41. 2015/09/08 11:37:47| Completed Validation Procedure
  42. 2015/09/08 11:37:47| Validated 25 Entries
  43. 2015/09/08 11:37:47| store_swap_size = 0
  44. 2015/09/08 11:37:48| storeLateRelease: released 0 objects
  45. =====================================================================
 

###不能停止另外一个窗口,查看是否启动

  1. netstat -lntup|grep 31
  2. =====================================================================
  3. [root@Squid ~]# netstat -lntup|grep 31
  4. tcp 0 0 0.0.0.0:3128 0.0.0.0:* LISTEN 6993/squid
  5. udp 0 0 0.0.0.0:3130 0.0.0.0:* 6993/squid
  6. [root@Squid ~]#
  7. =================================================================
 

====

#上面的是普通代理

用法:

打开IE浏览器>工具>连接>局域网设置>为LAN使用代理服务器

把squid服务器ip和3128端口填上去即可

之后重新打开IE,浏览网页。

一边看日志,一般通过IE浏览,会看到访问日志不停滚动

tailf /application/squid/var/logs/access.log

看服务器日志

  1. =================
  2. [root@Squid ~]# tailf /application/squid/var/logs/access.log
  3. 1447122191.894 23 10.0.0.1 TCP_MISS/200 7428 GET http://img.firefoxchina.cn/2015/11/8/201511090953240.jpg - DIRECT/222.186.20.109 image/jpeg
  4. 1447122191.895 24 10.0.0.1 TCP_MISS/200 5339 GET http://img.firefoxchina.cn/2015/11/8/201511090954000.jpg - DIRECT/58.220.2.124 image/jpeg
  5. 1447122191.900 29 10.0.0.1 TCP_MISS/200 20789 GET http://img.firefoxchina.cn/2015/11/4/201511100926100.jpg - DIRECT/58.220.2.10 image/jpeg
  6. 1447122191.901 27 10.0.0.1 TCP_MISS/200 25635 GET http://img.firefoxchina.cn/2015/11/8/201511091118200.jpg - DIRECT/58.217.195.246 image/jpeg
  7. 1447122191.916 41 10.0.0.1 TCP_MISS/200 6183 GET http://img.firefoxchina.cn/2015/11/8/201511091539590.jpg - DIRECT/222.186.20.123 image/jpeg
  8. 1447122193.337 30012 10.0.0.1 TCP_MISS/200 2878 CONNECT g.alicdn.com:443 - DIRECT/222.73.134.40 -
  9. 1447122193.493 30011 10.0.0.1 TCP_MISS/200 2878 CONNECT img.alicdn.com:443 - DIRECT/101.226.178.140 -
  10. [root@Squid ~]# tree /application/squid3.0/var/cache/|wc -l
  11. 4714
  12. [root@Squid ~]#
  13. [root@Squid ~]# tree /application/squid3.0/var/cache/|wc -l
  14. 4847
  15. [root@Squid ~]#
  16. ##################################前面的就是普通代理模式
  17. #######设置开机自启动
  18. /application/squid/sbin/squid -D ##-D 参数指后台运行
  19. echo "start squid">>/etc/rc.local
  20. echo "/application/squid/sbin/squid -D">>/etc/rc.local
  21. tail -2 /etc/rc.local
  22. ######################################################
 

设置启动脚本

  1. #!/bin/sh
  2. ################################################
  3. #this scripts is created by gao at 20150908
  4. #gao lvnian
  5. ################################################
  6. #!/bin/bash #
  7. # chkconfig: 345 89 24
  8. # description: squid is a web cache server
  9. # processname: squid
  10. . /etc/rc.d/init.d/functions
  11. function_start_squid()
  12. {
  13. /application/squid/sbin/squid -D
  14. if [ $? == "0" ];then
  15. action " $1 squid..." /bin/true
  16. sleep 3
  17. echo "-----------------------check ing-----------------------------"
  18. netstat -lntup|egrep '31|squid'
  19. echo "-----------------------check over-----------------------------"
  20. else
  21. action " $1 squid..." /bin/failed
  22. echo "please check the log"
  23. fi
  24. }
  25. #stop function
  26. function_stop_squid()
  27. {
  28. /application/squid/sbin/squid -k shutdown
  29. if [ $? == "0" ];then
  30. action "$1 squid..." /bin/true
  31. else
  32. action "$1 squid..." /bin/failed
  33. echo "please check the log"
  34. fi
  35. }
  36. #restart function
  37. function_restart_squid()
  38. {
  39. printf "Restarting squid...\n"
  40. function_stop_squid
  41. sleep 2
  42. function_start_squid
  43. }
  44. case $1 in
  45. start)
  46. function_start_squid
  47. ;;
  48. stop)
  49. function_stop_squid
  50. ;;
  51. restart)
  52. function_restart_squid
  53. ;;
  54. *)
  55. printf "Usage: $0 {start|stop|restart}\n"
  56. esac
  57. ========================================================
  58. 把这个文件放到/etc/init.d/目录下并命名为squid
  59. chmod +x /etc/init.d/squid
  60. chkconfig --add squid
  61. ========================================================
  62. [root@Squid ~]# chmod +x /etc/init.d/squid
  63. [root@Squid ~]# chkconfig --add squid
  64. [root@Squid ~]# chkconfig --list squid
  65. squid 0:off 1:off 2:off 3:on 4:on 5:on 6:off
  66. [root@Squid ~]#
 

##设置日志轮询

  1. /application/squid/sbin/squid -k rotate
  2. 查看日志
  3. [root@Squid ~]# ll /application/squid/var/logs/
  4. total 276
  5. -rw-r----- 1 squid squid 99547 Sep 8 13:05 access.log
  6. -rw-r----- 1 squid squid 28301 Sep 8 13:05 cache.log
  7. -rw-r--r-- 1 root squid 6 Sep 8 13:03 squid.pid
  8. -rw-r----- 1 squid squid 134582 Sep 8 13:05 store.log
  9. [root@Squid ~]# /application/squid/sbin/squid -k rotate
  10. [root@Squid ~]# ll /application/squid/var/logs/
  11. total 284
  12. -rw-r----- 1 squid squid 0 Sep 8 13:08 access.log
  13. -rw-r----- 1 squid squid 100020 Sep 8 13:08 access.log.0
  14. -rw-r----- 1 squid squid 458 Sep 8 13:08 cache.log
  15. -rw-r----- 1 squid squid 28301 Sep 8 13:05 cache.log.0
  16. -rw-r--r-- 1 root squid 6 Sep 8 13:03 squid.pid
  17. -rw-r----- 1 squid squid 0 Sep 8 13:08 store.log
  18. -rw-r----- 1 squid squid 135372 Sep 8 13:08 store.log.0
  19. [root@Squid ~]#
  20. #######设置切换时名称改为时间类型
  21. 脚本
  22. echo "0 0 * * * /bin/sh /server/script/rotate_squid.sh" >>/var/spool/cron/root
  23. crontab -l
  24. ####################
  25. mkdir -p /server/script/
  26. cat > /server/script/rotate_squid.sh <EOF
  27. #!/bin/sh
  28. cd /application/squid/var/logs/
  29. [ -f access.log ] && mv access.log access_`data +%F`.log
  30. /application/squid/sbin/squid -k rotate
  31. EOF
  32. ===================================================================
  33. [root@Squid script]# cat rotate_squid.sh
  34. #!/bin/sh
  35. cd /application/squid/var/logs/
  36. [ -f access.log ] && access.log access_`data +%F`.log
  37. /application/squid/sbin/squid -k rotate
  38. [root@Squid script]#
  39. [root@Squid script]# cat rotate_squid.sh
  40. #!/bin/sh
  41. cd /application/squid/var/logs/
  42. [ -f access.log ] && {
  43. for n in `ls *.log`
  44. do
  45. mv $n $n.`date +%F.%N`
  46. done
  47. }
  48. /application/squid/sbin/squid -k rotate
  49. [root@Squid script]#
  50. ================================================================
 

到这来一个完整的squid代理服务器就安装完成了

###配置管理页面

1.squid有一个cachemgr.cgi 的程序,可以用web来显示内容,这个对调整squid的参数很方便,平时我们安装完squid后,其实就有这个程序了,只有我们在apache下做相应的配置即可

  1. ========================================
  2. ScriptAlias "/squid" "/application/squid3.0/libexec/cachemgr.cgi"
  3. <Location "/squid">
  4. Orderdeny,allow
  5. Denyfrom all
  6. Allowfrom all
  7. </Location>
  8. #cachemgr_passwd lvnian config ##这个如果启动。第一个lvnian是密码,第二个config是行为
  9. ##修改一个端口为8080(可选)
  10. Listen 8080
  11. ========================================
  12. 2.直接打开测试接可以了,默认好像是不用密码和用户的,记住端口即可
  13. http://ip/squid
  14. http://10.0.0.4:8080/squid
 

实战

配置ACL

  1. ##############################
  2. acl lvnian url_regex -i ^http://.*baidu.* ##禁止访问http开头,且保护oldboy字符串的网站
  3. acl lvnian1 url_regex -i ^http://.*51.*| .*taobao.* ##禁止访问http开头,且保护oldboy字符串的网站,或者包含aobao字符串的网站网站
  4. http_access deny lvnian
  5. http_access deny lvnian1
 

提示:注意放置的位置:

  1. [root@Squid etc]# vim squid.conf
  2. acl manager proto cache_object
  3. acl localhost src 127.0.0.1/32
  4. acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
  5. acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
  6. acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
  7. acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
  8. acl SSL_ports port 443
  9. acl Safe_ports port 80 # http
  10. acl Safe_ports port 21 # ftp
  11. acl Safe_ports port 443 # https
  12. acl Safe_ports port 70 # gopher
  13. acl Safe_ports port 210 # wais
  14. acl Safe_ports port 1025-65535 # unregistered ports
  15. acl Safe_ports port 280 # http-mgmt
  16. acl Safe_ports port 488 # gss-http
  17. acl Safe_ports port 591 # filemaker
  18. acl Safe_ports port 777 # multiling http
  19. acl CONNECT method CONNECT
  20. acl Safe_ports port 70 # gopher
  21. acl Safe_ports port 210 # wais
  22. acl Safe_ports port 1025-65535 # unregistered ports
  23. acl Safe_ports port 280 # http-mgmt
  24. acl Safe_ports port 488 # gss-http
  25. acl Safe_ports port 591 # filemaker
  26. acl Safe_ports port 777 # multiling http
  27. acl CONNECT method CONNECT
  28. ###########################
  29. acl lvnian url_regex -i ^http://.*baidu.*
  30. acl lvnian1 url_regex -i ^http://.*51.*| .*taobao.*
  31. http_access deny lvnian
  32. http_access deny lvnian1
  33. ############################
  34. http_access allow manager localhost
  35. http_access deny manager
  36. http_access deny !Safe_ports
  37. http_access deny CONNECT !SSL_ports
  38. http_access allow localnet
  39. http_access deny all
  40. icp_access allow localnet
  41. icp_access deny all
  42. htcp_access allow localnet
  43. htcp_access deny all
  44. http_port 3128
  45. hierarchy_stoplist cgi-bin ?
  46. "squid.conf" 50L, 1932C written
  47. [root@Squid etc]# /etc/init.d/squid restart
  48. Restarting squid...
  49. squid... [ OK ]
  50. squid... [ OK ]
  51. -----------------------check ing-----------------------------
  52. udp 0 0 0.0.0.0:3130 0.0.0.0:* 7112/(squid)
  53. -----------------------check over-----------------------------
  54. [root@Squid etc]#
 

这个时候你就无法访问51cto的网站,百度网站,以及淘宝的网站了。

部署完成

普通代理分为两种:

一种是普通代理服务器:

作为代理服务器,这是Squid的最基本的功能;通过在squid.conf配置文件添加一系列的访问及控制规则,用户在客户端设置服务器地址和端口,即可通过squid访问internet,在下面的规则里,squid实现局域网用户和高速缓存功能:

即通过浏览器设置代理服务器地址共享上网,这种方式不需要代理服务器在网络的出入口的位置,只需要代理服务器能够上网即可,其他的客户端就可以通过IE等客户端设置代理服务器的地址及端口进行上网。上一篇文件就是这样的一个例子。这个技术以及落后了。

另外一种就是透明代理,所谓的透明代理,是相对于代理服务器而已,客户端不需要做任何和代理服务器相关的设置和操作,对用户而言,感觉不到代理服务器的存在,所以称之为透明代理。即把代理服务器部署在核心的上网出口,当用户上网浏览页面时,会交给代理服务器向外请求,如果结合iptables可以实现代理+网关+内容过滤+浏览安全控制等完整的上网解决方案

透明代理流程说明:

用户A发送一个访问请求到防火墙,有防火墙将该用户的访问请求转发给SQUID,SQUID在先检查自身缓存中有无该用户请求的访问的内容,如果没有,则请求远端目的服务器,获取该用户的访问内容,在返回给用户的同事,自身也缓存保留一份记录,以备下次调用,当用户B发送一个和用户A刚才访问的相同请求时,由防火墙将转发该用户请求到SQUID,squid检查自身缓存,发现有同样的内容后,直接将该内容返回给用户,而无需再去向源站点请求。

注意:在实际使用中,通常将squid和防火墙放在同一台服务器上。

透明代理的好处:

上网行为控制

节约网站带宽成本

提升员工上网速度

squid可以为局域网中的客户端做代理,用来加速用户的网页访问,第一次访问的时候,squid会把数据缓存到服务器上,当客户端第二次访问的时候,squid会对比文件的新旧,如果文件没有发送变化,则squid直接返回给用户数据,不需要在互联网上重新下载一份。

squid有没代理物理拓扑说明

作为透明代理服务器,一般和公司的上网网关放在一起,即所以的客户机的网关都设置为代理服务器的IP,具体需求如下

a、至少有两块网卡,一块连接路由器,一块连接内部网络

b、所以的上网请求都不行经过代理服务器(即把代理服务器设置为网关)

下面我部署的透明代理如下:

需要两块网卡

  1. eth0 10.0.0.4
  2. eth1 172.16.1.1
 

如果要实现透明代理,在编译安装squid的时候需要添加下面编译参数

  1. ./configure \
  2. --enable-linux-entfilter
  3. --enable-linux-tproxy
 

##部署方法

#在squid.conf 中的http——port 3128 后面加transparent参数。这个是核心参数,有这个参数就可以了。

  1. grep "http_port 3128" squid.conf
  2. sed -i 's/http_port 3128/http_port 3128 transparent/g' squid.conf
  3. grep "http_port 3128" squid.conf
 

#增加几个参数,这些是调优参数,不是必须的

  1. #cache_mem 128 MB#这是一个优化选项,增加该内存值有利于缓存。应该注意的是: 一般来说如果系统有内存,设置该值为(n/)3M。现在是3G 所以这里1G
  2. #cache_swap_low 90#最小允许使用swap 90%
  3. #cache_swap_high 95#最多允许使用swap 95%
  4. #maximum_object_size 8192 KB#允午最大文件请求体大小
  5. #minimum_object_size 0 KB#允午最小文件请求体大小
  6. #maximum_object_size_in_memory 4096 KB#允许最大的文件载入内存
  7. #emulate_httpd_log on#将使Squid仿照Web服务器的格式创建访问记录。如果希望使用
  8. #memory_replacement_policy lru ##缓存算法,保持最近使用的
  9. ##sed名称操作如下:
  10. sed -i '/http_port 3128/acache_mem 128 MB\ncache_swap_low 90\ncache_swap_high 95' squid.conf
  11. sed -i '/cache_swap_high/a \nmaximum_object_size 8192 KB\nminimum_object_size 0 KB\nmaximum_object_size_in_memory 4096 KB\nemulate_httpd_log on\nmemory_replacement_policy lru' squid.conf
  12. sed -n '/http_port 3128/,+9p' squid.conf
  13. =========================================================
  14. [root@Squid etc]# sed -n '/http_port 3128/,+9p' squid.conf
  15. http_port 3128 transparent
  16. cache_mem 64 MB
  17. cache_swap_low 90
  18. cache_swap_high 95
  19. nmaximum_object_size 8192 KB
  20. minimum_object_size 0 KB
  21. maximum_object_size_in_memory 4096 KB
  22. emulate_httpd_log on
  23. memory_replacement_policy lru
  24. hierarchy_stoplist cgi-bin ?
  25. [root@Squid etc]#
  26. =========================================================
 

##防火墙设置

  1. iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j REDIRECT --to-ports 3128
  2. iptables -t nat -A POSTROUTING -o eth0 -s 172.16.1.0/24 -j MASQUERADE
  3. iptables -t nat -L -nv
  4. ===========================================================
  5. [root@Squid etc]# iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j REDIRECT --to-ports 3128
  6. [root@Squid etc]# iptables -t nat -A POSTROUTING -o eth0 -s 172.16.1.0/24 -j MASQUERADE
  7. [root@Squid etc]# iptables -t nat -L -nv
  8. Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
  9. pkts bytes target prot opt in out source destination
  10. 0 0 REDIRECT tcp -- eth1 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 redir ports 3128
  11. Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
  12. pkts bytes target prot opt in out source destination
  13. 0 0 MASQUERADE all -- * eth0 172.16.1.0/24 0.0.0.0/0
  14. Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
  15. pkts bytes target prot opt in out source destination
  16. [root@Squid etc]#
  17. ===========================================================
 

#开启转发功能

  1. sed -i 's#net.ipv4.ip_forward = 0#net.ipv4.ip_forward = 1#g' /etc/sysctl.conf
  2. sysctl -p
 

####这样就设置成功了

我们在用一台服务器,设置只有内网网卡,网段在172.16.1.0/24 网段。把网关设置为172.16.1.1.用它访问外网。这样就可以了,

一边访问web,一般查看squid的日志

  1. [root@Squid ~]# tailf /application/squid/var/logs/access.log
  2. 1441695622.825 52 172.16.1.100 TCP_MISS/200 97207 GET http://www.baidu.com/ - DIRECT/115.239.211.112 text/html
  3. 1441695625.005 52 172.16.1.100 TCP_MISS/200 97406 GET http://www.baidu.com/ - DIRECT/115.239.210.27 text/html
  4. 1441695709.770 208 172.16.1.100 TCP_MISS/200 727503 GET http://www.163.com/ - DIRECT/114.80.143.158 text/html
 

##TCP_MISS  代表没有缓冲

普通代理方式是代理内部网络用户访问Internet上服务器的连接请求,客户端必须指定代理服务器,并将本来要直接发送到internet上服务器的连接请求发送给代理服务器处理。

反向代理方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外表现问一台服务器。

1、反向代理流程说明:

squid做为反向代理服务器,通常工作在一个服务器集群的前端,在用户端看来,squid服务器就是他所要访问的服务器,而实际意义上squid只是接受用户的请求,同时将用户请求转发给内网真正的web服务器,如果squid本身有用户要访问的内容,则squid直接将内容返回给用户,而无需再去后端的服务器中请求

2、squid反向代理生产案例

很多大型门户网站,经常使用squid作为服务器的反向cache,提高了服务器的访问性能,这些cache服务器组有效减轻了后端web服务器的负载,并且提高了访问速度,在某种程度上保护了后端的web服务器。

3、squid反向代理的优点:

1.节约带宽(对CDN),自己在机房部署squid反向代理,不能节约带宽

2.提升用户体验

3.减轻服务器压力,减少WEB,存储,数据库的压力

公司购买CDN了,企业还需要在机房搭建squid反向代理

答:需求不大,有CDN了,静态业务命中到百分之90左右了,所以需求不大

在穿透CDN请求比较多的时候,超过web服务及其他存储,数据库的压力时候,需要部署squid

4、squid 反向代理如何获取数据更新

squid反向代理一般只缓存可缓存的数据(比如html页面,js,css和图片等静态数据),而一些CGI脚本程序或ASP、JSP、PHP之类的动态程序默认不缓存,它根据从WEB服务器返回的HTTP头标记来缓冲静态页面,有四个重要的HTTP头标记。

Last-Modified:告诉反向代理页面什么时间被修改;

Expries:告诉反向代理页面什么时间应该从缓冲区中删除

Cache-Control:告诉方向代理页面是否应该被缓存;

Pragma:用来包含实现特定的指令,最常见的是Pragma:no-Cache

优先级对比(no-cache,expries,max-age)参考

经验:在squid中Cache-Control:no-cache>expries>refresh_pattern>Last-Modified.考前面的最重要,前面的失效了,后面的基本也就失效了。

另外安装一台web服务器,作为源站。也就是RS

web 服务器ip是 10.0.0.4 端口为80

  1. yum install httpd*
  2. /etc/init.d/iptables stop
  3. /etc/init.d/httpd start
  4. netstat -lnt|grep 80
  5. lsof -i :80
  6. echo 111>/var/www/html/index.html
  7. ====================================================================
  8. [root@WEB html]# ps -ef |grep http
  9. root 1798 1 0 14:47 ? 00:00:00 /usr/sbin/httpd
  10. apache 1800 1798 0 14:47 ? 00:00:00 /usr/sbin/httpd
  11. apache 1801 1798 0 14:47 ? 00:00:00 /usr/sbin/httpd
  12. apache 1802 1798 0 14:47 ? 00:00:00 /usr/sbin/httpd
  13. apache 1803 1798 0 14:47 ? 00:00:00 /usr/sbin/httpd
  14. apache 1804 1798 0 14:47 ? 00:00:00 /usr/sbin/httpd
  15. apache 1805 1798 0 14:47 ? 00:00:00 /usr/sbin/httpd
  16. apache 1806 1798 0 14:47 ? 00:00:00 /usr/sbin/httpd
  17. apache 1807 1798 0 14:47 ? 00:00:00 /usr/sbin/httpd
  18. root 1811 1743 0 14:49 pts/0 00:00:00 grep http
  19. [root@WEB html]# netstat -lntup|grep 80
  20. tcp 0 0 :::80 :::* LISTEN 1798/httpd
  21. [root@WEB html]# lsof -i :80
  22. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  23. httpd 1798 root 4u IPv6 19044 0t0 TCP *:http (LISTEN)
  24. httpd 1800 apache 4u IPv6 19044 0t0 TCP *:http (LISTEN)
  25. httpd 1801 apache 4u IPv6 19044 0t0 TCP *:http (LISTEN)
  26. httpd 1802 apache 4u IPv6 19044 0t0 TCP *:http (LISTEN)
  27. httpd 1803 apache 4u IPv6 19044 0t0 TCP *:http (LISTEN)
  28. httpd 1804 apache 4u IPv6 19044 0t0 TCP *:http (LISTEN)
  29. httpd 1805 apache 4u IPv6 19044 0t0 TCP *:http (LISTEN)
  30. httpd 1806 apache 4u IPv6 19044 0t0 TCP *:http (LISTEN)
  31. httpd 1807 apache 4u IPv6 19044 0t0 TCP *:http (LISTEN)
  32. [root@WEB html]#
  33. [root@WEB html]# curl 10.0.0.4
  34. 111
  35. [root@WEB html]#
  36. ====================================================================
 

在客户端打开检查看看是否正确

上传3张图片到web站点,后缀为jpg的图片

  1. [root@WEB html]# ls
  2. 1.jpg 1.png 2.jpg 3.jpg index.html K2.png keep1.png na1.png na2.png
  3. [root@WEB html]#
  4. http://10.0.0.4/1.jpg
  5. http://10.0.0.4/2.jpg
  6. http://10.0.0.4/3.jpg
 

在squid服务器(IP:10.0.0.3),设置squid.conf

  1. #首先设置squid支持虚拟主机模式
  2. host_port 8000 accel vhost vport ##加速模式
  3.  
  4. #设置squid运行的用户
  5. cache_effective_user squid
  6. cache_effective_group squid
  7. #设置对哪里web做方向代理
  8. cache_peer imag1.lvnian.org parent 80 0 no-query no-digest max-conn=32 originserver
  9. #cache_peer vi.gis.cttic.cn parent 80 0 no-query no-digest max-conn 32 originserve
  10. #设置缓存目录(squid吃没吃文件系统类型、缓存目录、目录大小Mbyte、第一层目录的文件数,第二层文件夹的数目)
  11. cache_dir ufs /app/squid/cache 10000 16 256
  12.  
  13. cache_mem 128 MB
  14. #这是一个优化选项,增加该内存值有利于缓存。应该注意的是: 一般来说如果系统有内存,设置该值为(n/)3M。现在是3G 所以这里1G
  15. cache_swap_low 90 #最小允许使用swap 90%
  16. cache_swap_high 95 #最多允许使用swap 95%
  17. maximum_object_size 8192 KB #设置缓存在硬盘上的文件的最大大小
  18. minimum_object_size 0 KB #设置缓存在硬盘上的文件的最小大小
  19. maximum_object_size_in_memory 4096 KB#设置缓存在内存上的文件的大小
  20. emulate_httpd_log on
  21. #将使Squid仿照Web服务器的格式创建访问记录。如果希望使用
  22. memory_replacement_policy lru #缓存算法,保持最近使用的
  23.  
  24. #设置这些方向代理的ACL,避免squid被恶意用户作为代理
  25. acl my_allow_domain_list dstdomain imag1.lvnian.org
  26. #acl my_allow_domain_list dstdomain vi.gis.cttic.cn
  27. #只允许我们定义的域名my_allow_domain_list
  28. cache_peer_access imag1.lvnian.org vi.gis.cttic.cn
  29.  
  30. #设置缓存过期时间
  31. refresh_pattern -i \.jpg$ 30 50% 4320 reload-into-ims
  32. refresh_pattern -i \.png$ 30 50% 4320 reload-into-ims
  33. refresh_pattern -i \.gif$ 30 50% 4320 reload-into-ims
  34. 上面的意思是:如果某个响应驻留在cache里的时间没有超过这个30分钟最低的限制,那么他不会过期,类似最高限制4320分钟是存活响应的最高时间限制,如果某个响应驻留在你cache里的实际高于这个最高限制,那么它必须被刷新,在最低和最高时间限制之间的响应,会面对squid的最后修改系数(LM-factor)算法,对这样的响应,squid计算响应的年龄和最后修改的系数,然后将他作为百分比值进行比较,如果这个系数超过50%,那么这个响应必须被刷新
 

###################

squid缓存设置--控制页面的缓存时间

refresh_pattern的作用: 用于确定一个页面进入cache后,它在cache中停留的时间。

refresh_pattern 只对后端没设置Expires过期时间的页面起作用,比如论坛页面;而对类似apache mod_expires 设置过的页面不起作用。

语法:

  1. refresh_pattern [-i] regexp min percent max [options
  2. refresh_pattern -i \.css$ 1440 50% 129600 reload-into-ims
  3. refresh_pattern -i \.xml$ 1440 50% 129600 reload-into-ims
  4. refresh_pattern -i \.html$ 1440 90% 129600 reload-into-ims
  5. refresh_pattern -i \.shtml$ 1440 90% 129600 reload-into-ims
  6. refresh_pattern -i \.hml$ 1440 90% 129600 reload-into-ims
  7. refresh_pattern -i \.jpg$ 1440 90% 129600 reload-into-ims
  8. refresh_pattern -i \.png$ 1440 90% 129600 reload-into-ims
  9. refresh_pattern -i \.gif$ 1440 90% 129600 ignore-reload
  10. refresh_pattern -i \.bmp$ 1440 90% 129600 reload-into-ims
  11. refresh_pattern -i \.js$ 1440 90% 129600 reload-into-ims
 

#指定hosts 文件

  1. hosts_file /etc/hosts
  2. request_header_max_size 128 KB
  3. ipcache_size 1024
  4. ipcache_low 90
  5. ipcache_high 95
 

###在hosts中添加相应的解析

  1. cp /etc/hosts /etc/hosts
  2. echo -e "10.0.0.4 imag1.lvnian.org" >>/etc/hosts
 

##只有缓存完了,不管你在不在线,都会有。哪怕你把客户端端IE删除缓存,把后端的http服务关闭,也一样可以看到数据

  1. offline_mode on
 

##定义日志格式

  1. logformat squid %{X-Forwarded-For}>h %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st %tr "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
  2. ##应用日志格式
  3. access_log /application/squid3.0/var/logs/access.log squid
 

###可以配置多个缓存目录

  1. cache_dir aufs /data1/cache 94000 64 256
  2. cache_dir aufs /data2/cache 94000 64 256
  3. cache_dir aufs /data3/cache 94000 64 256
 

完整的配置文件如下:

  1. [root@Squid etc]# cat squid.conf
  2. acl manager proto cache_object
  3. acl localhost src 127.0.0.1/32
  4. acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
  5. acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
  6. acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
  7. acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
  8. acl SSL_ports port 443
  9. acl Safe_ports port 80 # http
  10. acl Safe_ports port 21 # ftp
  11. acl Safe_ports port 443 # https
  12. acl Safe_ports port 70 # gopher
  13. acl Safe_ports port 210 # wais
  14. acl Safe_ports port 1025-65535 # unregistered ports
  15. acl Safe_ports port 280 # http-mgmt
  16. acl Safe_ports port 488 # gss-http
  17. acl Safe_ports port 591 # filemaker
  18. acl Safe_ports port 777 # multiling http
  19. acl CONNECT method CONNECT
  20. http_access allow manager localhost
  21. http_access deny manager
  22. http_access deny !Safe_ports
  23. http_access deny CONNECT !SSL_ports
  24. http_access allow localnet
  25. http_access deny all
  26. icp_access allow localnet
  27. icp_access deny all
  28. htcp_access allow localnet
  29. htcp_access deny all
  30. ###################################
  31. http_port 80 accel vhost vport
  32. #设置对哪里web做方向代理
  33. cache_peer imag1.lvnian.org parent 80 0 no-query no-digest max-conn=32 originserver
  34. cache_mem 64 MB
  35. cache_swap_low 90
  36. cache_swap_high 95
  37. maximum_object_size 8192 KB
  38. minimum_object_size 0 KB
  39. maximum_object_size_in_memory 4096 KB
  40. emulate_httpd_log on
  41. memory_replacement_policy lru
  42. ###################################
  43. hierarchy_stoplist cgi-bin ?
  44. cache_dir ufs /application/squid3.0/var/cache 100 16 256
  45. access_log /application/squid3.0/var/logs/access.log squid
  46. cache_log /application/squid3.0/var/logs/cache.log
  47. cache_store_log /application/squid3.0/var/logs/store.log
  48. refresh_pattern ^ftp: 1440 20% 10080
  49. refresh_pattern ^gopher: 1440 0% 1440
  50. refresh_pattern (cgi-bin|\?) 0 0% 0
  51. refresh_pattern . 0 20% 4320
  52. cache_mgr 75685538@qq.com
  53. cache_effective_user squid
  54. cache_effective_group squid
  55. visible_hostname img01.etiantian.org
  56. icp_port 3130
  57. coredump_dir /application/squid3.0/var/cache
  58. #######################################
  59. refresh_pattern -i \.css$ 1440 50% 129600 reload-into-ims
  60. refresh_pattern -i \.xml$ 1440 50% 129600 reload-into-ims
  61. refresh_pattern -i \.html$ 1440 90% 129600 reload-into-ims
  62. refresh_pattern -i \.shtml$ 1440 90% 129600 reload-into-ims
  63. refresh_pattern -i \.htm$ 1440 90% 129600 reload-into-ims
  64. refresh_pattern -i \.jpg$ 1440 90% 129600 reload-into-ims
  65. refresh_pattern -i \.png$ 1440 90% 129600 reload-into-ims
  66. refresh_pattern -i \.gif$ 1440 90% 129600 ignore-reload
  67. refresh_pattern -i \.bmp$ 1440 90% 129600 reload-into-ims
  68. refresh_pattern -i \.js$ 1440 90% 129600 reload-into-ims
  69. #指定hosts 文件
  70. hosts_file /etc/hosts
  71. request_header_max_size 128 KB
  72. ipcache_size 1024
  73. ipcache_low 90
  74. ipcache_high 95
  75. cache_dir ufs /app/squid/cache 10000 16 256
  76. offline_mode on
  77. [root@Squid etc]#
 

###检查语法

  1. [root@Squid etc]# squid -k parse
  2. 2015/09/08 16:54:32| Processing Configuration File: /application/squid3.0/etc/squid.conf (depth 0)
  3. 2015/09/08 16:54:32| WARNING: use of 'reload-into-ims' in 'refresh_pattern' violates HTTP
  4. 2015/09/08 16:54:32| Initializing https proxy context
  5. [root@Squid etc]#
  6. 上面是没错误的
  7. #启动
  8. [root@Squid etc]# /etc/init.d/squid restart
  9. squid restart... [ OK ]
  10. ---------- -------------check ing-----------------------------
  11. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 55119/(squid)
  12. udp 0 0 0.0.0.0:3130 0.0.0.0:* 55119/(squid)
  13. -----------------------check over-----------------------------
  14. [root@Squid etc]#
 

#检查命中转态

  1. curl -I -s 10.0.0.3/1.jpg|grep -i x-cache
  2. [root@Squid ~]# curl -I -s 10.0.0.4/1.jpg|grep -i x-cache
  3. X-Cache: HIT from img01.lvnian.org
  4. [root@Squid ~]# curl -I -s 10.0.0.4/2.jpg|grep -i x-cache
  5. X-Cache: HIT from img01.lvnian.org
  6. [root@Squid ~]# curl -I -s 10.0.0.4/3.jpg|grep -i x-cache
  7. X-Cache: HIT from img01.lvnian.org
  8. [root@Squid ~]#
 

一边用windows浏览器访问下面地址,看能不能正确访问到10.0.0.4后端RS的内容。一边看10.0.0.3squid的日志文件。观察squid的代理情况

  1. http://10.0.0.3/1.jpg
  2. http://10.0.0.3/2.jpg
  3. http://10.0.0.3/3.jpg
 

学习Squid(三)的更多相关文章

  1. squid 三种代理实验

    squid 软件既可以做代理,也可以做实现缓存加速,大大降低服务器的I/O.. 1.其中squid代理分为三种,正向代理.透明代理.反向代理. (1)squid正向代理和squid透明代理都位客户端: ...

  2. Oracle学习笔记三 SQL命令

    SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)  

  3. 从零开始学习jQuery (三) 管理jQuery包装集

    本系列文章导航 从零开始学习jQuery (三) 管理jQuery包装集 一.摘要 在使用jQuery选择器获取到jQuery包装集后, 我们需要对其进行操作. 本章首先讲解如何动态的创建元素, 接着 ...

  4. 前端学习 第三弹: JavaScript语言的特性与发展

    前端学习 第三弹: JavaScript语言的特性与发展 javascript的缺点 1.没有命名空间,没有多文件的规范,同名函数相互覆盖 导致js的模块化很差 2.标准库很小 3.null和unde ...

  5. Android Animation学习(三) ApiDemos解析:XML动画文件的使用

    Android Animation学习(三) ApiDemos解析:XML动画文件的使用 可以用XML文件来定义Animation. 文件必须有一个唯一的根节点: <set>, <o ...

  6. 三、Android学习第三天——Activity的布局初步介绍(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 三.Android学习第三天——Activity的布局初步介绍 今天总结下 ...

  7. JavaWeb学习总结(三)——Tomcat服务器学习和使用(二) 包含https 非对称秘钥 NB

    JavaWeb学习总结(三)--Tomcat服务器学习和使用(二) 一.打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命 ...

  8. MyEclipse Spring 学习总结三 SpringMVC

    MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...

  9. Quartz定时任务学习(二)web应用/Quartz定时任务学习(三)属性文件和jar

    web中使用Quartz 1.首先在web.xml文件中加入 如下内容(根据自己情况设定) 在web.xml中添加QuartzInitializerServlet,Quartz为能够在web应用中使用 ...

  10. MyBatis学习系列三——结合Spring

    目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring MyBatis在项目中应用一般都要结合Spring,这一章主要把MyBat ...

随机推荐

  1. 开源绘画应用 Pinta 已移植到GTK 3和.NET 6

    Pinta 是一款开源绘画应用,适用于 Linux.Windows 和 macOS.你可以用它来进行自由手绘/素描.你也可以用它在现有的图片上添加箭头.方框.文字等. 年初发布了 Pinta 2.0. ...

  2. 【C#特性】 Attribute 特性

    msdn:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/attributes/creating-c ...

  3. ssh执行命令或者脚本里最好先加上 source /etc/profile

    记: 写一次shell脚本,发现总提示 找不到JAVA_HOME .明明已经配了环境变量,并且java -version也已经能输出信息,但就是提示. 后来在 脚本中加入第一句代码 source /e ...

  4. 基于Lumisoft.NET组件,使用IMAP协议收取邮件

    在早期一直使用Lumisoft.NET组件来进行邮件的处理查找,对于邮件的处理非常方便,之前在随笔<基于Lumisoft.NET组件的POP3邮件接收和删除操作>中也介绍过基于POP3和S ...

  5. SpringBoot入门二:与Mybatis整合

    一.编程步骤 1.引入依赖 springboot相关依赖(略).mybatis-spring-boot-starter.mysql.druid.lombook <dependency> & ...

  6. quartz框架(六)-ThreadPool

    ThreadPool 本篇博文,博主将介绍Quartz框架中ThreadPool线程池相关的内容.线程池顾名思义,就是一个可以帮助我们来进行线程资源管理的对象.在web开发中,常见的就有数据库连接池, ...

  7. linux作业--第九周

    1.简述DNS服务器原理,并搭建主--从服务器. 一.什么是DNS DNS(domain name system)域名系统或者(domain named system)区域名称服务,分为正向与反向域名 ...

  8. JQ,JQuery的ajax卡住了,浏览器页面卡住

    在使用ajax的时候浏览器卡住了, 经过测试是因为在ajax中使用的data数据变量写错了 不存在也不报错,直接卡主了 好好检查一下吧.

  9. Navicat连接不上MySQL 8.0

    Navicat连接不上MySQL 8.0 更改加密方式 1.先通过命令行进入mysql的root账户: C:\Windows\system32> mysql -uroot -p 2.更改加密方式 ...

  10. 面向对象编程(C++篇3)——析构

    目录 1. 概述 2. 详论 2.1. 对象生命周期 2.2. 不一定需要显式析构 2.3. 析构的必要性 3. 总结 1. 概述 类的析构函数执行与构造函数相反的操作,当对象结束其生命周期,程序就会 ...