一,安装squlid部署环境:
-
安装底层编译环境
yum -y install gcc gcc-c++ make -
将软件包拖到/opt/目录,
解压:
tar zxf squid-3.4.6.tar.gz //解压到当前目录下就行了
编译安装:
cd squid-3.4.6/ //到软件目录下
./configure --prefix=/usr/local/squid --sysconfdir=/etc --enable-arp-acl --enable-linux-netfilter --enable-async-io=100 --enable-err-language="Simplify_Chinese" --enable-underscore --enable-poll --enable-gnuregex
make && make install //编译有一会时间
编译安装的解释:
1.指定安装路径;
2.配置文件存放目录;
3.启用acl访问控制列表;通过客户端的MAC地址进行管理,防止客户使用arp欺骗。
4.内核过滤机制
5.支持透明模式功能
6.调用I/O线程优化
7.报错提示简体中文提示
8.url支持下划线
9.字符设备去驱动中的函数,文件指针挂载到当前等待队列
10.支持正则表达式
3.将软件squid命令能够被系统命令能识别
//查看squid启动命令是在那个目录,是/bin 还是/sbin/
//在/sbin/下面,放到系统可以使用的/sbin/下,或者/bin
ln -s /usr//local/squid/sbin/squid /usr/local/sbin/
4.创建一个程序账户;
useradd -M -s /sbin/nologin squid
//更改属账户权限, var目录下有两个目录run,cahe当中都有squid的目录
chown -R squid.squid /usr/local/squid/var
5.更改配置文件
vim /etc/squid.conf
56 #http_access deny all
57 http_access allow all //设置为允许所有
60 http_port 3128
61 cache_effective_user squid //添加主账户
62 cache_effective_group squid //添加组账户
68 coredump_dir /usr/local/squid/var/cache/squid //在第一个缓存目录中保留coredump,容灾目录,出现问题故障可以通过目录还原;
6.检查配置文件语法:
[root@as squid]#squid -k parse
.....省略部分.....
正确的话会显示很快
[root@as squid]# squid -z
[root@as squid]# 2020/10/30 12:19:23 kid1| Set Current Directory to /usr/local/squid/var/cache/squid
2020/10/30 12:19:23 kid1| Creating missing swap directories
2020/10/30 12:19:23 kid1| No cache_dir stores are configured.
^C
[root@as squid]# squid
[root@as squid]# netstat -antp|grep squid
tcp6 0 0 :::3128 :::* LISTEN 55371/(squid-1)
7.配置squid启动脚本;
vim /etc/init.d/squid
#!/bin/bash
#chkconfig: 2345 90 25
PID="/usr/local/squid/var/run/squid.pid" ##PID文件进程号
CONF="/etc/squid.conf" ##主配置文件
CMD="/usr/local/squid/sbin/squid" ##启动命令
case "$1" in
start)
netstat -ntap | grep squid &> /dev/null
if [ $? -eq 0 ]
then
echo "squid is running"
else
echo "正在启动 squid...."
$CMD
echo "启动完成!"
fi
;;
stop)
$CMD -k kill &> /dev/null ##关闭squid
rm -rf $PID &> /dev/null ##删除PID文件
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
netstat -ntap | 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|reload|status|check|restart}"
;;
esac
验证脚本:
[root@as init.d]# chmod +x /etc/init.d/squid //脚本执行权限
[root@as init.d]# chkconfig --add /etc/init.d/squid //添加到启动项
[root@as init.d]# service squid stop
[root@as init.d]# netstat -antp|grep squid
[root@as init.d]# service squid start
正在启动 squid....
启动完成!
[root@as init.d]# netstat -antp|grep squid
tcp6 0 0 :::3128 :::* LISTEN 56348/(squid-1)
二,传统代理
手动指定代理服务器; 指定代理服务器;
- 更改squid配置,添加优化
[root@squid init.d]# vim /etc/squid.conf
http_port 3128
cache_effective_user squid
cache_effective_group squid
63 cache_mem 64 MB ###自定义缓存空间大小,容量最好为4的倍数'
64 reply_body_max_size 10 MB ###允许下载最大文件大小,以字节为单位,默认设置0表示不进行限制'
65 maximum_object_size 4096 KB ###允许保存到缓存空间的最大对象的大小,以KB为单位,超过限制不会缓存,直接转到用户端的缓存'
[root@squid ~]# service squid reload ###重载服务'
[root@squid ~]# netstat -antp |grep squid
tcp6 0 0 :::3128 :::* LISTEN 56799/(squid-1)
- 防火墙设置
[root@squid ~]# iptables -F ###清空防火墙表内容’
[root@squid ~]# iptables -L ###查看防火墙表内容’
[root@squid ~]# setenforce 0 ###关闭防火墙增强型功能’
[root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT ###新增规则,允许3128端口’
- (真机或win10虚拟机)浏览器验证:
使用浏览器不同,配置代理服务地址也不同;
设置-----网络----选择手动配置代理ip
三,透明代理
关闭传统代理的,客户端代理ip
- 添加一个网卡ens36 做代理网关;
识别不到新添加网卡:systemctl start networkmanager 网络管理服务;
cd /etc/sysconfig/network-scripts/
cp ifcfg-ens33 ifcfg-ens36
vim ifcfg-ens36
//文件之前的ens33改成ens36
//uuid删除去掉
//ip地址为192.168.100.1 网关地址
//重启下网络服务
systemctl restart network
-------------
- 做路由转发
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@squid network-scripts]# sysctl -p
net.ipv4.ip_forward = 1
- 更改squid配置文件,改为透明代理模式
vim /etc/squid.conf
60 http_port 192.168.100.1:3128 transparent
//地址是新建网卡的网关地址;
-
重启服务;
service squid restart -
防火墙策略设置:
iptables -t nat -I PREROUTING -i ens36 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3128 //定义入口规则 网卡名称 源地址 协议 http80端口重定向到3128端口
iptables -t nat -I PREROUTING -i ens36 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to 3128 //定义入口规则 网卡名称 源地址 协议 https443端口重定向到3128
iptables -I INPUT -p tcp --dport 3218 -j ACCEPT //新增规则,允许3128端口
- web节点、
1.做静态路由,指向squid代理服务器地址;
[root@web httpd]# route add -net 192.168.100.0/24 gw 20.0.0.16 //添加路由到路由表
[root@web httpd]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 20.0.0.2 0.0.0.0 UG 100 0 0 ens33
20.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.100.0 20.0.0.16 255.255.255.0 UG 0 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
- 客户端验证:
将之前的网卡更换,与squid代理服务网关网卡一致
更改为192.168.100.10地址,网关指向代理服务网关192.168.100.1
四,ACL访问控制
- 修改squid配置文件
vim /etc/squid.conf
acl mygw src 192.168.100.0/24 //访问列表控制原地址某个网段;
acl localip src 192.168.100.10/32 //访问列表控制具体地址‘,固定ip地址是32位
acl desthost dst 20.0.0.17/32 //访问列表控制目标地址
acl mc20 maxconn 20 //访问数量,最大访问
acl burl url_regex -i ^rtsp:// ^cmule:// //正则表达式,以什么协议为开头
acl purl urlpath_regex -i \.mp3$ \.mp4$ \.rmvb$ //以什么为结尾
acl work time MTWHF 08:30-17:30 //对某段时间做限制,MTWHF是周一到周五,AC是周六周日
//拒绝localip列表;以上往下的顺序
http_access deny localip
- acl访问控制的ip地址,也可以放在一起列表管理
方便,灵活,减少配置文件的代码量
mkdi /etc/squid/ //创建目录,存放对象地址
vim /etc/squid/dest.ip //输入web目标地址
20.0.0.11
20.0.0.12
.....
- 配置文件内
vim /etc/squid.conf
acl desthost dst "/etc/squid/dest.ip"
-
重启squid
service squid restart -
客户端访问
五,squid日志分析sarg工具
-
安装准备环境
yum -y install gd gd-devel //图像处理扩展库gd -
创建编译安装准备目录
mkdir /usr/local/sarg -
将软件拖到/opt 解压
cd /opt
tar zxvf sarg-2.3.7.tar.gz -
编译安装
cd sarg-2.3.7/
./configure --prefix=/usr/local/sarg --sysconfdir=/etc/sarg --enable-extraprotection
//–enable-extraprotection //额外安全防护
make && make install
- 将sarg配置备份,修改配置文件
cd /etc/sarg/
cp sarg.conf sarg.conf.bak
编辑配置
vim 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
//配置解释:
7:指定访问日志文件
25:网页标题,可自定义
120:报告输出目录,存放目录
178:使用用户名显示
184:top排序中有连接次数,访问字节,降序排列,升序是normal
206:不计入排序的站点列表文件
257:同名日志是否覆盖
289:发送邮件报告命令
434:使用字符集
518:top排序的星期周期
525:top排序的时间周期
633:网页根目录
-
创建列表文件
touch /usr/local/sarg/noreport -
将sarg命令能够被系统识别
ln -s /usr/local/sarg/bin/sarg /usr/local/bin/
-
安装httpd
yum -y install httpd -
开启sarg工具,生成报告
[root@as sarg]# sarg
SARG: 纪录在文件: 81, reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/squid-reports/2020Nov01-2020Nov01
- 执行每天生成报告
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)
前一天的时间 - 今天的时间 = 就是一天的生成报告时间
[root@as squid-reports]# date -d "1 day ago" +%d/%m/%Y
31/10/2020
[root@as squid-reports]# date +%d/%m/%Y
01/11/2020
六,squid 反向代理
20.0.0.11 网页文档修改
vi /var/www/html/index.html
<h1> this is web1 </h1>
- 新添加 web2节点 20.0.0.14
//安装httpd
yum -y install httpd
//开启服务,做站点目录网页文档,
vi /var/www/html/index.html
<h1> this is web2 </h1>
systemctl enable httpd
systemctl start httpd
netstat -antp|grep 80
//添加路由表,静态路由指向代理服务器ip
route add -net 192.168.100.0/24 gw 20.0.0.13
route -n //查看路由表
ping 192.168.100.10 测试客户端的连通性
- squid端 修改squid配置,改为反向代理
vim /etc/squid.conf
http_port 20.0.0.16:80 accel vhost vport //监听squid代理地址,端口为80
cache_peer 20.0.0.11 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web //访问后面web节点服务,重定向,定位到80端口,采用轮询机制,最大访问量30 , 权重为1 ,别名web
cache_peer 20.0.0.12 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2 //访问后面web节点服务,重定向,定位到80端口,采用轮询机制,最大访问量30 , 权重为1 ,别名web
cache_peer_domain web web2 www.as.com //匹配,如果访问的域名是www.as.com 就访问到两个web节点;
//停止httpd服务;端口冲突
systemctl stop httpd
//重启squid服务,过滤端口
[root@squid ~]# service squid restart
正在关闭 squid...
正在启动 squid...
[root@squid ~]# netstat -antp |grep squid
tcp 0 0 20.0.0.13:80 0.0.0.0:* LISTEN 60392/(squid-1)
- 防火墙策略
[root@squid ~]# iptables -F //清除之前的所有规则
[root@squid ~]# iptables -t nat -F //清除之前表内的所有策略
[root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT //新添加入规则,允许端口3128
[root@squid ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:squid
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
- 客户端
验证:
//开启代理ip