1、简单介绍: 我们之前提到过Nginx在做反向代理服务器时,本身就具有缓存功能的,只不过它的缓存,是缓存在文件当中的, 而内存当中缓存的只是文件描述符,源文件(所谓的文件属性),所以Nginx本身在提供 缓存方面的能力就不甚理想,(后端Application Server的执行结果).那么因此就有专门的服务器,提供缓存的.squid这是一款非常古老的缓存服务器,它既能够实现正向代理,又能实现反向代理,并且还能够作为缓存服务器进行使用.并且还能够使用ACL.squid非常古老,在上个世纪90年代,squid绝对是一统天下的.市面上几乎百分之90的站点在实现网站加速务的时候都是 Squid来做反向代理.Squid能够代理的协议有:HTTP FTP GOPHER, SSL.

   缺点:但是由于squid本身功能灼眼点过多,在某一个特征方面优化不是特别好.没有什么优点.

 

   Squid实现正向代理:如下图所示:需要客户端在浏览器上添上代理服务器的IP:Port,设置代理服务器

   如果公司内网用户请求http://www.nginx.com这个网站,在squid做正向代理这个应用场景下,客户端其实请求的是Squid,Squid服务器,如果客户端请求的内容,在squid服务器本地有缓存的话就直接响应客户端,如果本地没有缓存的话,就以自己的身份,通过自己的公网IP,去请求www.Nginx.com服务器,并将请求当中可以缓存的内容缓存下来,然后再返回客户端.

squid 缓存服务器详解_能力

      正向代理(透明代理) Squid在做透明代理的时候,与正向代理不同的是客户端不需要再浏览器上指代理服务器IP及其端口,不过由于Squid透明代理服务器监听在一个很奇怪的端口上(3128),默认不是80,所以需要,结合iptables去实现,反向代理,当客户端请求服务器的时候,请求的是80端口,当数据包到达prerouting这条链上,IPtables就将客户端请求的80端口改为Squid服务器能够接收请求的端口(3128).

squid 缓存服务器详解_代理服务器_02

      正向代理squid兄弟服务器基于ICP协议缓存内容共享,ICP(UDP),客户端请求www.nginx.com 如果squid1没有,就向squid2请求,squid(兄弟服务器),如果都没有就向squid父服务器去请求,以此类推.

squid 缓存服务器详解_浏览器_03

           Squid在做反向代理的时候其实跟nginx差不多.能够定义权重,负载均衡,timeout,实现的机制都是如此.squid和nginx本身都没有检测后端web服务器健康状态的能.


          squid 安装 http://www.squid-cache.org  squid 官网,有需要的话自己去download Source packages 安装,这里我们不做源码包安装的介绍,所以;你懂的;

# yum -y install squid
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
base                                                                                                                                            | 3.2 kB     00:00    
epel                                                                                                                                            | 2.9 kB     00:00    
extra                                                                                                                                           | 2.9 kB     00:00    
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package squid.x86_64 7:3.1.10-19.el6_4 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================================================================================
 Package                              Arch                                  Version                                          Repository                           Size
=======================================================================================================================================================================
Installing:
 squid                                x86_64                                7:3.1.10-19.el6_4                                base                                1.7 M
Transaction Summary
=======================================================================================================================================================================
Install       1 Package(s)
Total download size: 1.7 M
Installed size: 5.8 M
Downloading Packages:
squid-3.1.10-19.el6_4.x86_64.rpm                                                                                                                | 1.7 MB     00:00    
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : 7:squid-3.1.10-19.el6_4.x86_64                                                                                                                      1/1
  Verifying  : 7:squid-3.1.10-19.el6_4.x86_64                                                                                                                      1/1
Installed:
  squid.x86_64 7:3.1.10-19.el6_4                                                                                                                                      
Complete!

# cd /etc/squid/
#ls
cachemgr.conf          errorpage.css          mime.conf          msntauth.conf          squid.conf
cachemgr.conf.default  errorpage.css.default  mime.conf.default  msntauth.conf.default  squid.conf.default

                             

    squid 默认不对其配置的话它就是一个正向代理服务器,只要ACL规则没有限定;

