文章目录
一、squid 概述
Squid是一个高性能的代理缓存服务器,Squid支持FTP、gopher、HTTPS和HTTP协议。和一般的代理缓存软件不同,Squid用一个单独的、非模块化的、I/O驱动的进程来处理所有的客户端请求。
Web代理的工作机制
缓存网页对象,减少重复请求
代理的基本类型
传统代理
适用于internet ,需要指定服务端,开启代理ip地址
透明代理
客户机不需要指定代理服务器的地址和端口,而通过默认路由,防火墙策略将web访问定向给代理服务器处理
代理的好处
访问速率提高
隐藏客户机真是ip地址
二、传统dl搭建
2.1、实验环境
VMware软件
一台centos7.4虚拟机作为squid服务器,IP地址为:20.0.0.21
一台centos7.4虚拟机作为web服务器,IP地址为:20.0.0.22
真机可做为client测试机
2.2、实验过程
2.2.1、部署squid 服务
把squid软件包传进/opt下
[root@squid ~]# cd /opt
[root@squid opt]# tar zxvf squid-3.4.6.tar.gz
[root@squid opt]# cd squid-3.4.6/
[root@squid squid-3.4.6]# yum -y install gcc gcc-c++
[root@squid 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
--prefix=/usr/local/squid \ ## 指定安装路径
--sysconfdir=/etc \ ## 配置文件所存在目录
--enable-arp-acl \ ## 启用acl访问控制列表
--enable-linux-netfilter \ ## 内核过滤表
--enable-linux-tproxy \ ## 支持透明代理模式
--enable-async-io=100 \ ## io的优化
--enable-err-language="Simplify_Chinese" \ ##报错简体中文
--enable-underscore \ ## 在url中支持下划线
--enable-poll \ ## poll是Linux的字符设备驱动中的一个函数
--enable-gnuregex \ ## url支持正则表达式
[root@squid squid-3.4.6]# make && make install
[root@squid squid-3.4.6]# ln -s /usr/local/squid/sbin/squid /usr/local/sbin/ ## 做软连接,让squid命令便于被系统识别
[root@squid squid-3.4.6]# ll /usr/local/sbin/
总用量 34992
lrwxrwxrwx. 1 root root 22 10月 30 19:13 sbin -> /usr/local/squid/sbin/
-rwxr-xr-x. 2 root root 35829680 10月 30 19:08 squid
[root@squid squid-3.4.6]# useradd -M -s /sbin/nologin squid ## 创建squid用户,并指定家目录,指定shell环境,无法登录终端
[root@squid squid-3.4.6]# chown -R squid.squid /usr/local/squid/var ## 给权限,有一个run目录,跑pid进程文件
修改squid配置文件,优化启动项
[root@squid squid-3.4.6]# vim /etc/squid.conf
56: http_access allow all ## 添加默认允许通过
57 :#http_access deny all ## 把原来的默认拒绝注释掉
60: http_port 3128 ## squid 端口号 3128
61: cache_effective_user squid ## 缓存管理用户 squid
62: cache_effective_group squid ## 缓存管理组 squid
68:coredump_dir /usr/local/squid/var/cache/squid ## 是一个容灾备份的目录
[root@squid squid-3.4.6]# squid -k parse ## 查看语法
[root@squid squid-3.4.6]# squid -z ## 语法没有问题后,初始化缓存
[root@squid squid-3.4.6]# squid ## 启动squid
[root@squid squid-3.4.6]# netstat -anpt | grep 3128 ## 过滤一下端口,查看是否启动
tcp6 0 0 :::3128 :::* LISTEN 36645/(squid-1)
添加服务到service管理
[root@squid ~]# cd /etc/init.d/ ##squid启动脚本
[root@squid init.d]# vim 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
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 ## $0 是脚本名称
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@squid init.d]# chmod +x squid ##给执行权限
[root@squid init.d]# chkconfig --add squid ##添加到service管理中
[root@squid init.d]# chkconfig --level 35 squid on ##开机自启
2.2.2、配置传统模式
root@squid init.d]# vim /etc/squid.conf
60 :http_port 3128
61 :cache_effective_user squid
62 :cache_effective_group squid ## 添加如下代码
63 :cache_mem 64 MB ## 指定缓存功能所使用的内存空间大小,便于保持访问较频繁的WEB对象,容量最好为4的倍数,单位为M
64 :reply_body_max_size 10 MB ##允许用户下载最大文件大小,以自己字节为单位,默认设置0表示不进行限制
65 :maximum_object_size 4096 KB ##允许保存到缓存空间最大对象大小,以KB为单位,超过大小限制的文件将不被缓存,而是直接发往用户端
[root@squid init.d]# iptables -F
[root@squid init.d]# iptables -t nat -F
[root@squid init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
-I 在开头插入input链 , -p 用的是tcp 协议 ,--dport 指定端口3128 , -j ACCEPT 允许通过
[root@squid init.d]# service squid reload ## 重载配置
配置WEB端
[root@web ~]# systemctl stop firewalld ## 关闭防火墙
[root@web ~]# setenforce 0 ## 关闭核心防护
[root@web ~]# yum -y install httpd ## 安装apache服务
[root@web ~]# systemctl start httpd
[root@web ~]# systemctl enable httpd
[root@web ~]# netstat -anpt | grep httpd
tcp6 0 0 :::80 :::* LISTEN 40633/httpd
2.2.3、真机测试
打开谷歌浏览器—>设置—>高级—>系统—>打开您计算机的代理设置—>找到手动代理设置,地址填:20.0.0.21(代理服务器IP) 端口:3128—>保存—>清空浏览器缓存,重新输入20.0.0.22web页面,查看日志,发现客户端访问20.0.0.22web页面时,是在缓存代理服务器拿的web静态资源
cd /var/log/httpd/
cat access_log
20.0.0.1 - - [30/Oct/2020:14:13:08 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://20.0.0.22/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
20.0.0.21 - - [30/Oct/2020:14:15:10 +0800] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
20.0.0.21 - - [30/Oct/2020:14:15:10 +0800] "GET /noindex/css/fonts/Bold/OpenSans-Bold.woff HTTP/1.1" 404 239 "http://20.0.0.22/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
三、透明dl搭建
3.1、实验环境
VMware软件
一台centos7.4虚拟机作为squid服务器,IP地址为:20.0.0.21
另一张网卡 ip地址为:192.168.100.1
一台centos7.4虚拟机作为web服务器,IP地址为:20.0.0.22
一台win10虚拟机作为client服务器,IP地址为:192.168.100.20
3.2、squid 服务器配置
配置第二张网卡
[root@squid ~]# cd /etc/sysconfig/network-scripts/
[root@squid network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
[root@squid network-scripts]# vim ifcfg-ens36
NAME=ens36
DEVICE=ens36
ONBOOT=yes
IPADDR=192.168.100.1
NETMASKE=255.255.255.0
UUID、DNS都不用设置,可以删掉
[root@squid network-scripts]# systemctl restart network ###重启一下网卡
[root@squid network-scripts]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1 ###开启路由转发功能,1是开启,0是不开启
[root@squid network-scripts]# sysctl -p
net.ipv4.ip_forward = 1
web端配置一个回去的路由
[root@web ~]# route add -net 192.168.100.0/24 gw 20.0.0.21
squid服务器配置防火墙规则
[root@squid network-scripts]# vim /etc/squid.conf
http_port 192.168.100.1:3128 transparent ###把3182端口的地方改成透明模式
[root@squid network-scripts]# service squid start ###重启服务
[root@squid network-scripts]# iptables -F ###清除一下filter表防火墙规则(不指定表,默认是filter表)
[root@squid network-scripts]# iptables -t nat -F ###清除一下nat表防火墙规则
[root@squid network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3128 ###添加一个80端口访问的规则
[root@squid network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to 3128 ###访问的是https的就要改成443端口
[root@squid network-scripts]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT ###可以进行一个转发
3.3、测试
win10虚拟机要设置成192.168.100.20,网关为192.168.100.1,然后浏览器输入20.0.0.22访问
在web服务器查看
[root@web httpd]# cd /var/log/httpd/
[root@web httpd]# cat access_log
20.0.0.21 - - [30/Oct/2020:03:03:55 -0400] "GET /favicon.ico HTTP/1.1" 404 209 "http://20.0.0.22/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"
访问的20.0.0.21 Squid代理服务器,测试成功
四、反向dl搭建
在之前的传统代理环境下继续,之前的web为web1,在添加一台web2服务器,ip地址为20.0.0.23
4.1、web1 配置
[root@web1 ~]# vim /var/www/html/index.html
<h1>this is test1 web </h1>
[root@web1 ~]# systemctl start httpd
[root@web1 ~]# netstat -anpt | grep 80
tcp6 0 0 :::80 :::* LISTEN 1962/httpd
[root@web1 ~]# route add -net 192.168.100.0/24 gw 20.0.0.21 ## 添加一条把数据包传回来的静态路由
4.2、web2 配置
[root@web2 ~]# yum -y install httpd
[root@web2 ~]# vim /var/www/html/index.html
<h1>this is test2 web </h1>
[root@web2 ~]# systemctl start httpd
[root@web2 ~]# netstat -anpt | grep 80
tcp6 0 0 :::80 :::* LISTEN 1962/httpd
[root@web1 ~]# route add -net 192.168.100.0/24 gw 20.0.0.21 ## 添加一条把数据包传回来的静态路由
4.3、squid 服务器配置
[root@squid ~]# systemctl start firewalld
[root@squid ~]# iptables -F
[root@squid ~]# iptables -t nat -F
[root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@squid ~]# vim /etc/squid.conf
# Squid normally listens to port 3128
60 :http_port 20.0.0.21:80 accel vhost vport ## squid外网口IP
61 :cache_peer 20.0.0.22 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1 ## web1 IP
62 :cache_peer 20.0.0.23 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2 ## web2 IP
63 :cache_peer_domain web1 web2 www.yun.com
[root@squid ~]# service squid restart
4.4、测试
在客户端测试
C:\Windows\System32\drivers\etc 打开 hosts 做映射
20.0.0.21 www.yun.com
然后再浏览器输入 20.0.0.12 会轮询 web1 和 web2 的页面
五、ACI 控制
ACL(Access Control List,访问控制列表),可以针对源地址、目标地址、访问的URL路径、访问的时间等各种条件进行过滤
ACL访问控制的步骤
使用acl配置项定义需要控制的条件
通过http_access配置项对已定义的列表做允许或拒绝的访问控制
[root@squid ~]# vim /etc/squid.conf
7 :# should be allowed
8 :acl hostlocal src 20.0.0.22/24 ## 在squid代理服务器的acl访问列表添加一条访问20.0.0.22的记录
31 :# Deny requests to certain unsafe ports
32 :http_access deny hostlocal ## 然后拒绝这条访问,这样squid代理服务器就访问不了20.0.0.22了,同时也获取不到web资源
[root@squid ~]# systemctl restart squid
[root@squid ~]# netstat -anpt | grep 3128
tcp 0 0 192.168.100.2:3128 0.0.0.0:* LISTEN 9088/(squid-1)
## 测试,在客户端测试输入 squid的IP 20.0.0.21,访问不了了。因为squid访问20.0.0.22时被拒绝访问了
六、sarg 日志
[root@squid opt]# cd sarg-2.3.7/
[root@squid sarg-2.3.7]# ./configure \
> --prefix=/usr/local/sarg/ \
> --sysconfdir=/etc/sarg \
> --enable-extraprotection
[root@squid sarg-2.3.7]# make && make install
[root@squid sarg-2.3.7]# cd /etc/sarg/
[root@squid sarg]# 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 ## 使用用户名显示
206: exclude_hosts /usr/local/sarg/noreport ## 不计入排序的站点列表文件
184: topuser_sort_field connect reverse ## top排序中有连接次数、访问字节、降序排序 升序是normal
190: user_sort_field connect reverse ## 用户访问记录、连接次数、访问字节按降序排序
257: overwrite_report no ## 同名日志是否覆盖
289: mail_utility mailq.postfix ## 发送邮件报告命令
434: charset UTF-8 ## 使用字符集
518: weekdays 0-6 ## top排行的星期周期
525: hours 0-23 ## top排行的时间周期
633: www_document_root /var/www/html ## 网页根目录
[root@squid sarg]# mkdir /usr/local/sarg/noreport ## 创建报告输出目录
[root@squid sarg]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/
[root@squid sarg]# sarg
SARG: 纪录在文件: 330, reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/squid-reports/2020Oct30-2020Nov01
[root@squid sarg]# yum -y install httpd (如果是接着反向代理继续的 上边80端口已被占用,重启会报错,改成8080即可)
[root@squid sarg]# systemctl start httpd
[root@squid sarg]# cd /var/www/html/
[root@squid html]# ll
总用量 0
drwxr-xr-x. 4 root root 65 11月 1 14:52 squid-reports
## 测试 在浏览器输入 192.168.100.1/index-reports/
就会出现访问日志