缓存加速----Squid ACL 列表与日志

缓存加速----Squid ACL 列表与日志

1.解压squid软件包
tar xzvf squid-3.4.6.tar.gz  -C /opt/
2.安装相关软件包
yum install gcc gcc-c++ make -y 
3.编辑编译安装
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
4.优化服务控制方式
ln -s /usr/local/squid/sbin/squid /usr/local/sbin
vim /etc/init.d/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 "正在启动 squid...."
        echo "成功启动"
        $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 "正在关闭 squid..."
           echo "关闭成功"
          $0 start &> /dev/null
           echo "正在启动 squid..."
           echo "启动成功"
          ;;
reload)
          $CMD -k reconfigure
          ;;
check)
          $CMD -k parse
        ;;
        *)
            echo "用法:$0{start|stop|status|reload|check|restart}"
        ;;
esac
 
chmod +x /etc/init.d/squid
chkconfig --add squid
5.添加squid的账户,修改squid文件的属性
useradd -M -s /sbin/nologin squid
chown -R squid.squid /usr/local/squid/var/
6.编辑squid的主配置文件
vim /etc/squid.conf
 
57 # And finally deny all other access to this proxy
 58 http_access allow all
 59 http_access deny all
 60 
 61 # Squid normally listens to port 3128
 62 http_port 192.168.10.1:3128 transparent
 63 cache_mem 64 MB
 64 reply_body_max_size 10 MB
 65 maximum_object_size 4096 KB
 66 cache_effective_user squid
 67 cache_effective_group squid
 68 
 69 # Uncomment and adjust the following to add a disk cache directory.
 
7.开启squid服务
service squid start
 
8.添加网卡,修改网卡信息
ens33 NAT模式
ens37 仅主机模式
cd /etc/sysconfig/network-scripts/
cp -p ifcfg-ens33 ifcfg-ens37
vim ifcfg-ens37
 
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens37"
DEVICE="ens37"
ONBOOT="yes"
IPADDR="192.168.10.1"
PREFIX="24"
IPV6_PRIVACY="no"
~                   
9.重启网卡
service network restart
 
10.开启路由转发功能
vim /etc/sysctl.conf
net.ipv4.ip_forward=1
 
sysctl -p   //加载转发功能
 
11.添加防火墙规则
iptables -F
iptables -t nat -F
iptables -t nat -I PREROUTING -i ens37 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
iptables -t nat -I PREROUTING -i ens37 -s 192.168.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128 
iptables -I INPUT -p tcp --dport 3128 -j ACCEPT


acl localhost src 192.168.10.1/32    //把源地址加入acl列表中,且命名为localhost
acl MyLan src 192.168.10.0/24    //将源地址网段添加到acl列表中,命名为MyLan
acl MaxC20 maxconn 20    //规定最大通过代理的连接数为20个,命名为MaxC20
acl BURL url_regex -i ^rtsp:// ^emule://    //基于协议,正则表达式
acl PURL urlpath_regex -i \.mp3$ \.mp4$ \.rmvb$      //基于文件末尾的文件类型,正则表达式
acl WorkTime MTWHF 08:30-17:30    //规定时间,周一到周五的8点半到17点半

//基于某个地址
[root@squid ~]# vim /etc/squid.conf
 
8 acl ip_deny src 192.168.10.10/32
...
33 http_access deny ip_deny        //拒绝源地址访问
[root@squid ~]# service squid restart
正在关闭 squid...
关闭成功
正在启动 squid...
启动成功
[root@squid ~]# 


//基于文件
[root@squid squid]# vim dest.list