Squid 配置文件详解
# NETWORK OPTIONS(网络选项)
# -----------------------------------------------------------------------------
http_port 3128  #代理端口
icp_port 3130   #icp端口
# OPTIONS WHICH AFFECT THE NEIGHBOR SELECTION ALGORITHM
#-----------------------------------------------------------------------------
#禁止缓存
hierarchy_stoplist cgi-bin ?
hierarchy_stoplist -i ^https:\\ ?
acl QUERY urlpath_regex -i cgi-bin \? \.asp \.php \.jsp \.cgi
acl denyssl urlpath_regex -i ^https:\\
no_cache deny QUERY
no_cache deny denyssl
#上面几个就是说遇到URL中有包含cgi-bin和以https:\\开头的都不要缓存,
#asp、cgi、php等动态脚本也不要缓存,
#https://开通的不缓存是因为一般我们进行电子商务交易,
#例如银行付款等都是采用这个的,如果把信用卡号什么缓存那不是很危险。
# OPTIONS WHICH AFFECT THE CACHE SIZE(定义cache大小的选项)
# -----------------------------------------------------------------------------
cache_mem 32 MB       #额外使用内存量,可根据你的系统内存在设定,一般为实际内存的1/3
cache_swap_low  70    #最低缓存百分比
cache_swap_high 95   #最高缓存百分比,就是上面那个额外内存的使用百分比
maximum_object_size 4096 KB  #单个文件最大缓存大小,超过这个大小将不缓存
maximum_object_size_in_memory 8 KB  #在内存中单个文件最大缓存大小,超过这个大小将不缓存到内存中
#有DNS正反解所得到的IP存在缓存区的大小,这样可以加快解析速度
ipcache_size 1024
ipcache_low 90
ipcache_high 95
fqdncache_size 1024
# LOGFILE PATHNAMES AND CACHE DIRECTORIES(定义日志文件的路径及cache的目录)
# -----------------------------------------------------------------------------
# 各发行版自带的Squid略有区别,一般使用各发行版自带的设置
#  ; ; 目录所在>; 大小>; ; ;
#  那个 aufs 只有在编译的时候加入 --enable-async-io 那个选项才有支持,
#  至于目录所在地与所占用的磁盘大小则请视您的主机情况而定,
#  而后面 dir1, dir2 则是两个次目录的大小,通常 16 256 或 64 64 皆可,
#  一般来说,数字最好是 16 的倍数,据说性能会比较好啦!
cache_dir aufs /Cache1 100 16 256 
cache_dir aufs /Cache2 100 16 256
#日志存放位置
#cache_access_log /usr/local/squid/var/logs/access.log
#cache_log /usr/local/squid/var/logs/cache.log
#  TAG: cache_store_log
#cache_store_log /usr/local/squid/var/logs/store.log
#  TAG: pid_filename
#pid_filename /usr/local/squid/var/logs/squid.pid
# OPTIONS FOR EXTERNAL SUPPORT PROGRAMS(外部支持程序选项)
# -----------------------------------------------------------------------------
#用代理登陆匿名ftp服务选项
# 各发行版自带的Squid略有区别,一般使用各发行版自带的设置
#  TAG: ftp_user
ftp_user Squid@    #用户名
ftp_passive on     #被动模式
#认证
#auth_param basic children 5
#auth_param basic realm Squid proxy-caching web server
#auth_param basic credentialsttl 2 hours
#auth_param basic casesensitive off
# OPTIONS FOR TUNING THE CACHE(调整cache的选项)
# -----------------------------------------------------------------------------
#  TAG: refresh_pattern    Cache更新时间设置
#; ; 最小时间>; 百分比>; 最大时间>;
refresh_pattern ^ftp:           1440        20%        10080
refresh_pattern ^gopher:        1440        0%        1440
refresh_pattern .                0        20%        4320
#上面第一行如果网址开头是 ftp 的话,那么在一天(1440分钟)后,
#如果proxy 再次取用这个档案时,则 cache 内的数据会被更新!
# TIMEOUTS (超时)
# -----------------------------------------------------------------------------
#连接到其他机器的最大尝试时间
connect_timeout 1 minute
#连接到上层代理的超时时间
peer_connect_timeout 30 seconds
#返回超时
request_timeout 2 minutes
#持续连接时间
persistent_request_timeout 1 minute
# ACCESS CONTROLS(访问控制)
# -----------------------------------------------------------------------------
#  TAG: acl
#Examples:
#acl myexample dst_as 1241
#acl password proxy_auth REQUIRED
#acl fileupload req_mime_type -i ^multipart/form-data$
#acl javascript rep_mime_type -i ^application/x-javascript$
#
#Recommended minimum configuration:
acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443 563
acl Safe_ports port 80                # http
acl Safe_ports port 21                # ftp
acl Safe_ports port 443 563        # https, snews
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
acl CONNECT method CONNECT
acl msn url_regex -i ^
http://gateway.messenger.hotmail.com
acl inside1 src 192.168.1.0/24   #内部网IP段
acl inside2 src 192.168.2.0/24
acl localmac arp "/usr/local/squid/localmac"  #mac地址文件
#  TAG: http_access
http_access allow inside1  #允许inside1规则通过
http_access allow inside2  #允许inside2规则通过
http_access allow localmac  #允许localmac里面有登记的mac地址通过
http_access allow msn  #允许访问
[/url]
[url=http://gateway.messenger.hotmail.com/]http://gateway.messenger.hotmail.com
acl admin arp 00:40:05:13:C4:B2
http_access allow admin  #允许00:40:05:13:C4:B2这个mac地址
#
#Recommended minimum configuration:
#
# Only allow cachemgr access from localhost
http_access allow manager localhost #允许manager访问localhost
http_access deny manager            #禁止manager访问
# Deny requests to unknown ports
http_access deny !Safe_ports        #禁止访问不在Safe_ports里的端口
# Deny CONNECT to other than SSL ports
http_access deny CONNECT !SSL_ports #禁止访问非443,563端口
#
acl banned_sites url_regex "/etc/squid/banned.list" 
acl worktime time MTWHF 8:30-12:00 14:00-18:00
acl mmxfile urlpath_regex -i \.mp3$ \.avi$ \.rmvb$ \.rm$ \.wma$ \.wmv$
http_access deny worktime mmxfile #禁止在worktime时间内访问.mp3,.avi,.rmvb,.rm,.wma文件
http_access deny worktime banned_sites  #banned.list文件里的网址全部丢弃
http_access allow localhost             #localhost可以访问
#------按照网段,限制连接数
acl loc1 src 192.168.1.0/24          
acl loc1_conn maxconn 50
acl loc2 src 192.168.2.0/24
acl loc2_conn maxconn 30
http_access deny loc1 loc1_conn       
http_access allow loc1
http_access deny loc2 src loc2_conn
http_access allow loc2
#-----------------------------
http_access allow localhost     #localhost可以访问
http_access deny all            #丢弃其他
# HTTPD-ACCELERATOR OPTIONS(HTTPD加速选项)
# -----------------------------------------------------------------------------
#设定透明代理
httpd_accel_host virtual #主机名
httpd_accel_port 80      #透明代理端口
httpd_accel_with_proxy on
httpd_accel_uses_host_header on