一、概述
一、web代理机制
缓存网页对象,减少重复请求。
二、squid
Squid是一个缓存Internet数据的一个软件,它接收用户的下载申请,并自动处理所下载的数据。也就是说,当一个用户想要下载一个主页时,它向Squid发出一个申请,要Squid替它下载,然后Squid 连接所申请网站并请求该主页,接着把该主页传给用户同时保留一个备份,当别的用户申请同样的页面时,Squid把保存的备份立即传给用户,减少了向Internet提交重复的Web请求的过程,提高了用户下载网页的速度,隐藏了客户机的真实IP。
一、类型
1、传统代理:适用于Internet,需在客户机指定代理服务器的地址和端口。
2、透明代理:客户机不需指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将web访问重定向给代理服务器处理。
3、反向代理:反向代理服务器中缓存了该请求的资源,则将该请求的资源直接返回给客户端:否则反向代理服务器将向后台的 WBB服务器请求资源,然后将请求的应答返回给客户端,同时也将该应答缓存(静态)在本地,供下一个请求者使用。
二、实例
实验环境
squid:192.168.238.150
web1:192.168.238.100
web2:192.168.238.99
一、squid服务安装
[root@localhost ~]# hostnamectl set-hostname squid
[root@localhost ~]# su
[root@squid ~]# ntpdate ntp.aliyun.com
[root@squid ~]# cd /opt
[root@squid opt]# rz #上传squid-3.5.27.tar.gz软件包
[root@squid opt]# yum -y install gcc gcc-c++ make #安装服务依赖环境
[root@squid opt]# tar xvf squid-3.5.27.tar.gz
[root@squid squid-3.5.27]# ./configure \
--prefix=/usr/local/squid \ #安装目录
--sysconfdir=/etc \ #单独将配置文件修改到/etc目录下
--enable-arp-acl \ #可在ACL中设置通过MAC地址进行管理,防止IP欺骗
--enable-linux-netfilter \ #使用内核过滤
--enable-linux-tproxy \ #支持透明模式
--enable-async-io=100 \ #异步I/O,提升储存性能,值可修改
--enable-err-language="Simplify_Chinese" \ #错误信息的显示语言
--enable-underscore \ #允许URL中有下划线
--enable-poll \ #使用Poll()模式,提升性能
--enable-gnuregex #使用GNU正则表达式
[root@squid squid-3.5.27]# make && make install
[root@squid squid-3.5.27]# ln -s /usr/local/squid/sbin/* /usr/local/sbin #创建链接文件,优化路径
[root@squid squid-3.5.27]# useradd -M -s /sbin/nologin squid #创建程序用户
[root@squid squid-3.5.27]# chown -R squid:squid /usr/local/squid/var/ #改变目录属主
[root@squid squid-3.5.27]# vim /etc/squid.conf #修改Squid的配置文件
55 # And finally deny all other access to this proxy
56 http_access allow all #放在http_access deny all之前,允许任意客户机使用代理服务,控制规则自上而下匹配
57 http_access deny all
58
59 # Squid normally listens to port 3128
60 http_port 3128 #用来指定代理服务监听的地址和端口(默认的端口号为3128)
61 cache_effective_user squid #指定程序用户,用来设置初始化、运行时缓存的账号,否则启动不成功
62 cache_effective_group squid #指定账号基本组
63
64 # Uncomment and adjust the following to add a disk cache directory.
65 #cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256
66
67 # Leave coredumps in the first cache dir
68 coredump_dir /usr/local/squid/var/cache/squid #指定缓存文件目录
[root@squid squid-3.5.27]# squid -k parse #检查配置文件
[root@squid squid-3.5.27]# squid -k rec #重新加载配置文件
[root@squid squid-3.5.27]# squid -zX #初始化缓存目录
[root@squid squid-3.5.27]# squid #开启服务
[root@squid squid-3.5.27]# netstat -anpt | grep squid
tcp6 0 0 :::3128 :::* LISTEN 121831/(squid-1)
#或者执行下面的脚本
[root@squid squid-3.5.27]# vim /etc/init.d/squid #编写squid服务脚本
#!/bin/bash
#chkconfig: 35 90 25
#config: /etc/squid.conf
#pidfile: /usr/local/squid/var/run/squid.pid
#Description: Squid - Internet Object Cache
PID="/usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"
case "$1" in
start)
netstat -utpln | grep squid &>/dev/null
if [ $? -eq 0 ]
then
echo "Squid is running"
else
$CMD
fi
;;
stop)
$CMD -k kill &>/dev/null
rm -rf $PID &>/dev/null
;;
status)
[ -f $PID ] &>/dev/null
if [ $? -eq 0 ]
then
netstat -utpln | 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 "用法:{start | stop | restart | reload | check | status}"
esac
[root@squid squid-3.5.27]# chmod +x /etc/init.d/squid #赋予执行权限
[root@squid squid-3.5.27]# chkconfig --add squid #加入系统服务
[root@squid squid-3.5.27]# chkconfig squid on #指定squid在各等级为on
[root@squid squid-3.5.27]# service squid restart #
正在关闭Squid...
正在启动Squid...
二、web服务器
[root@localhost ~]# hostnamectl set-hostname web1
[root@localhost ~]# su
[root@web1 ~]# ntpdate ntp.aliyun.com
[root@web1 ~]# yum install -y httpd
[root@web1 ~]# systemctl start httpd
[root@web1 ~]# netstat -antp | grep httpd
tcp6 0 0 :::80 :::* LISTEN 20578/httpd
三、传统代理
实验环境
squid:192.168.238.150
web1:192.168.238.100
client:192.168.238.60
[root@squid squid-3.5.27]# vim /etc/squid.conf
cache_mem 64 MB
#指定缓存功能所使用的内存空间大小,便于保存访问较频繁的WEB对象,容量最好为4的倍数,单位为MB,建议设为物理内存的1/4
reply_body_max_size 10 MB
#允许用户下载的最大文件大小,以字节为单位,当下载超过指定大小的Web对象时,浏览器的报错页面中会出现“请求或访问太大”的提示默认设置0表示不进行限制
maximum_object_size 4096 KB
#允许保存到缓存空间的最大对象大小,以KB为单位,超过大小限制的文件将不被缓存,而是直接转发给用户
[root@squid squid-3.5.27]# systemctl restart squid
[root@squid squid-3.5.27]# systemctl restart firewalld #开启防火墙
[root@squid squid-3.5.27]# iptables -F #清楚规则
[root@squid squid-3.5.27]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT #允许来自tcp3128的流量进入
client客户端
访问web1的IP地址,同时在web1上实时查看访问日志
四、透明代理
实验环境
squid:ens33:192.168.238.150 ens37:192.168.211.100
web1:192.168.238.100
client:ip:192.168.211.60 gw:192.168.211.100
squid服务器
[root@squid ~]# cd /etc/sysconfig/network-scripts/
[root@squid network-scripts]# cp ifcfg-ens33 ifcfg-ens37
[root@squid network-scripts]# vim ifcfg-ens37
[root@squid network-scripts]# systemctl restart network
[root@squid network-scripts]# cd /opt/squid-3.5.27/
[root@squid squid-3.5.27]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf #开启路由转发
[root@squid squid-3.5.27]# sysctl -p
net.ipv4.ip_forward = 1
[root@squid squid-3.5.27]# vim /etc/squid.conf
http_port 192.168.211.100:3128 transparent
#添加提供内网服务的IP地址,和支持透明代理选项transparent
[root@squid squid-3.5.27]# systemctl restart squid
[root@squid squid-3.5.27]# iptables -F
[root@squid squid-3.5.27]# iptables -t nat -F
[root@squid squid-3.5.27]# iptables -t nat -I PREROUTING -i ens33:0 -s 192.168.211.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
[root@squid squid-3.5.27]# iptables -t nat -I PREROUTING -i ens33:0 -s 192.168.211.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
[root@squid squid-3.5.27]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
web1服务器
[root@web1 ~]# route add -net 192.168.211.0/24 gw 192.168.238.150 #添加静态路由
client客户端
关闭上个实验设置的代理
五、ACL访问控制
在配置文件/etc/squid.conf 中,ACL访问控制通过以下两个步骤来实现:
1、使用acl 配置项定义需要控制的条件;
2、通过http_access配置项对已定义的列表做“允许”或“拒绝”访问的控制。
#squid服务器
[root@squid network-scripts]# vim /etc/squid.conf
client客户端再次访问web1的IP地址
六、日志分析
#rz上传sarg-2.3.7.tar.gz软件包
[root@squid opt]# yum install -y gd gd-devel pcre-devel #安装图像处理软件包
[root@squid opt]# tar xf sarg-2.3.7.tar.gz
[root@squid opt]# cd sarg-2.3.7/
[root@squid sarg-2.3.7]# ./configure --prefix=/usr/local/sarg \
> --sysconfdir=/etc/sarg \ #配置文件目录,默认是/usr/loca/etc
> --enable-extraprotection #额外安全防护
[root@squid sarg-2.3.7]# make && make install
[root@squid sarg-2.3.7]# 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/sarg #报告输出目录
#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 #指定网页根目录
[root@squid sarg-2.3.7]# touch /usr/local/sarg/noreport #添加不计入站点文件,添加的域名将不被显示在排序中
[root@squid sarg-2.3.7]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin #设置软链接方便管理
[root@squid sarg-2.3.7]# sarg #开启服务
SARG: 纪录在文件: 54, reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/sarg/2021Jul31-2021Jul31
[root@squid sarg-2.3.7]# yum install httpd -y #安装web服务
[root@squid sarg-2.3.7]# systemctl start httpd #开启
[root@squid sarg-2.3.7]# netstat -antp | grep 80 #查看端口
tcp6 0 0 :::80 :::* LISTEN 126978/httpd
在squid服务器上访问
优化:生成报告
[root@squid sarg-2.3.7]# vim /usr/local/sarg/report.sh
#!/bin/bash
#Get current 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 /usr/local/squid/var/logs/access.log -o /var/www/html/sarg -z -d $YESTERDAY-$TODAY &> /dev/null
exit 0
[root@squid sarg-2.3.7]# chmod +x /usr/local/sarg/report.sh
[root@squid sarg-2.3.7]# crontab -e
[root@squid sarg-2.3.7]# crontab -l
0 0 * * * /usr/local/sarg/report.sh
七、反向代理
工作机制
1、缓存网页对象,减少重复请求。
2、将互联网请求轮询或按权重分配到内网web服务器。
3、代理用户请求,避免用户直接访问Web服务器,提高安全。
实验环境
squid:192.168.238.150
web1:192.168.238.100
web2:192.168.238.99
client:192.168.238.60
[root@squid sarg-2.3.7]# systemctl stop httpd
[root@squid sarg-2.3.7]# iptables -F
[root@squid sarg-2.3.7]# iptables -t nat -F
[root@squid sarg-2.3.7]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@squid sarg-2.3.7]# iptables -L
[root@squid sarg-2.3.7]# vim /etc/squid.conf
32 #http_access deny host #将其注释
63 #http_port 192.168.211.100:3128 transparent #将其注释
64 http_port 192.168.238.150:80 accel vhost vport
65 cache_peer 192.168.238.100 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
66 cache_peer 192.168.238.99 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
67 cache_peer_domain web1 web2 www.aa.com
#以上代码说明
accel :反向代理加速模式
vhost:支持域名或主机名来表示代理节点
vport :支持IP和端口来表示代理节点
parent :代表为父节点,上下关系,非平级关系
80:代理内部web服务器的80端口
0 :没有使用icp,表示就一台squid服务器
no-query :不做查询操作,直接获取数据
originserver :指定是源服务器
round-robin :指定squid 通过轮询方式将请求分发到其中一台父节点
max_conn :指定最大连接数
weight :指定 权重
name :设置别名
[root@squid sarg-2.3.7]# systemctl restart squid
web服务器
[root@web1 ~]# echo "this is web1" > /var/www/html/index.html
[root@web2 ~]# echo "this is web2" > /var/www/html/index.html
client客户端
#修改域名映射
#修改代理
访问www.aa.com
小结:
1、传统代理(需要指向squid)
需要客户端指向squid 代理服务器,客户端能感知到squid 代理服务器的存在
2、透明代理(常用,不需要指向squid)
客户端不需要配置,只要直接访问即可,服务端,借助了防火墙规则及静态路由的方式,完成透明代理
3、反向代理(需要指向squid)
做为类似与Nginx服务器的反向代理功能,但自身不需要一个首页,基于虚拟的IP:端口、以及虚拟的域名进行反向代理给后端真实服务器的IP:端口,并且以权重的方式完成反向代理(负载均衡)