192.168.100.150
192.168.100.160
~
[root@squid squid]# vim /etc/squid.conf
acl destionhost dst "/etc/squid/dest.list"
......            
http_access deny destionhost
[root@squid ~]# service squid restart
----------------------------------------------日志管理-------------------
1.安装必要软件包
yum install -y gd gd-devel
2.解压软件包
tar xzvf sarg-2.3.7.tar.gz -C /opt
3.安装功能模块,编译安装
mkdir /usr/local/sarg   //创建安装目录
cd /opt/sarg-2.3.7
./configure \
--prefix=/usr/local/sarg \
--sysconfdir=/etc/sarg \             //配置文件存放位置
--enable-extraprotection             //额外的安全防护
make && make install 
4.编辑sarg的配置文件
[root@squid ~]# vim /etc/sarg/sarg.conf    //开启以下功能
access_log /usr/local/squid/var/logs/access.log //指定访问日志文件
title "Squid User Access Reports"    //网页标题
output_dir /var/www/html/squid-reports  //输出网页的目录
user_ip no   //是否使用用户名显示
topuser_sort_field connect reverse   //top排序中有连接次数、访问字节、降序排列 降序为reverse,升序为normal
exclude_hosts /usr/local/sarg/noreport    //不计入排序的站点列表文件,需要创建
overwrite_report no    //不覆盖同名日志
mail_utility mailq.postfix    //发送邮件报告,命令
charset UTF-8    //开启字符集
weekdays 0-6    //按星期周期排序
hours 0-23    //按小时周期排序
www_document_root /var/www/html    //网页根目录
5.添加不计入站点文件,添加的域名将不被显示在排序中
touch /usr/local/sarg/noreport
6.创建软链接,优化服务控制方式
ln -s /usr/local/sarg/bin/sarg /usr/local/bin
 
7.安装http服务
yum install httpd -y
8.开启日志分析服务
[root@squid ~]# sarg
SARG: 纪录在文件: 17, reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/squid-reports/2020Feb03-2020Feb04
[root@squid ~]# 
 
9.开启httpd服务,关闭防火墙
systemctl stop firewalld
setenforce 0
systemctl start httpd
netstat -natp | grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      1631/httpd  

#计划性任务的sarg的日志分析
sarg命令常用选项:
-l 指定输入日志来源
-o 指定输出目录
-z 处理信息
-d 指定日期
#编写sarg日志分割脚本
[root@squid ~]# cd /usr/local/sarg/bin/
[root@squid bin]# ls
sarg
[root@squid bin]# vim sarg.sh
#!/bin/bash
# Get curent 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 /var/log/squid/access.log -o /var/www/html/squid-reports -z -d $YESTERDAY-$TODAY &>/dev/null
exit 0

[root@squid bin]# ls
sarg  sarg.sh
[root@squid bin]# chmod +x sarg.sh 
[root@squid bin]# ls
sarg  sarg.sh
[root@squid bin]# crontab -e
[root@squid bin]# crontab -l
0 0 * * * /usr/local/sarg/bin/sarg.sh
[root@squid bin]# 

[root@squid squid-reports]# 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)

####################反向代理
//配置web服务器
'//原本的web服务器修改一下主页内容用来和新添加的web2服务器区分'
[root@web ~]# cd /var/www/html
[root@web html]# vim index.html	'//修改首页内容'
this is erbao web!
'//web2服务器设置'
[root@web2 ~]# yum install httpd -y	'//安装httpd服务'
[root@web2 ~]# vim /var/www/html/index.html	'//修改首页内容'
this is sanbao web!
systemctl stop firewalld.service	
setenforce 0
systemctl start httpd


//Squid
[root@squid squid-reports]# systemctl stop httpd	'//关闭httpd服务,因为会占用80端口,后面设置代理需要用到80端口'
[root@squid squid-reports]# systemctl start firewalld.service 
[root@squid squid-reports]# iptables -F


//配置反向代理
[root@squid squid-reports]# vim /etc/squid.conf

vim /etc/squid.conf
55 # And finally deny all other access to this proxy
 56 http_access allow all
 57 http_access deny all        //允许所有用户访问
 58 
 59 # Squid normally listens to port 3128
 60 http_port 192.168.100.140:80 accel vhost vport   //定义虚拟主机和虚拟端口
    cache_peer 192.168.100.140 parent 80 no_query originserver round-robin max_conn=30 weight=1 name=web1        //编辑节点服务器
    cache_peer 192.168.100.150 parent 80 no_query originserver round-robin max_conn=30 weight=1 name=web2
    cache_peer_domain web1 web2 www.yun.com    //用域名调用节点服务器别名
 61 cache_mem 64 MB  //指定缓存功能所使用的内存空间大小
 62 reply_body_max_size 10 MB    //允许用户下载的最大文件大小
 63 maximum_object_size 4096 KB    //允许保存到缓存空间的最大对象大小
 64 cache_effective_user squid    //添加指定程序用户
 65 cache_effective_group squid    //添加指定账户基本组
 66 
 67 # Uncomment and adjust the following to add a disk cache directory.
 68 #cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256