Squid作为应用层的代理服务器,主要提供缓存加速,应用层过虑控制,提供用私有IP访问互联网,提供网络的安全性,节省公司服务器的带宽资源。
工作原理:
当客户机通过代理来请求web页面是,制定的代理服务器会首先查看自己的缓存,如果缓存中已经有客户端之前访问过的缓存,那么会直接将缓存中的页面给反馈给客户机,如果缓存中没有客户机需要的页面,代理服务器则会向Internet发送访问请求,获得返回的Web页面后将页面保存到缓存中并发送给客户机。HTTP代理的缓存加速对象主要是文件,图像等静态Web元素,使用缓存机制后,当客户机在不同的时候访问同一Web元素,或者不同的客户访问相同的Web元素时,可以直接从代理服务器的缓存中获取结果,这样就大大减少了向Internet重复提交Web请求的过程,提高客户机的Web访问的响应速度。
由于客户机的web访问请求实际上时由代理服务器来代理完成的。从而可以隐藏用户的真是IP地址,起到一定的保护作用,另一方面,代理服务器担任这类似“经纪人”的角色,所以有机会针对要访问的目标,客户机的地址,访问的时间段等进行过滤控制。
代理的类型:
①:传统代理:需要在客户机软件上手动指定代理服务器的IP地址和端口号,才可以使用代理上网;对于网页浏览器,访问网站时的域名解析请求也会发送给指定的代理服务器。
②:透明代理:提供的服务和功能和传统代理相同,区别就是客户机不需要指定代理服务器IP和端口,而是通过默认路由,防火墙策略将web网页重定向实际内容仍交给代理服务器来处理,重定向这个过程对于客户机是透明的,客户机并不知道自己使用的代理服务器,透明代理当浏览器访问网站时的域名解析请求优先转发给DNS服务器。
编译安装Squid代理服务器
①.上传squid源码包到/usr/src目录并解压
[root@squid /]# cd /usr/src/ 切换到/usr/src/目录
[root@squid src]# tar zxvf squid-3.5.23.tar.gz 解压squid源码包
②.安装依赖包,切换到squid解压后目录,配置安装的功能
[root@squid /]# yum -y install gcc* gd 安装依赖软件包
[root@squid /]# cd /usr/src/squid-3.5.23/ 切换到squid目录
&nsbp;
[root@squid squid-3.5.23]# ./configure --prefix=/usr/local/squid --sysconfdir=/etc --enable-linux-netfilter --enable-async-io=240 --enable-default-err-language=Simplify_Chinese --disable-poll --enable-epoll --enable-gnuregex ##配置suqid安装的功能,各配置项功能如下:
--prefix=/usr/local/squid:指定squid安装的目录
--sysconfdir=/etc:将squid配置文件修改到其他目录
--enable-linux-netfilter:使用内核过滤功能
--enable-async-io=240:异步I/O提升存储性能
--enable-default-err-language=Simplify_Chinese:错误信息显示的语言类型
--disable-poll:关闭默认使用的poll模式,开启epoll模式提升性能
--enable-gnuregex:使用GUN正则表达式
三:编译并编译安装squid
[root@squid /]#make && make install
④:安装完最后的配置
[root@squid squid-3.5.23]# useradd -M -s /sbin/nologin squid 创建squid运行用户
[root@squid squid-3.5.23]# ln -s /usr/local/squid/sbin/* /usr/local/bin/ 优化squid执行路径
[root@squid /]# chown -R squid.squid /usr/local/squid/ 修改squid的目录属性
五:编辑/etc/squid.conf编辑配置文件添加一下两行
[root@squid squid]# vim /etc/squid.conf ##编辑squid主配置文件
-------------------添加以下两行--------------------
cache_effective_user squid ##初始化和运行时用的用户
cache_effective_group squid ##初始化和运行时用的组账号
注意:如果这一步做完初始化缓存空间提示报错
[root@squid /]# 2019/10/30 23:53:28 kid1| Set Current Directory to /usr/local/squid/var/cache/squid
2019/10/30 23:53:28 kid1| Creating missing swap directories
2019/10/30 23:53:28 kid1| No cache_dir stores are configured.
解决办法:把注释文件中这一行的注释取消掉
#cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256
⑥.初始化缓存空间
[root@squid /]# squid -z 初始化缓存空间
⑦.启动squid服务的语法
[root@squid squid]#squid -z 初始化缓存空间
[root@squid squid]# squid –s ##后台启动squid服务
[root@squid squid]# squid ##启动squid服务
[root@squid squid]# squid -k parse ##检查squid配置文件语法
[root@squid squid]# squid -k reconfigure ##重新加载squid配置文件
[root@squid squid]# squid -k shutdown ##停止squid服务
⑧.为了使squid服务的启动,停止,重载等操作更加方便,可以编写squid服务脚本,并使用chkconfig和server工具来进行管理。
#!/bin/bash
#chkconfig: 2345 90 25
#config: /etc/squid.conf
#pidfile: /usr/local/squid/var/run/squid.pid
#description: squid - internet object cache.
PID="usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"
case "$1" in
start)
netstat -anpt | grep squid &>/dev/null
if [ $? -eq 0 ]
then
echo "squid is running"
else
echo "正在启动squid…….."
$CMD
fi
;;
stop)
$CMD -k kill &> /dev/null
rm -rf $PID &> /dev/null
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
netstat -anpt | grep squid
else
echo "squid is not running"
fi
;;
restart)
$0 stop &> /dev/null
echo "正在关闭squid……"
$0 start &> /dev/null
echo "正在启动squid……"
;;
reload)
$CMD -k reconfigure
;;
check)
$CMD -k parse
;;
*)
echo "用法:$0 {start | stop |restart | reload | check | status}"
;;
esac
将Squid添加为系统服务
[root@squid init.d]# chmod +x /etc/init.d/squid
[root@squid init.d]# chkconfig --add squid
[root@squid init.d]# chkconfig squid on
Squid配置文件
配置文件路径:/etc/squid.conf
##定义访问控制列表
acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7 # RFC 4193 local private network range
acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines
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列表内容控制
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localhost manager
http_access deny manager
http_access allow localnet
http_access allow localhost
http_access deny all
##全局配置参数
cache_effective_user squid ##squid初始化和运行时的用户账号,初始化必加
cache_effective_group squid ##squid初始化和运行时的组账号,初始化必加
http_port 3128 ##squid服务监听的端口和地址;ip:端口
cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256 ##缓存文件夹,默认在内容中缓存,这里指定缓存大小为100M,第一层子目录为16个,第二层为256
coredump_dir /usr/local/squid/var/cache/squid
cache_mem 64M ##内存缓冲区的大小
dns_nameservers 8.8.8.8 ##指定dns地址,一般不设置用服务器本地的dns
cache_log /var/log/squid/cache.log ##缓存日志文件的保存路径
cache_access_log /var/log/squid/access.log ##访问日志文件的保存路径
visible_hostname nnv5.cn ##设置Squid服务器的名称
reply_body_max_size 100 MB ##设置用户下载文件大小限制
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern . 0 20% 4320
配置文件详解:
http_port 3128 ##squid服务监听的端口和地址;ip:端口
cache_effective_user squid ##squid初始化和运行时的用户账号
cache_effective_group squid ##squid初始化和运行时的组账号
cache_mem 64M ##内存缓冲区的大小
cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256 ##硬盘缓冲区的大小
dns_nameservers 8.8.8.8 ##指定dns地址,一般不设置用服务器本地的dns
cache_log /var/log/squid/cache.log ##缓存日志文件的保存路径
cache_access_log /var/log/squid/access.log 访问日志文件的保存路径
visible_hostname ityunn ##设置Squid服务器的名称
传统代理配置:
传统代理只需要将服务安装成功然后启动服务,客户机指定代理服务的地址为squid服务器的ip地址即可
①:修改squid.conf配置文件
[root@squid /]# vim /etc/squid.conf
-------------------添加以下两行--------------------
reply_body_maax_size 10 MB 允许下载的最大文件大小(10MB)
http_access allow all 放在http_access deny all
②:重载squid服务
[root@squid /]# squid -k parse
[root@squid /]# squid -k reconfigure
三:添加iptables规则
[root@squid /]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
④:客户机使用浏览器指定代理服务器的IP和端口,进行验证。
透明代理配置案例
环境介绍:
Squid服务器 | eth0 桥接\eth1 nat | 172.16.10.10 | 192.168.10.101 |
---|---|---|---|
互联网Web服务器 | eth0 桥接 | 172.16.10.20 | |
测试客户机 | eth0 nat | 192.168.10.102 |
Web服务器操作:
①:配置Web服务器IP地址,将网卡改为桥接模式,关闭防火墙和selinux。
②:安装Httpd服务,并启动httpd服务。
Squid服务器操作:
①:添加网卡,将eth0网卡改为桥接模式,eth1网卡模式改为nat模式
②:更改网卡IP地址,eth0改为172.16.10.10,eth1改为192.168.10.101
3:开启内核路由转发功能 (如果配置iptable或者firewald的转发规则可以省略)
④:安装Squid,并启动Squid服务程序
5.配置透明模式
[root@squid /]# vim /etc/squid.conf
-----------------------修改以下内容------------------------
http_port 172.16.88.200:3128 transparent ##添加内网ip,支持透明模式
6.启动Iptables防火墙,设置规则
[root@squid /]# iptables -t nat -I PREROUTING -i eth1 -s 192.168.10.102 -p tcp --dport=80 -j REDIRECT --to 3128 将来自源地址是192.168.10.102访问80的数据转发给3128进行处理。
客户端操作
①:将网卡模式改为桥接,IP改为192.168.10.102,网关为192.168.10.101
②:打开IE浏览器访问172.16.10.20查看是否访问成功
3:查看Httpd访问日志是否有Squid服务器访问请求,查看Squid访问日志
透明代理二:
如果代理服务器没有搭建web网站,squid可以直接使用80端口,就不需要配置防火墙的nat规则了
[root@Squid]# vim /etc/squid.conf ##编辑squid配置文件
-----------------------------添加以下内容------------------------
http_port 80 transparent
-----------------------------或者添加以下内容------------------------
http_port 80 accel vhost allow-direct
注意事项:
开启防火墙后,一旦有端口发生变化,需要在防火墙上面开放指定的端口,传统代理可以不设置网关,只需要在浏览器里面设置IE代理服务器即可,透明代理需要关闭ID代理服务器,配置网关即可。
ACL访问控制
ACL可以针对源地址,目标地址,访问的url路径,访问的时间等各种条件过虑,在配置文件中;其中使用acl配置项定义需要控制的条件,其二就是通过httpd_access配置项对已经定义的列表做允许或者拒绝访问的控制。
访问控制注意事项:
- 每一条access规则中可以同时包含多个访问控制列表名,每一个列表之间使用空格分割,为"与"的关系。必须满足所有访问控制列表对应的条件才会进行限制,需要使用取反条件时,在访问控制列表前添加!符号。
- Squid会安装各条规则的排序进行检查,一旦匹配到相应规则则不继续向下去匹配,所以规则的排放顺序时非常重要的。
- 没有设置任何规则时,Squid服务会拒绝客户端的请求
- 有规则但找不到相应的匹配项,squid会采用与最后一条规则相反的动作,即如果最后一条规则是allow,就拒绝客户端的请求,否则就允许该请求。
定义acl语法:
acl列表名称 列表类型 列表内容
注意:列表名称可以随意定义,列表类型必须使用squid预定义的类型,列表内容是要控制的具体对象,不同类型的列表对应的内容也不一样,可以有多个值,(要以空格分割,为或的关系)
常用访问控制类型
列表类型 | 列表内容 | 含义/用途 |
---|---|---|
src | 192.168.10.101 192.168.10.0/24 192.168.10.0-192.168.30.0/24 | 源IP地址,网段,IP地址范围 |
dst | 192.168.10.101 192.168.10.0/24 www.ityunn.com | 目标IP地址,网段,主机名 |
port | 80 443 9000 3128 20 21 53 3306 1521 | 目标端口 |
dstdomain | .ityunn.com | 目标域,匹配域内的所有站点 |
time | MTWHF 8:30-17:30 12:00-13:00 AS | 使用代理服务器的时间段,字母表示一星期中各天的英文缩写 |
maxconn | 20 | 每一个客户机的并发连接数 |
url_regex | url_regex -i ^http:// url_regex - ^https:// | 目标资源的URL地址,-i表示忽略大小写 |
urlpath_regex | urlpath_regex -i sex adult urlpath_regex -i .mp4$ | 目标资源的整个URL路径,-i表示忽略大小写 |
常用访问控制实例:
1.禁用局域网内ip 192.168.10.101访问Internet
acl client_101 src 192.168.10.101
http_access deny client_101
2.禁用局域网ip网段192.168.10.0/24访问Internet
acl client_0/24 src 192.168.10.0/24
http_access deny client_0/24
3.只允许访问baidu.com域名
acl scopedomain dstdomain –i .baidu.com
http_access allow scopedomain
4.允许访问.com .cn .org .net
acl scopedomain domain –i .com .cn .net .org
http_access allow scopedomain
5.禁止客户端下载包含.exe .mp3 .avi 的文件类型
acl badfiletype urlpath_regex .mp3 .exe .avi
http_access deny badfiletype
6.只允许客户端192.168.10.101最大并发链接数为3个
acl client_101 src 192.168.10.101
acl maxconnect maxconn 3
http_access deny client_101 maxconnect
如果要设置的列表内容较多的时候可以使用调用文件的方式来进行限制
实例:
vim /etc/squid/denyip.list
-----------------------添加以下内容------------------------
192.168.10.102
192.168.10.0/24
acl denyIP src “/etc/squid/denyip.list”
http_access deny denyIP
/etc/squid/denyip.list文件中内容自己建立,一行一个IP
完整实例:
Acl aaa src 172.16.16.1/255.255.255.255 ##源ip地址为具体ip
Acl aaa src 172.16.0.0/16 192.168.1.0/24 ##源ip地址为某个网段
Acl aaa dst 192.168.10.0/24 ##目录地址为某个网段
Acl aaa maxconn 20 ##最大并发连接数
Acl aaa url_regex -i ^rtsp:// ^emule:// ##以rtsp emule指定url开头的网址
Acl aaa urlpath_regex -i .mp3$ .mp4 .avi ##以.mp3 .mp4 .avi结尾的路径
Acl aaa time MTWHF 08:30-17:30 ##时间为周一至周五的8:30-17:30
Squid日志分析
1.安装GD库
[root@squid /]# yum -y install gd*
2.安装sarg (squid服务器也要安装Web服务)
[root@squid /]# cd /usr/src/
[root@squid src]# cd sarg-2.3.7/
[root@squid sarg-2.3.7]# ./configure --prefix=/usr/local/sarg --sysconfdir=/etc/sarg --enable-extraprotection && make && make install
3.配置
[root@squid sarg]# cd /etc/sarg/
里面要改的东西较多,可以删掉sarg.conf,在创建一个将下面的代码复制进去即可
[root@squid sarg]# grep -v “^#” /etc/sarg/sarg.conf | grep -v “^$”
access_log /usr/local/squid/var/logs/access.log
title "Squid User Access Reports"
output_dir /var/www/html/sarg
user_ip no
topuser_sort_field connect reverse
user_sort_field connect reverse
exclude_hosts /usr/local/sarg/noreport
overwrite_report no
mail_utility mailq.postfix
charset UTF-8
weekdays 0-6
hours 9-12,14-16,18-20
www_document_root /var/www/html
4.运行
[root@squid /]# touch /usr/local/sarg/noreport \该文件中添加的域名将不被显示在排序中
[root@squid /]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/
用客户端访问一次网站
[root@squid /]# sarg
SARG: 纪录在文件: 50, reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/sarg/2019Oct31-2019Oct31
5.验证
多次访问web,并多次在squid上执行sarg
在客户端访问http://192.168.10.101/sarg
6.计划任务
vim /usr/local/sarg/daily.sh
#!/bin/bash
#Get current date
TODAY=$(date +%d/%m/%Y)
#Get one week ago today
YESTERDAY=$(DATE -d "1 day ago" +%d/%m/%Y)
/usr/local/sarg/bin/sarg -l /usr/local/squid/var/logs/access.log -o /var/www/html/sarg -z -d $YESTERDAY-$TODAY &> /dev/null
exit 0
chmod +x /usr/local/sarg/daily.sh
crontab -e
20 10 * * * /usr/local/sarg/daily.sh
chkconfig crond on