squid代理服务器
squid主要提供缓存加速、应用层过滤控制的功能。
1、代理的工作机制
代替客户机向网站请求数据,从而可以隐藏用户的真实IP地址。
将获得的网页数据(静态web元素)保存到缓存中并发送给客户机,以便下次请求相同的数据时快速响应。
2、代理的类型
①传统代理:
适用于Internet,需在客户机指定代理服务器的地址和端口。
②透明代理:
客户机不需指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将Heb访问重定向给代理服务器处理
③反向代理:如果squid
反向代理服务器中缓存了该请求的资源,则将该请求的资源直接返回给客户端;否则反向代理服务器将向后台的 wEB服务器请求资源,然后将请求的应答返回给客户端,同时也将该应答缓存〈静态)在本地,供下一个请求者使用。
安装和编译squid
[root@squid opt]# tar xvf squid-3.5.27.tar.gz
[root@squid opt]# yum -y install gcc gcc-c++ make
[root@squid squid-3.5.27]# cd squid-3.5.27/
[root@squid squid-3.5.27]# ./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
[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/ 改变目录属主
修改squid的配置文件
[root@squid squid-3.5.27]# vim /etc/squid.conf
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 添加,指定账号基本组
编写squid的脚本
[root@squid squid-3.5.27]# vim /etc/init.d/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
[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 -natp | grep squid
[root@squid squid-3.5.27]# service squid restart
搭建传统代理
squid服务器
[root@squid squid-3.5.27]# vim /etc/squid.conf
[root@squid squid-3.5.27]# service squid restart
修改防火墙规则
[root@squid squid-3.5.27]# systemctl start firewalld
[root@squid squid-3.5.27]# iptables -F
[root@squid squid-3.5.27]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
配置web1、web2的httpd
[root@web1 ~]# yum -y install httpd
[root@web1 ~]# systemctl start httpd
[root@web1 ~]# netstat -natp | grep httpd
web2 服务器查看访问日志信息
动态查看访问日志,观察来访IP
[root@web2 ~]# tail -f /var/log/httpd/access_log
透明代理
添加网卡
[root@squid squid-3.5.27]# 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
修改添加内网服务的IP地址
[root@squid network-scripts]# vim /etc/squid.conf
[root@squid network-scripts]# service squid restart
[root@squid network-scripts]# netstat -antp | grep squid
[root@squid network-scripts]# vim /etc/sysctl.conf
[root@squid network-scripts]# sysctl -p
添加防火墙规则
[root@squid network-scripts]# iptables -F
[root@squid network-scripts]# iptables -t nat -F
[root@squid network-scripts]# iptables -t nat -I PREROUTING -i ens37 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
[root@squid network-scripts]# iptables -t nat -I PREROUTING -i ens37 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
web1和web2服务器添加一条静态路由
[root@web1 ~]# route add -net 192.168.100.0/24 gw 192.168.235.144
[root@web1 ~]# netstat -natp | grep 80
在win10上访问web1
squid日志分析
安装编译图像处理软件包
[root@squid opt]# yum -y install gd gd-devel pcre-devel
[root@squid opt]# mkdir /usr/local/sarg
[root@squid opt]# tar zxvf sarg-2.3.7.tar.gz -C /opt/
[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]# 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]# touch /usr/local/sarg/noreport
[root@squid sarg]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/
[root@squid sarg]# sarg 启动一次记录
验证
[root@squid sarg]# yum -y install httpd
[root@squid sarg]# systemctl start httpd
添加计划任务,执行每天生成报告
[root@squid sarg]# 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]# crontab -e
反向代理
如果Squid反向代理服务器中缓存了该请求的资源,则将该请求的资源直接返回给客户端;否则反向代理服务器将向后台的Web服务器请求资源,然后将请求的应答返回给客户端,同时也将该应答缓存在本地,供下一个请求者使用。
工作机制:
缓存网页对象,减少重复请求
将互联网请求轮询或按权重分配到内网web服务器
代理用户请求,避免用户直接访问Web服务器,提高安全
开启防火墙、关闭httpd
[root@squid sarg]# systemctl restart firewalld
[root@squid sarg]# systemctl stop httpd
[root@squid sarg]# iptables -F
[root@squid sarg]# iptables -t nat -F
[root@squid sarg]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
修改配置文件
[root@squid sarg]# vim /etc/squid.conf
61 http_port 192.168.235.144:80 accel vhost vport
62 cache_peer 192.168.235.156 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web 1
63 cache_peer 192.168.235.179 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web 2
64 cache_peer_domain web1 web2 www.kgc.com
[root@squid sarg]# service squid reload
[root@squid sarg]# netstat -natp | grep squid
后端web1、web2安装httpd
[root@web2 ~]# yum -y install httpd
[root@web2 ~]# systemctl restart httpd
[root@web2 ~]# netstat -natp | grep httpd
在web1添加 this is kgc web
[root@web1 ~]# cd /var/www/html/
[root@web1 html]# ls
[root@web1 html]# vim index.html
在web2添加 this is accp web
[root@web2 ~]# cd /var/www/html/
[root@web2 html]# ls
[root@web2 html]# vim index.html
客户机的代理配置
小结
squid代理的三种模式比较
1,传统代理,需要指定客户端指定squid代理服务,客户端感知到squid代理服务的存在
2,透明代理:客户端不需要配置,利用squid服务器的防火墙规则和web服务的静态路由方式,实验代理的功能,对用户时无感知的
3,反向代理:类似于nginx的服务器反向代理功能,但是资深粗腰一个首页,基于虚拟的ip以及端口,以及虚拟的域名进行反向代理给后端真是的服务器ip而且可以采用负载均衡的方式代理
squid自身的管理功能
ACL:做acces的访问控制,增加一些允许和拒绝管理
sarg:日志分析工具,可以以天的方式将access_log中的内容以web页面的方式,展示给用户。