您的位置:首页 > 运维架构 > 反向代理

Squid介绍及普通代理完整部署

2015-11-10 11:34 555 查看
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的编译和安装

部署前环境准备
环境 :
squid server           IP:10.0.0.4
WEB server             IP:10.0.0.3
1.更改yum源:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo yum install tree -y
grep keepcache /etc/yum.conf
sed -i 's/keepcache=0/keepcache=1/g' /etc/yum.conf
grep keepcache /etc/yum.conf
2.关闭selinux:
setenforce 0         #临时生效
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config  #永久生效
3.关闭防火墙
/etc/init.d/iptables stop
chkconfig iptables off
chkconfig --list iptables
4.时间同步
/usr/sbin/ntpdate pool.ntp.org
echo '#time sync by gao at 2010-2-1'>>/var/spool/cron/root
echo '*/10 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1'>>/var/spool/cron/root
crontab -l
5.修改主机名
=======================================
hostname Squid
cp /etc/hosts /etc/hosts.bak
cp /etc/sysconfig/network /etc/sysconfig/network.bak
sed -i '$a 10.0.0.4 WEB' /etc/hosts
sed -i '$a 10.0.0.3 Squid' /etc/hosts
sed -i -e '/HOSTNAME=/d' -e '1aHOSTNAME=WEB' /etc/sysconfig/network
logout
=======================================
hostname WEB
cp /etc/hosts /etc/hosts.bak
cp /etc/sysconfig/network /etc/sysconfig/network.bak
sed -i '$a 10.0.0.4    WEB' /etc/hosts
sed -i '$a 10.0.0.3 Squid' /etc/hosts
sed -i -e '/HOSTNAME=/d' -e '1aHOSTNAME=Squid' /etc/sysconfig/network
logout
=======================================
6.调整文件描述符
ulimit -Hn 20480
echo "ulimit -Hn 20480" >>/etc/rc.local
tail -1 /etc/rc.local
ulimit -n
echo "*   -  nofile   20480">>/etc/security/limits.conf
tail -2 /etc/security/limits.conf
7.调整临时端口
cat /proc/sys/net/ipv4/ip_local_port_range
echo "net.ipv4.ip_local_port_range = 4000   65000">>/etc/sysctl.conf
sysctl -p|grep port_range
###记得一定要做时间同步

