文章目录
一、缓存代理概述
1.1 Web代理的工作机制
缓存网页对象,减少重复请求
注意:Squid代理服务器和源站服务器之间跑的就是BGP。
1.2 代理的基本类型
- 1.传统代理:需明确指定服务端
- 2.透明代理:客户机不需指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将Web访问重定向给代理服务器处理
(区别在于需不需要客户端自己在浏览器上指定服务端) - 反向代理:如果Squid反向代理服务器中缓存了该请求的资源,则将该请求的资源直接返回给客户端;否则反向代理服务器将向后台的WEB服务器请求资源,然后将请求的应答返回给客户端,同时也将该应答缓存在本地,供下一个请求者使用。
1.3 使用代理的优势
1.提高Web访问速度
2.隐藏客户机的真实IP地址
二、传统代理的搭建
主机 | IP地址 |
---|---|
Squid代理服务器 | 192.168.73.10 |
Web网站服务(源主机) | 192.168.73.20 |
客户机 | 192.168.73.30 |
1.安装依赖环境
yum -y install gcc gcc-c++ make
2.编译安装Squid服务
[root@Squid ~]# tar zxf squid-3.5.23.tar.gz -C /opt
[root@Squid ~]# cd /opt/squid-3.5.23/
[root@Squid squid-3.5.23]# ./configure --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 \ ###支持下划线
> --enable-poll \ ###默认使用poll模式,开启epoll模式时提升性能
> --enable-gnuregex ###支持正则表达式
[root@Squid squid-3.5.23]# make && make install
[root@Squid squid-3.5.23]# ln -s /usr/local/squid/sbin/* /usr/local/sbin
[root@Squid squid-3.5.23]# useradd -M -s /sbin/nologin squid ###创建不可登录的程序用户
[root@Squid squid-3.5.23]# chown -R squid.squid /usr/local/squid/var
3.修改配置文件,优化启动项
[root@Squid ~]# vi /etc/squid.conf
cache_effective_user squid #添加 指定程序用户
cache_effective_group squid #添加 指定账号基本组
[root@Squid ~]# squid -k parse ###检查配置文件语法
[root@Squid ~]# squid -z ###初始化缓存目录
[root@Squid ~]# squid ###启动服务
[root@Squid ~]# netstat -anpt | grep squid
tcp6 0 0 :::3128 :::* LISTEN 104314/(squid-1)
4.添加服务到service管理
[root@Squid ~]# vi /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..."
$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..."
$0 start &> /dev/null
echo "正在启动 squid..."
;;
reload)
$CMD -k reconfigure
;;
check)
$CMD -k parse
;;
*)
echo "用法:$0{start|stop|status|reload|check|restart}"
;;
esac
[root@Squid ~]# chmod +x /etc/init.d/squid
[root@Squid ~]# chkconfig --add squid
[root@Squid ~]# chkconfig --level 35 squid on
5.配置传统代理
[root@Squid ~]# vi /etc/squid.conf
http_access allow all
http_access deny all
http_port 3128
cache_mem 64 MB ###指定缓存功能所使用的内存空间大小,便于保持访问较频繁的WEB对象,容量最好为4的倍数,单位为MB,建议设为物理内存的1/4
reply_body_max_size 10 MB ###允许用户下载的最大文件大小,以字节为单位。默认设置0表示不进行限制
maximum_object_size 4096 KB ###允许保存到缓存空间的最大对象大小,以KB为单位,超过大小限制的文件将不被缓存,而是直接转发给用户
[root@Squid ~]# setenforce 0
[root@Squid ~]# iptables -F
[root@Squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@Squid ~]# systemctl restart squid
6.在web服务器上安装http服务
systemctl stop firewalld
setenforce 0
yum -y install httpd
7.看客户机能否成功访问Apache首页
查看日志文件,看访问的IP
是客户机自身IP
8.设置Squid代理,查看来访IP变化
1、先清除浏览器缓存
2、在浏览器中设置代理
3、查看日志文件中来访IP变化
成功变化代理IP
三、透明代理
在搭建的传统代理基础上做如下修改:
(1)squid服务器添加一块网卡:192.168.100.20(仅主机模式);开启路由哦转发功能
(2)web服务器不变
(3)客户端IP地址修改为192.168.100.10,且浏览器关闭手动代理设置
主机 | IP地址 |
---|---|
Squid代理服务器 | 192.168.73.10,192.168.100.20 |
Web网站服务(源主机) | 192.168.73.20 |
客户机 | 192.168.100.10 |
squid服务器配置:
1、开启路由转发功能
[root@Squid ~]# vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@Squid ~]# sysctl -p
net.ipv4.ip_forward = 1
2、修改配置文件
vi /etc/squid.conf
http_port 192.168.100.20:3128 transparent
systemctl restart squid.service
3.配置防火墙规则
[root@Squid ~]# iptables -F
[root@Squid ~]# iptables -t nat -F
[root@Squid ~]# iptables -t nat -I PREROUTING -i ens37 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
[root@Squid ~]# iptables -t nat -I PREROUTING -i ens37 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
[root@Squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
4.web端设置
[root@Web ~]# route add -net 192.168.100.0/24 gw 192.168.73.10 ###添加一条静态路由
5.测试
修改为不使用代理
查看日志
通过代理访问,成功!
四、ACL访问控制
使用acl访问控制列表,禁止客户机访问web服务器
1.修改配置文件
[root@Squid ~]# vi /etc/squid.conf
acl host src 192.168.100.10/32
http_access deny host ###禁止访问,注意置顶
[root@Squid ~]# systemctl restart squid
2.测试
查看squid日志
成功!
六、Squid 日志分析
1.安装图像处理软件包
yum install -y pcre-devel gd gd-devel
mkdir /usr/local/sarg
tar zxvf sarg-2.3.7.tar.gz -C /opt/
cd /opt/sarg-2.3.7
./configure --prefix=/usr/local/sarg --sysconfdir=/etc/sarg --enable-extraprotection
make && make install
#---------./configure中模块解释---------------------------------------
--prefix=/usr/local/sarg
--sysconfdir=/etc/sarg #配置文件目录,默认是/usr/local/etc
--enable-extraprotection #额外安全防护
#------------------------------------------------------------------
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 #top排序中,指定连接次数采用降序排列,升序是normal
#---------190行,取消注释,修改--------------------------------------
user_sort_field connect reverse #对于用户访问记录,连接次数按降序排序
#---------206行,取消注释,修改--------------------------------------
exclude_hosts /usr/local/sarg/noreport #指定不计入排序的站点列表的文件
#---------257行,取消注释-------------------------------------------
overwrite_report no #同名同日期的日志是否覆盖
#---------289行,取消注释,修改--------------------------------------
mail_utility mailq.postfix #发送邮件报告命令
#---------434行取消注释,修改---------------------------------------
charset UTF-8 #指定字符集UTF-8
#---------518行,取消注释-------------------------------------------
weekdays 0-6 #top排行的星期周期
#---------525行,取消注释-------------------------------------------
hours 0-23 #top排行的时间周期
#---------633行,取消注释--------------------------------------------
www_document_root /var/www/html #指定网页根目录
2.添加不计入站点文件,添加的域名将不被显示在排序中
touch /usr/local/sarg/noreport
ln -s /usr/local/sarg/bin/sarg /usr/local/bin/
sarg --help
3.运行
4.验证
此时安装的web服务实在squid服务器上,其他客户端机器无法访问测试的页面
yum -y install httpd
systemctl start httpd
5.做周期性计划任务crontab使其每天生成报告
[root@Squid ~]# 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)
SARG: TAG: access_log /usr/local/squid/var/logs/access.log
SARG: TAG: title "Squid User Access Reports"
SARG: TAG: output_dir /var/www/html/squid-reports
SARG: TAG: user_ip no
SARG: TAG: topuser_sort_field BYTES reverse
SARG: TAG: exclude_hosts /usr/local/sarg/noreport
SARG: TAG: overwrite_report no
SARG: TAG: mail_utility mailq.postfix
SARG: TAG: charset UTF-8
SARG: TAG: weekdays 0-6
SARG: TAG: hours 0-23
SARG: TAG: www_document_root /var/www/html
SARG: 纪录在文件: 127, reading: 100.00%
SARG: 期间被日志文件覆盖: 07/11/2020 - 08/11/2020
SARG: (info) date=08/11/2020
SARG: (info) period=2020 11月 07-2020 11月 08
SARG: (info) outdirname=/var/www/html/squid-reports//2020Nov07-2020Nov08
SARG: (info) Dansguardian report not produced because no dansguardian configuration file was provided
SARG: (info) No redirector logs provided to produce that kind of report
SARG: (info) No downloaded files to report
SARG: (info) Authentication failures report not produced because it is empty
SARG: (info) Redirector report not generated because it is empty
SARG: 成功的生成报告在 /var/www/html/squid-reports//2020Nov07-2020Nov08
[root@Squid ~]# crontab -e
30 22 * * * 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)
#测试在浏览器输入 192.168.73.10/squid-reports/,又会出现一条新的访问记录,然后查看/var/www/html/squid-reports文件
[root@Squid ~]# cd /var/www/html/squid-reports/
[root@Squid squid-reports]# ll
总用量 8
drwxr-xr-x. 4 root root 201 11月 8 13:44 2020Nov07-2020Nov08
drwxr-xr-x. 4 root root 201 11月 8 12:14 2020Nov07-2020Nov08.1
drwxr-xr-x. 2 root root 92 11月 8 12:14 images
-rw-r--r--. 1 root root 4690 11月 8 13:44 index.html
六、Squid反向代理
在透明模式的基础上进行反向代理
因为httpd会占用80端口,所以必须关闭squid服务器中的httpd服务
Web1配置
[root@Web1 ~]# yum -y install httpd
[root@Web1 ~]# echo "<h1>this is test1 web </h1>" > /var/www/html/index.html
[root@Web1 ~]# systemctl start httpd
[root@Web1 ~]# netstat -anpt | grep httpd
tcp6 0 0 :::80 :::* LISTEN 50552/httpd
[root@Web1 ~]# route add -net 192.168.100.0/24 gw 192.168.73.10 ###添加静态路由
Web2配置
[root@Web2 ~]# yum -y install httpd
[root@Web2 ~]# echo "<h1>this is test2 web </h1>" > /var/www/html/index.html
[root@Web2 ~]# systemctl start httpd
[root@Web2 ~]# netstat -anpt | grep httpd
tcp6 0 0 :::80 :::* LISTEN 14645/httpd
[root@Web2 ~]# route add -net 192.168.100.0/24 gw 192.168.73.10
squid配置
[root@Squid ~]# iptables -F
[root@Squid ~]# iptables -t nat -F
[root@Squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@Squid ~]# vi /etc/squid.conf
# Squid normally listens to port 3128
http_port 192.168.73.10:80 accel vhost vport ###squid外网口IP
cache_peer 192.168.73.20 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
cache_peer 192.168.73.40 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
cache_peer_domain web1 web2 www.yun.com
[root@Squid ~]# systemctl restart squid
测试
vi /etc/hosts
192.168.73.10 www.yun.com
刷新
轮询web1和web2