缓存代理概述
Web代理的工作机制
作用:
缓存网页对象,减少重复请求
代理的基本类型
传统代理:适用于Internet,需明确指定服务端
透明代理:客户机不需指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将Web访问重定向给代理服务器处理
使用代理的好处
提高Web访问速度
隐藏客户机的真实IP地址
部署准备
Squid:192.168.1.10
web1:192.168.1.11
web2:192.168.1.12.
上传软件包 解压缩
tar zxvf squid-3.5.23.tar.gz
配置文件
cd squid-3.5.23/
./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
ln -s /usr/local/squid/sbin/* /usr/sbin/
useradd -M -s /sbin/nologin squid
chown -R squid.squid /usr /local/squid/var
编辑配置文件
vi /etc/squid.conf
添加内容
cache_effective_user squid
cache_effective_group squid
squid -k parse 检查语法
squid -z 初始化缓存目录
squid 启动服务
netstat -anpt | grep squid 查看端口状态
配置管理文件
vi /etc/init.d/squid
#!/bin/bash
#chkconfig: 35 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 -anpt | 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
;;
reload)
$CMD -k reconfigure
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then netstat -anpt | grep squid
else echo "squid is not running"
fi
;;
restart)
$0 stop &> /dev/null
$0 start &> /dev/null
;;
check)
$COM -k parse
;;
*)
echo "用法: $0{start|stop|status|restart|check|reload}"
esac
chmod +x /etc/init.d/squid
chkconfig --add /etc/init.d/squid
chkconfig --list
systemctl start squid
systemctl status squid
传统代理服务器配置
vi /etc/squid.conf
添加
http_access allow all
cache_mem 64 MB
reply_body_max_size 10 MB
maximum_object_size 4096 KB
squid -k parse
systemctl stop squid
systemctl start squid
netstat -anpt | grep squid
Squid配置完成
配置Web1、Web2一样的操作
yum -y install httpd
echo "<h1>This is web1</h1>" > /var/www/html/index.html
systemctl start httpd
curl http://localhost
访问 查看日志
tail -f /var/log/httpd/access_log
透明代理
实验环境
squid:192.168.10.11
192.168.1.11
web1:192.168.1.12
web2:192.168.1.13
部署准备
Web节点
route add -net 192.168.10.0/24 gw 192.168.1.10
关闭防火墙
代理服务器
安装Squid
vi /etc/squid.conf
http_port 192.168.10.10:3128 transparent
systemctl stop squid
systemctl start squid
iptables -t nat -I PREROUTING -i ens38 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
iptables -t nat -I PREROUTING -i ens38 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
客户机访问
ACL访问控制
常用的acl列表类型
src–源地址
dst–目标地址
port–端口号
destdomain–目标域
time–访问时间
maxconn–最大并发连接数
url_regex–目标url地址
urlpath_regex–整个目标url路径
vi /etc/squid.conf.
acl client src 192.168.10.13/32
http_access deny client
测试
squid日志分析
sarg
yum -y install gd gd-devel
mkdir /usr/local/sarg
tar xf sarg-2.3.7.tar.gz /optcd /opt/sarg-2.3.7/
./configure --prefix=/usr/local/sarg --sysconfdir=/etc/sarg --enable-extraprotection
make && make install
vi /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 reverse 用户访问记录连接次数、访问字节按降序排序(注释掉)
:206 修改添加 exclude_hosts /usr/local/sarg/ noreport 不计入排序的站点列表文件
:257 overwrite_report no 同名日志是否覆盖
:289 修改 mail_utility mailx 发送邮件报告命令
:434 修改 charset UTF-8 使用字符集
:518 weekdays 0-6 top排行的星期周期
:525 hours 0-23 top排行的时间周期
:633 www_document_root /var/www/html 网页根目录
touch /usr/local/sarg/noreport
ln -s /usr/local/sarg/bin/sarg /usr/local/bin/
yum -y install httpd
systemctl start httpd
周期性计划任务执行每天生成报告crontab
crontab -e
* 2 * * * /usr/lcoalbin/sarg -l /usr/local/var/logs/access.log -o /var/www/html/squid-reports/ -z -d $(date -d "1 day ago" +%Y-%m-%d)-$(date +%Y-%m-%d)
反向代理
工作机制
缓存网页对象,减少重复请求
将互联网请求轮询或按权重分配到内网尾部服务器
让用户请求,避免用户直接访问web服务器,提高安全性(类似于调度器)
配置
systemctl stop httpd
vi /etc/squid.conf
http_port 192.168.1.10:80 accel vhost vport
acche_peer 192.168.1.11 parent 80 0 no_query originserver round-robin max-conn=100 weight=1 name=web1
acche_peer 192.168.1.12 parent 80 0 no_query originserver round-robin max-conn=100 weight=1 name=web2
cache peer_domain web1 web2 www.chen.com #需要用域名访问时设置
systemctl restart squid
重启Squid
systemctl restart squid
查看状态
netstat -anpt | grep squid