##开始编译安装Squid
在10.0.0.3操作
创建放置软件的目录
mkdir -p /home/lvnian/tools
cd /home/lvnian/tools
下载并安装squid
#wget http://www.it.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE20.tar.gz #wget http://down1.chinaunix.net/distfiles/squid-3.0.STABLE20.tar.gz #http://www.it.squid-cache.org/Versions/v3/3.0
wget http://www.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE20.tar.gz tar xf squid-3.0.STABLE20.tar.gz
cd squid-3.0.STABLE20
./configure --prefix=/application/squid3.0 \
--enable-asyno-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-cachemgr-hostname=localhost \
--enable-arp-acl \
--enable-default-err-language=English \
--enable-err-language="Simplify_Chinese" \
--disable-poll \
--disable-wccp \
--disable-ident-lookups \
--disable-internal-dns \
--enable-basic-auth-helpers="NCSA" \
--enable-stacktrace \
--with-filedescriptors=64000 \
--enable-ssl \
--enable-x-accelerator-very \
--disable-snmp \
--with-aio \
--enable-linux-netfilter \
--enable-linux-tproxy
==========================================================
make && make install
##############################################
make 报错解决
structs.h:738: error: ISO C++ forbids declaration of ‘SSL_CTX’ with no type
structs.h:738: error: expected ‘;’ before ‘*’ token
structs.h:1160: error: ISO C++ forbids declaration of ‘SSL_CTX’ with no type
structs.h:1160: error: expected ‘;’ before ‘*’ token
structs.h:1161: error: ISO C++ forbids declaration of ‘SSL_SESSION’ with no type
structs.h:1161: error: expected ‘;’ before ‘*’ token
make[1]: *** [cf_gen.o] Error 1
make[1]: Leaving directory `/home/lvnian/tools/squid-3.0.STABLE20/src'
make: *** [all-recursive] Error 1
[root@Squid squid-3.0.STABLE20]#
安装openssl*
yum install openssl* -y
cd /home/lvnian/tools
rm -rf squid-3.0.STABLE20
tar xf squid-3.0.STABLE20.tar.gz
cd squid-3.0.STABLE20
##############################################


#制作软连接
ln -s /application/squid3.0 /application/squid
到这来安装squid完毕:

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


squid.conf 语法介绍
[root@Squid etc]# pwd
/application/squid/etc
[root@Squid etc]# ls
cachemgr.conf  cachemgr.conf.default  mime.conf  mime.conf.default  squid.conf  squid.conf.default
[root@Squid etc]#
上面中以default为后缀的都是对于配置文件的备份

#看去掉注销和空行之后的配置文件
[root@Squid etc]# cat squid.conf.default|egrep -v '^#|^$'
acl manager proto cache_object
acl localhost src 127.0.0.1/32
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
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 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
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 localnet
http_access deny all
icp_access allow localnet
icp_access deny all
htcp_access allow localnet
htcp_access deny all
http_port 3128
hierarchy_stoplist cgi-bin ?
access_log /application/squid3.0/var/logs/access.log squid
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern (cgi-bin|\?)    0       0%      0
refresh_pattern .               0       20%     4320
icp_port 3130
coredump_dir /application/squid3.0/var/cache
[root@Squid etc]#
#创建加用户
useradd squid -s /sbin/nologin -M
id squid


#调整配置文件
vi /etc/squid.conf
#############################################
修改下面的用户为squid,注意不能是root,root无法执行
2941 # cache_effective_user nobody
2941   cache_effective_user squid
或者用sed修改
grep "cache_effective_user " squid.conf
sed -i "s/# cache_effective_user nobody/cache_effective_user squid/g" squid.conf
grep "cache_effective_user " squid.conf
============================================


#添加组
sed -i "/cache_effective_user squid/a cache_effective_group squid" squid.conf
grep "cache_effective" squid.conf
============================================
[root@Squid etc]# grep "cache_effective" squid.conf
#  TAG: cache_effective_user
#       see also; cache_effective_group
cache_effective_user squid
cache_effective_group squid
#  TAG: cache_effective_group
[root@Squid etc]#
============================================
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的一个方法,以便这些消息能保存下来
local4.warning/var/log/squid.log
在维护多个squid主机时,使用syslog来记录cache.log特别方便,可以配置每个本机syslog进程,转发这些消息到中央日志主机,这样就可以在一个地方统一浏览所有cache日志,例如。可以在/etc/sysconfigd.conf 里使用如下接口
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规则。

如:
acl mynet src 10.0.0.0/24
htcp_access allow mynet

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

##访问日志,保存默认,修改为如下
access_log /application/squid3.0/var/logs/access.log squid
##修改存储日志打开
============================================
sed -i 's/# cache_store_log/cache_store_log/g' squid.conf
grep "cache_store" squid.conf
============================================
[root@Squid etc]# grep "cache_store" squid.conf
#  TAG: cache_store_log
cache_store_log /application/squid3.0/var/logs/store.log
============================================


#打开缓存log
============================================
grep cache_log squid.conf
sed -i 's/# cache_log/cache_log/g' squid.conf
grep cache_log squid.conf
============================================
[root@Squid etc]# grep cache_log squid.conf
#  TAG: cache_log
cache_log /application/squid3.0/var/logs/cache.log
============================================


#####修改缓存目录,也可以配置多个
grep cache_dir squid.conf
sed -i 's%# cache_dir ufs /application/squid3.0/var/cach%cache_dir ufs /application/squid3.0/var/cach%g' squid.conf
grep cache_dir squid.conf
============================================
[root@Squid etc]# grep cache_dir squid.conf
#       Applies to any cache_dir lines listed below this.
#  TAG: cache_dir
#       cache_dir Type Directory-Name Fs-specific-data [options]
#       You can specify multiple cache_dir lines to spread the
#       cache_dir ufs Directory-Name Mbytes L1 L2 [options]
#       cache_dir aufs Directory-Name Mbytes L1 L2 [options]
#       cache_dir diskd Directory-Name Mbytes L1 L2 [options] [Q1=n] [Q2=n]
#       block-size=n defines the "block size" for COSS cache_dir's.
#       leads to a maximum cache_dir size of 512<<24, or 8 GB.  Note
#       has written some objects to the cache_dir.
#       no-store, no new objects should be stored to this cache_dir
#       the cache_dir lines with the smallest max-size value first and the
cache_dir ufs /application/squid3.0/var/cache 100 16 256
#       'cache_dir' directory, but you may specify an alternate
#       a representation of the cache_dir name where each / is replaced
#       with '.'. This is needed to allow adding/removing cache_dir
#       If have more than one 'cache_dir', and %s is not used in the name
#       corresponds to the order of the 'cache_dir' lines in this
#       configuration file.  If you change the order of the 'cache_dir'
#       the correct 'cache_dir' entry (unless you manually rename
#       better to keep these index files in each 'cache_dir' directory.
[root@Squid etc]#
============================================


##编辑可见主机名,如果不配置,可能无法启动squid
sed -n '/visible_hostname/p' squid.conf
sed -i '/#  TAG: visible_hostname/avisible_hostname img01.etiantian.org ' squid.conf
sed -n '/visible_hostname/p' squid.conf
============================================
[root@Squid etc]# sed -n '/visible_hostname/p' squid.conf
#  TAG: visible_hostname
visible_hostname img01.etiantian.org
#       'visible_hostname' you must give each machine a different
[root@Squid etc]#
============================================


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

sed -n '/cache_mgr /p' squid.conf
sed -i 's/# cache_mgr webmaster/cache_mgr 75685538@qq.com/g' squid.conf
sed -n '/cache_mgr /p' squid.conf
============================================
[root@Squid etc]#  sed -n '/cache_mgr /p' squid.conf
cache_mgr 75685538@qq.com
[root@Squid etc]#
============================================


对比修改哪些内容:
##############################################################################
##############################################################################
[root@Squid etc]# diff squid.conf squid.conf.default
1710c1710
< cache_dir ufs /application/squid3.0/var/cache 100 16 256
---
> # cache_dir ufs /application/squid3.0/var/cache 100 16 256
1889c1889
< cache_log /application/squid3.0/var/logs/cache.log
---
> # cache_log /application/squid3.0/var/logs/cache.log
1899c1899
< cache_store_log /application/squid3.0/var/logs/store.log
---
> # cache_store_log /application/squid3.0/var/logs/store.log
2912c2912
< cache_mgr 75685538@qq.com
---
> # cache_mgr webmaster
2941,2942c2941
< cache_effective_user squid
< cache_effective_group squid
---
> # cache_effective_user nobody
2971d2969
< visible_hostname img01.etiantian.org
[root@Squid etc]#
##############################################################################
修改后的配置文件
[root@Squid etc]# egrep -v '^#|^$' squid.conf
acl manager proto cache_object
acl localhost src 127.0.0.1/32
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
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 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
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 localnet
http_access deny all
icp_access allow localnet
icp_access deny all
htcp_access allow localnet
htcp_access deny all
http_port 3128
hierarchy_stoplist cgi-bin ?
cache_dir ufs /application/squid3.0/var/cache 100 16 256
access_log /application/squid3.0/var/logs/access.log squid
cache_log /application/squid3.0/var/logs/cache.log
cache_store_log /application/squid3.0/var/logs/store.log
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern (cgi-bin|\?)    0       0%      0
refresh_pattern .               0       20%     4320
cache_mgr 75685538@qq.com
cache_effective_user squid
cache_effective_group squid
visible_hostname img01.etiantian.org
icp_port 3130
coredump_dir /application/squid3.0/var/cache
[root@Squid etc]#
##############################################################################

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


简单命令说明:

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


#检查配置文件语法

/application/squid/sbin/squid -k parse
chown -R squid.squid /application/squid3.0/var/logs/
/application/squid/sbin/squid -k parse
=====================================================================
[root@Squid etc]# /application/squid/sbin/squid -k parse
2015/09/08 11:23:45| Processing Configuration File: /application/squid3.0/etc/squid.conf (depth 0)
2015/09/08 11:23:45| Initializing https proxy context
WARNING: Cannot write log file: /application/squid3.0/var/logs/cache.log
/application/squid3.0/var/logs/cache.log: Permission denied  《=============语法问题。这个文件没有出现
messages will be sent to 'stderr'.
[root@Squid etc]# chown -R squid.squid /application/squid3.0/var/logs/
[root@Squid etc]# /application/squid/sbin/squid -k parse
2015/09/08 11:24:49| Processing Configuration File: /application/squid3.0/etc/squid.conf (depth 0)
2015/09/08 11:24:49| Initializing https proxy context
[root@Squid etc]#
=====================================================================
###设置环境变量
echo 'export PATH=$PATH:/application/squid/sbin:=/application/squid/bin '>>/etc/profile
. /etc/profile
echo $PATH


###初始化squid

grep cache_dir squid.conf
ll /application/squid3.0/var/cache
ll /application/squid3.0/var/
squid -z /application/squid3.0/var/
chown -R squid.squid /application/squid3.0/var
squid -z /application/squid3.0/var/
tree /application/squid3.0/var/ |wc -l

=====================================================================
[root@Squid etc]# grep cache_dir squid.conf
#       Applies to any cache_dir lines listed below this.
#  TAG: cache_dir
#       cache_dir Type Directory-Name Fs-specific-data [options]
#       You can specify multiple cache_dir lines to spread the
#       cache_dir ufs Directory-Name Mbytes L1 L2 [options]
#       cache_dir aufs Directory-Name Mbytes L1 L2 [options]
#       cache_dir diskd Directory-Name Mbytes L1 L2 [options] [Q1=n] [Q2=n]
#       block-size=n defines the "block size" for COSS cache_dir's.
#       leads to a maximum cache_dir size of 512<<24, or 8 GB.  Note
#       has written some objects to the cache_dir.
#       no-store, no new objects should be stored to this cache_dir
#       the cache_dir lines with the smallest max-size value first and the
cache_dir ufs /application/squid3.0/var/cache 100 16 256
#       'cache_dir' directory, but you may specify an alternate
#       a representation of the cache_dir name where each / is replaced
#       with '.'. This is needed to allow adding/removing cache_dir
#       If have more than one 'cache_dir', and %s is not used in the name
#       corresponds to the order of the 'cache_dir' lines in this
#       configuration file.  If you change the order of the 'cache_dir'
#       the correct 'cache_dir' entry (unless you manually rename
#       better to keep these index files in each 'cache_dir' directory.
[root@Squid etc]#  ll /application/squid3.0/var/cache
ls: cannot access /application/squid3.0/var/cache: No such file or directory
[root@Squid etc]#
[root@Squid etc]#
[root@Squid etc]# ll /application/squid3.0/var/
total 4
drwxr-xr-x. 2 squid squid 4096 Nov 10 00:15 logs
[root@Squid etc]# squid -z /application/squid3.0/var/
2015/11/10 10:09:45| Creating Swap Directories
FATAL: Failed to make swap directory /application/squid3.0/var/cache: (13) Permission denied
[root@Squid etc]# chown -R squid.squid /application/squid3.0/var
[root@Squid etc]# squid -z /application/squid3.0/var/
2015/11/10 10:10:09| Creating Swap Directories
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/00
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/01
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/02
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/03
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/04
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/05
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/06
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/07
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/08
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/09
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0A
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0B
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0C
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0D
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0E
2015/11/10 10:10:09| Making directories in /application/squid3.0/var/cache/0F
[root@Squid etc]# tree /application/squid3.0/var/ |wc -l
4118
[root@Squid etc]#
=====================================================================


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

#上面的是普通代理
用法:
打开IE浏览器>工具>连接>局域网设置>为LAN使用代理服务器
把squid服务器ip和3128端口填上去即可
之后重新打开IE,浏览网页。

一边看日志,一般通过IE浏览,会看到访问日志不停滚动
tailf /application/squid/var/logs/access.log

看服务器日志
=================
[root@Squid ~]#  tailf /application/squid/var/logs/access.log
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
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
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
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
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
1447122193.337  30012 10.0.0.1 TCP_MISS/200 2878 CONNECT g.alicdn.com:443 - DIRECT/222.73.134.40 -
1447122193.493  30011 10.0.0.1 TCP_MISS/200 2878 CONNECT img.alicdn.com:443 - DIRECT/101.226.178.140 -
[root@Squid ~]# tree /application/squid3.0/var/cache/|wc -l
4714
[root@Squid ~]#
[root@Squid ~]# tree /application/squid3.0/var/cache/|wc -l
4847
[root@Squid ~]#
##################################前面的就是普通代理模式
#######设置开机自启动
/application/squid/sbin/squid -D       ##-D 参数指后台运行
echo "start squid">>/etc/rc.local
echo "/application/squid/sbin/squid -D">>/etc/rc.local
tail -2 /etc/rc.local
######################################################


设置启动脚本
#!/bin/sh
################################################
#this scripts is created by gao at 20150908
#gao lvnian
################################################
#!/bin/bash #
# chkconfig: 345 89 24
# description: squid is a web cache server
# processname: squid
. /etc/rc.d/init.d/functions
function_start_squid()
{
/application/squid/sbin/squid -D
if [ $? == "0" ];then
action " $1 squid..." /bin/true
sleep 3
echo "-----------------------check ing-----------------------------"
netstat -lntup|egrep '31|squid'
echo "-----------------------check over-----------------------------"
else
action " $1 squid..." /bin/failed
echo "please check the log"
fi
}
#stop function
function_stop_squid()
{
/application/squid/sbin/squid  -k shutdown
if [ $? == "0" ];then
action "$1 squid..." /bin/true
else
action "$1 squid..." /bin/failed
echo "please check the log"
fi
}
#restart function
function_restart_squid()
{
printf "Restarting squid...\n"
function_stop_squid
sleep 2
function_start_squid
}
case $1 in
start)
function_start_squid
;;
stop)
function_stop_squid
;;
restart)
function_restart_squid
;;
*)
printf "Usage: $0 {start|stop|restart}\n"
esac
========================================================
把这个文件放到/etc/init.d/目录下并命名为squid
chmod +x /etc/init.d/squid
chkconfig --add squid
========================================================
[root@Squid ~]# chmod +x /etc/init.d/squid
[root@Squid ~]# chkconfig --add squid
[root@Squid ~]# chkconfig --list squid
squid           0:off   1:off   2:off   3:on    4:on    5:on    6:off
[root@Squid ~]#


##设置日志轮询
/application/squid/sbin/squid -k rotate
查看日志
[root@Squid ~]# ll /application/squid/var/logs/
total 276
-rw-r----- 1 squid squid  99547 Sep  8 13:05 access.log
-rw-r----- 1 squid squid  28301 Sep  8 13:05 cache.log
-rw-r--r-- 1 root  squid      6 Sep  8 13:03 squid.pid
-rw-r----- 1 squid squid 134582 Sep  8 13:05 store.log
[root@Squid ~]# /application/squid/sbin/squid -k rotate
[root@Squid ~]# ll /application/squid/var/logs/
total 284
-rw-r----- 1 squid squid      0 Sep  8 13:08 access.log
-rw-r----- 1 squid squid 100020 Sep  8 13:08 access.log.0
-rw-r----- 1 squid squid    458 Sep  8 13:08 cache.log
-rw-r----- 1 squid squid  28301 Sep  8 13:05 cache.log.0
-rw-r--r-- 1 root  squid      6 Sep  8 13:03 squid.pid
-rw-r----- 1 squid squid      0 Sep  8 13:08 store.log
-rw-r----- 1 squid squid 135372 Sep  8 13:08 store.log.0
[root@Squid ~]#
#######设置切换时名称改为时间类型
脚本
echo "0 0 * * * /bin/sh /server/script/rotate_squid.sh" >>/var/spool/cron/root
crontab -l
####################
mkdir -p /server/script/
cat > /server/script/rotate_squid.sh <EOF
#!/bin/sh
cd /application/squid/var/logs/
[ -f access.log ] && mv access.log access_`data +%F`.log
/application/squid/sbin/squid -k rotate
EOF
===================================================================
[root@Squid script]# cat rotate_squid.sh
#!/bin/sh
cd /application/squid/var/logs/
[ -f access.log ] && access.log access_`data +%F`.log
/application/squid/sbin/squid -k rotate
[root@Squid script]#
[root@Squid script]# cat rotate_squid.sh
#!/bin/sh
cd /application/squid/var/logs/
[ -f access.log ] && {
for n in `ls *.log`
do
mv $n $n.`date +%F.%N`
done
}
/application/squid/sbin/squid -k rotate
[root@Squid script]#
================================================================
到这来一个完整的squid代理服务器就安装完成了

###配置管理页面
1.squid有一个cachemgr.cgi 的程序,可以用web来显示内容,这个对调整squid的参数很方便,平时我们安装完squid后,其实就有这个程序了,只有我们在apache下做相应的配置即可
========================================
ScriptAlias "/squid" "/application/squid3.0/libexec/cachemgr.cgi"
<Location "/squid">
Orderdeny,allow
Denyfrom all
Allowfrom all
</Location>
#cachemgr_passwd lvnian config        ##这个如果启动。第一个lvnian是密码,第二个config是行为
##修改一个端口为8080(可选)
Listen 8080
========================================
2.直接打开测试接可以了,默认好像是不用密码和用户的,记住端口即可 http://ip/squid http://10.0.0.4:8080/squid


实战
配置ACL
##############################
acl lvnian url_regex -i ^http://.*baidu.*     ##禁止访问http开头,且保护oldboy字符串的网站
acl lvnian1 url_regex -i ^http://.*51.*| .*taobao.*  ##禁止访问http开头,且保护oldboy字符串的网站,或者包含aobao字符串的网站网站
http_access deny lvnian
http_access deny lvnian1
提示:注意放置的位置:
[root@Squid etc]# vim squid.conf
acl manager proto cache_object
acl localhost src 127.0.0.1/32
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
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 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
acl CONNECT method CONNECT
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 lvnian url_regex -i ^http://.*baidu.*
acl lvnian1 url_regex -i ^http://.*51.*| .*taobao.*
http_access deny lvnian
http_access deny lvnian1
############################
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localnet
http_access deny all
icp_access allow localnet
icp_access deny all
htcp_access allow localnet
htcp_access deny all
http_port 3128
hierarchy_stoplist cgi-bin ?
"squid.conf" 50L, 1932C written
[root@Squid etc]# /etc/init.d/squid restart
Restarting squid...
squid...                                                  [  OK  ]
squid...                                                 [  OK  ]
-----------------------check ing-----------------------------
udp        0      0 0.0.0.0:3130                0.0.0.0:*                               7112/(squid)
-----------------------check over-----------------------------
[root@Squid etc]#
这个时候你就无法访问51cto的网站,百度网站,以及淘宝的网站了。

部署完成

本文出自 “奋斗吧” 博客,请务必保留此出处http://lvnian.blog.51cto.com/7155281/1711349
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: