Squid提供了强大的代理控制机制,通过合理设置ACL(Access Control List,访问控制列表)并进行限制,可以针对源地址、目标地址、访问的URL路径、访问的时间等各种条件进行过滤。
ACL访问控制的步骤:
1、使用acl配置项定义需要控制的条件
2、通过http_access配置项对已定义的列表做“允许”或“拒绝”访问的控制
定义格式:acl 列表名称 列表类型 列表内容 …
例:控制源IP网段,不同网段之间是使用空格分隔的
vim /etc/squid.conf
acl MYLAN src 192.168.10.0/24 192.168.60.0/24
常用的ACL列表类型
src 源地址
dst 目标地址
port 目标地址
dstdomain 目标域
time 访问时间
maxconn 最大并发连接
url_regex 目标URL地址 # 可以定义大的范围比如http://www.baidu.com
urlpath_regex 整个目标URL路径 # 可以定位到每个网站的具体目标的url,比如百度音乐的一首歌的url
vi /etc/squid.conf
acl all src 0.0.0.0/0.0.0.0
http_access deny all
service squid reload
vim /etc/squid.conf
acl all src 0.0.0.0/0.0.0.0 (有些版本要这么写acl all src all)
acl MYLAN src 192.168.1.0/24 192.168.4.0/24
acl WORKTIME time MTWHF 08:30-17:30 (其中MTWHF是周一到周五的英文首字母)
http_access allow MYLAN WORKTIME
http_access deny all
service squid reload
#注意要先写http_access allow MYLAN WORKTIME再写http_access deny all,因为先执行前面的,后面的就不执行了
(1)首先创建地址列表文件(直接在配置文件里写也行,但是这种用列表文件的方式适合拒绝或允许的网站域名比较多的情况,而且方便增删管理)
vim /etc/squid/ipblock.list
61.135.167.36
60.28.14.0/24
vim /etc/squid/dmblock.list
5 .qq.com
(2)配置acl
vim /etc/squid.conf
acl IPBLOCK dst "/etc/squid/ipblock.list"
acl DMBLOCK dstdomain "/etc/squid/dmblock.list"
http_access deny IPBLOCK
http_access deny DMBLOCK
service squid reload
yum install -y gcc gcc-c++
tar zxvf /opt/squid-3.4.6.tar.gz
cd /opt/squid-3.4.6
./configure \
--prefix=/usr/local/squid \
--sysconfdir=/etc \
--enable-arp-acl \
--enable-linux-netfilter \
--enable-linux-tproxy \
--enable-async-io=100 \
--enable-err-language="Simplify_Chinese" \
--enable-underscore \
--enable-poll \
--enable-gnuregex
make && make install
ln -s /usr/local/squid/sbin/* /usr/local/sbin/
useradd -M -s /sbin/nologin squid
chown -R squid:squid /usr/local/squid/var/
vim /etc/squid.conf
#65行添加,指定程序用户
cache_effective_user squid
#66行添加,指定程序用户基本组
cache_effective_group squid
#备份路径67行,不需要添加,默认就有
coredump_dir /usr/local/var/cahce/squid
squid -k parse //检查配置文件的语法
squid -z //初始化缓存目录
squid //启动squid服务
netstat -natp | grep 3128
cd /etc/init.d/
vim squid
#!/bin/bash
#chkconfig: 2345 90 25
PID="/usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"
case "$1" in
start)
netstat -natp | grep squid &> /dev/null
if [ $? -eq 0 ];then
echo "squid is running"
else
echo "please wait a moment"
$CMD
fi
;;
stop)
$CMD -k kill &> /dev/null
rm -rf $PID &> /dev/null
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ];then
netstat -natp | grep squid
else
echo "squid is not running"
fi
;;
restart)
$0 stop &> /dev/null
echo "please wait a moment"
$0 start &> /dev/null
echo "squid is running"
;;
reload)
$CMD -k reconfigure
;;
check)
$CMD -k parse
;;
*)
echo "Usage:$0{start|stop|restart|reload|check|}"
esac
chmod +x /etc/init.d/squid
chkconfig --add squid
chkconfig --level 35 squid on
(1)配置squid传统代理服务
vim /etc/squid.conf
#56行允许代理所有,默认是deny的,要修改为allow
http_access allow all
http_port 3128
#指定缓存功能所使用的内存空间大小
cache_mem 64 MB
#允许用户下载的最大文件的大小
reply_body_max_size 10 MB
#允许保存到缓存空间的最大对象的大小
maximum_object_size 4096 KB
(2)设置防火墙策略
iptables -F
setenforce 0
iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
(3)重启
service squid reload
(4)访问和验证
在Windows浏览器
设置—高级—系统—计算机的代理设置—手动设置代理【开,地址:代理服务器squid的IP地址,端口:3128】—保存
访问192.168.60.20web服务器
tar zxvf sarg-2.3.7.tar.gz
yum install -y gd gd-devel
mkdir /usr/local/sarg
cd /opt/sarg-2.3.7
./configure --prefix=/usr/local/sarg --sysconfdir=/etc/sarg --enable-extraprotection
make && make install
vim /etc/sarg/sarg.conf
7:access_log /usr/local/squid/var/logs/access.log
25:title "Squid User Access Reports"
120:output_dir /var/www/html/squid-reports
178:user_ip no
184:topuser_sort_field connect reverse
206:exclude_hosts /usr/local/sarg/noreport
257:overwrite_report no
289:mail_utility mailq.postfix
434:charset UTF-8
518:weekdays 0-6
525:hours 0-23
633:www_document_root /var/www/html
touch /usr/local/sarg/noreport
ln -s /usr/local/sarg/bin/sarg /usr/local/bin
sarg
yum install -y httpd
systemctl start httpd
sarg -l /usr/local/squid/var/logs/access.log -o /var/www/html/squid-reports -z
-d $(date -d "1 day ago" +%d%m%Y)-$(date +%d%m%Y)
yum install -y gcc gcc-c++
tar zxvf /opt/squid-3.4.6.tar.gz
cd /opt/squid-3.4.6
./configure \
--prefix=/usr/local/squid \
--sysconfdir=/etc \
--enable-arp-acl \
--enable-linux-netfilter \
--enable-linux-tproxy \
--enable-async-io=100 \
--enable-err-language="Simplify_Chinese" \
--enable-underscore \
--enable-poll \
--enable-gnuregex
make && make install
ln -s /usr/local/squid/sbin/* /usr/local/sbin/
useradd -M -s /sbin/nologin squid
chown -R squid:squid /usr/local/squid/var/
vim /etc/squid.conf
#65行添加,指定程序用户
cache_effective_user squid
#66行添加,指定程序用户基本组
cache_effective_group squid
#备份路径67行,不需要添加,默认就有
coredump_dir /usr/local/var/cahce/squid
squid -k parse //检查配置文件的语法
squid -z //初始化缓存目录
squid //启动squid服务
netstat -natp | grep 3128
cd /etc/init.d/
vim squid
#!/bin/bash
#chkconfig: 2345 90 25
PID="/usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"
case "$1" in
start)
netstat -natp | grep squid &> /dev/null
if [ $? -eq 0 ];then
echo "squid is running"
else
echo "please wait a moment"
$CMD
fi
;;
stop)
$CMD -k kill &> /dev/null
rm -rf $PID &> /dev/null
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ];then
netstat -natp | grep squid
else
echo "squid is not running"
fi
;;
restart)
$0 stop &> /dev/null
echo "please wait a moment"
$0 start &> /dev/null
echo "squid is running"
;;
reload)
$CMD -k reconfigure
;;
check)
$CMD -k parse
;;
*)
echo "Usage:$0{start|stop|restart|reload|check|}"
esac
chmod +x /etc/init.d/squid
chkconfig --add squid
chkconfig --level 35 squid on
(1)配置squid传统代理服务
vim /etc/squid.conf
#56行允许代理所有,默认是deny的,要修改为allow
http_access allow all
http_port 3128
#指定缓存功能所使用的内存空间大小
cache_mem 64 MB
#允许用户下载的最大文件的大小
reply_body_max_size 10 MB
#允许保存到缓存空间的最大对象的大小
maximum_object_size 4096 KB
(2)设置防火墙策略
iptables -F
setenforce 0
iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
(3)重启
service squid reload
vim /etc/squid.conf
http_port 192.168.60.30:80 accel vhost vport
cache_peer 192.168.60.20 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
cache_peer 192.168.60.10 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
#cache_peer_domain web1 web2 www.yun.com
squid
分析:很明显这是代理服务器出现问题,根据查看应该是配置文件中出现了差错
【解决方案】
进入配置文件,删除域名访问即可
vim /etc/squid.conf
http_port 192.168.60.30:80 accel vhost vport
cache_peer 192.168.60.20 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
cache_peer 192.168.60.10 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2