squid代理服务器应用
1. squid代理服务器概述
1.1 squid简介
squid是一个支持http、https、ftp等服务的web缓存代理软件,通过缓存页面实现降低宽带占用提高页面响应时间。另外,squid提供访问控制,squid的缓存页面存放在内存和硬盘中,在选择服务器时候内存和硬盘要求高,对于数据过期更新,需要不定期的清理缓存的数据。
1.2. squid 代码请求流程
1 客户端访问squid代理服务器
2 代理服务器代表客户端访问后端真实服务器
3 真实服务器把数据返回给squid代理服务器
4 代理服务器把响应数据发送给客户端,并把页面缓存在本地的内存及磁盘中
5 客户端再次请求相同数据时,代理服务器直接将缓存的数据返回给客户端
1.3 squid代理服务类型
一般分三种类型,正向代理服务器、透明代理服务器、反向代理服务器。
正向代理:主要应用于内部网络访问外部网络时缓存页面数据,提供统一网络接口链接到外网,所有的内网客户端无需配置外网ip即可通过squid上网,在这种模式下,squid主要负责提供缓存加速及访问控制的功能
透明代理:与正向代理类似,不需要终端用户进行特殊设置,需要结合网关部署,所有操作均由管理员在网关服务器和代理服务器进行设置,这些对于用户透明的。
反向代理:反向代理结合智能DNS可以实现基本的CDN框架,此时的squid代理服务器可以直接代表后端真实服务器提供页面访问,用户并感觉不到自己是在访问代理服务器,有利于减轻真实服务器压力,提高并发和响应速度。
2. 实验
2.1 案例环境
node1 20.0.0.15
node2 20.0.0.16
squid 20.0.0.14
要求:客户机访问web服务器,通过代理服务器squid访问,不直接访问web服务器
2.2 传统代理
安装squid服务
[root@localhost ~]# hostnamectl set-hostname squid ##改主机名
[root@localhost ~]# su
[root@localhost ~]# tar zxvf squid-3.4.6.tar.gz -C /opt ###解压软件包
[root@localhost ~]#cd /opt/
[root@opt~]#cd squid-3.4.6/
[root@opt~]#yum -y install gcc gcc-c++ make ##下载编译器
[root@squid squid-3.4.6]# ./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 \ ##url支持下划线
--enable-poll \ ###一个函数 字符设备驱动函数
--enable-gnuregex ##支持正则表达式
[root@squid squid-3.4.6]# make -j4 && make install ###编译并安装
[root@squid squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/ ###让可用命令让系统所能识别
[root@squid squid-3.4.6]# useradd -M -s /sbin/nologin squid ###建立一个程序性用户
[root@squid squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/ ###给用户一个权限
[root@squid squid-3.4.6]# vim /etc/squid.conf ###改主配置文件
#http_access deny all ###默认拒绝所有,注销掉,或者放下面,从上往下读,不会拒绝掉
http_access allow all ###加上这条allow,允许所有终端来访问,并且可用访问其他的源端服务器
http_port 3128
cache_effective_user squid ###在端口下面加上缓存管理用户
cache_effective_group 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 ###启动服务
[root@squid squid-3.4.6]# netstat -anpt | grep 3128 ###可用查看一下启动了
root@squid squid-3.4.6]# cd /etc/init.d/
#!/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" ##启动命令
[root@squid init.d]# vim 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
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]# service squid stop ###关闭squid服务网
[root@squid init.d]# netstat -anpt | grep 3128 ###查看一下是否关闭掉了
[root@squid init.d]# service squid start ###在启动
正在启动 squid....
node1配置
[root@localhost ~]# hostnamectl set-hostname web #设置主机名web
[root@localhost ~]# su
[root@web ~]# systemctl stop firewalld ###关闭防火墙
[root@web ~]# setenforce 0 ###关闭核心防护
[root@web ~]# yum -y install httpd ###安装Apache
[root@web ~]# systemctl start httpd ###启动Apache服务
传统代理
[root@squid init.d]# vim /etc/squid.conf ###配置主配置文件,在端口下面添加这三条!
cache_mem 64 MB ###指定缓存功能所使用的内存空间大小
reply_body_max_size 10 MB ###允许用户下载的单个文件最大文件大小,以字节为单位。设置0表示不限制
maximum_object_size 4096 KB ###允许保存到缓存空间的最大对象大小,以KB为单位,超过大小限制的文件将不被缓存,优化指令(大文件保存在用户浏览器缓存里,不用来找服务器了,不然会占用服务器的资源)
[root@squid init.d]# iptables -F ###清空防火墙规则
[root@squid init.d]# iptables -t nat -F ###清空防火墙表规则
[root@squid init.d]# setenforce 0 ###关闭核心防护(临时的)
[root@squid init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT ###-I在开头插入input链(一个链包含多个规则),-p定义一个tcp协议,-dport 端口,-j 允许操作
[root@squid init.d]# service squid stop
[root@squid init.d]# service squid start
测试
1、打开谷歌浏览器
2、打开设置
3、点开高级
4、点击左下角系统
5、点击页面上的 打开您计算机的代理设置
6、手动设置代理下面设置,使用代理,地址是你的代理服务器的ip地址,端口是3128,点击保存
7、然后浏览器输入20.0.0.21,去访问web服务器
8、查看node1 httpd文件日志产生的信息是否是squid的地址
2.3 透明代理
squid服务器增加网卡
在代理服务器上添加一块网卡,设置成主机模式
[root@squid ~]# cd /etc/sysconfig/network-scripts/
[root@squid network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
[root@squid network-scripts]# vim ifcfg-ens37
NAME=ens37 ###名称改了
DEVICE=ens37 ###名称改了
ONBOOT=yes
IPADDR=192.168.100.1 ###ip地址改了
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 ###加载一下
node1web服务器配置
root@web ~]# route add -net 192.168.10.0/24 gw 20.0.0.20 ##静态路由
透明代理配置
[root@squid network-scripts]# vim /etc/squid.conf
http_port 192.168.10.1:3128 transparent ###把3182端口的地方改成透明模式
[root@squid network-scripts]# service squid start ###重启服务
[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.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128 ###添加一个80端口访问的规则
[root@squid network-scripts]# iptables -t nat -I PREROUTING -i ens37 -s 192.168.10.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 ###可以进行一个重定向转发
测试
在虚拟机window10系统的虚拟机中设置网关为192.168.100.1
然后通过浏览器访问node1的地址
查看node1 httpd文件日志产生的信息是否是squid的地址
2.4 反向代理
安装squid软件
将软件包上传到服务器中
[root@squid ~]# tar zxvf squid-3.4.6.tar.gz -C /opt/ ###将包解压到/opt目录下
[root@squid ~]# cd /opt/squid-3.4.6/
[root@squid squid-3.4.6]# yum -y install gcc gcc-c++ ###安装编译工具gcc、gcc-c++
./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" \ ###支持err语言(也就是报错是简体中文形式)
--enable-underscore \ ###url支持下划线
--enable-poll \ ###字符设备驱动函数(内核函数)
--enable-gnuregex \ ###支持正则表达式
[root@squid squid-3.4.6]# make -j4 && make install ###编译并安装
[root@squid squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/
[root@squid squid-3.4.6]# useradd -M -s /sbin/nologin squid
[root@squid squid-3.4.6]# chown -R squid:squid /usr/local/squid/var/
[root@squid squid-3.4.6]# vim /etc/squid.conf
http_access allow localnet
http_access allow localhost
#And finally deny all other access to this proxy
http_access allow all
#http_access deny all
#Squid normally listens to port 3128
http_port 3128
cache_effective_user squid
cache_effective_group 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 ###启动服务
[root@squid squid-3.4.6]# netstat -ntap | grep 3128
tcp6 0 0 :::3128 :::* LISTEN 86794/(squid-1)
[root@squid squid-3.4.6]# cd /etc/init.d/
[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
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]# chkconfig --add squid ####添加到service 管理
[root@squid init.d]# chmod +x squid ###执行权限
[root@squid init.d]# chkconfig --level 35 squid on
[root@squid init.d]# service squid stop
[root@squid init.d]# netstat -ntap |grep 3128
[root@squid init.d]# service squid start
正在启动 squid....
配置反向代理squid
[root@squid init.d]# iptables -L
[root@squid init.d]# iptables -F
[root@squid init.d]# iptables -t nat -L
[root@squid init.d]# iptables -t nat -F
[root@squid init.d]# setenforce 0
[root@squid ~]# vim /etc/squid.conf
http_port 20.0.0.13:8080 accel vhost vport ###监听自己ip地址8080端口,虚拟主机、虚拟端口。
cache_peer 20.0.0.15 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1 ###重定向到web1服务器80端口,禁止查询、指定增值服务器、轮询机制、最大访问数量30、权重为1、别名web1
cache_peer 20.0.0.16 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2 ###重定向到web1服务器80端口,禁止查询、指定增值服务器、轮询机制、最大访问数量30、权重为1、别名web2
cache_peer_domain web1 web2 www.yun.com ###如果访问www.yun.com就等于访问我两个节点服务器
cache_effective_user squid
cache_effective_group squid
[root@squid ~]# service squid restart ###重启一下squid
DNS域名解析服务
[root@localhost init.d]# yum -y install bind
[root@localhost etc]# cd named/
[root@localhost etc]# vi named.conf
options {
listen-on port 53 { any; }; ##监听53端口
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; }; ##允许任何用户登录
[root@localhost etc]# vi named.rfc1912.zones
zone "yun.com" IN {
type master;
file "yun.com.zone";
allow-update { none; };
};
[root@localhost etc]# cd /var/named/
[root@localhost named]# ll
total 16
drwxrwx---. 2 named named 6 Jun 1 23:26 data
drwxrwx---. 2 named named 6 Jun 1 23:26 dynamic
-rw-r-----. 1 root named 2253 Apr 5 2018 named.ca
-rw-r-----. 1 root named 152 Dec 15 2009 named.empty
-rw-r-----. 1 root named 152 Jun 21 2007 named.localhost
-rw-r-----. 1 root named 168 Dec 15 2009 named.loopback
drwxrwx---. 2 named named 6 Jun 1 23:26 slaves
[root@localhost named]# cp -R named.localhost yun.com.zone
[root@localhost named]# vi yun.com.zone
$TTL 1D
@ IN SOA @ admin. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 20.0.0.14
systemctl restart named
netstat -ntap | grep named
两个node web服务配置
[root@web ~]# systemctl stop firewalld ###关闭防火墙
[root@web ~]# setenforce 0 ###关闭核心防护
[root@web ~]# yum -y install httpd ###安装Apache
[root@web ~]# systemctl start httpd ###启动Apache服务
两个web服务分别编一个测试页
一个显示1
一个显示12
测试
window10 虚拟机配置
2.5 sarg日志分析
将sarg包上传到服务器
[root@squid ~]# tar zxvf sarg-2.3.7.tar.gz -C /opt/ ###解压到opt目录
[root@squid ~]# cd /opt/sarg-2.3.7/
[root@squid sarg-2.3.7]# yum -y install gd gs-devel ###gd图像化处理工具
[root@squid sarg-2.3.7]# mkdir /usr/local/sarg ###在 /usr/local/创建一个sarg工作目录
[root@squid sarg-2.3.7]# ./configure --prefix=/usr/local/sarg \ ###指定安装路径
--sysconfdir=/etc/sarg \ ###指定配置文件所存在位置
--enable-extraprotection ###开启安全防护
[root@squid sarg-2.3.7]# make -j3 && make install ###编译安装
[root@squid sarg-2.3.7]# cd /etc/sarg/
[root@squid sarg]# vim sarg.conf ###里面全是注释的,需要什么就找到修改和开启
shift+: 输入set nu,开启行数显示,这样更快定位
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
206行 exclude_hosts /usr/local/sarg/noreport ###要修改。不计入排序的站点列表文件
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]# touch /usr/local/sarg/noreport ###刚刚配置里面我们开启了不计入站点功能,现在要创建一个文件
[root@squid sarg]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/ ###让系统识别sarg的命令
[root@squid sarg]# sarg ###开启生成
SARG: Records in file: 1796, reading: 100.00%
SARG: Successful report generated on /var/www/html/squid-reports/2020Oct30-2020Oct31
[root@squid ]# yum -y install httpd ###安装Apache
[root@squid squid-reports]# systemctl start httpd ###开启apache
测试
周期性计划任务执行每天生成报告
crontab -e 计划性周期任务
0 0 * * * /bin/bash /usr/local/bin/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)
2.6 ACL访问控制
[root@squid ~]# vim /etc/squid.conf ###编辑主配置
acl asd src 20.0.0.12/24 ###写在acl第一个就行,
http_access deny asd ###调用上面的ip地址和名称拒绝访问,写在http格式的第一个就行
[root@squid ~]# service squid reload ###重启
2.7 错误集
[root@opt~]#yum -y install gcc gcc-c++ make
报错
The other application is: PackageKit
Memory : 193 M RSS (1.6 GB VSZ)
Started: Fri Oct 30 11:16:37 2020 - 06:34 ago
State : Sleeping, pid: 15459
另一个应用程序目前持有yum锁;等待它退出…
另一个应用程序是:PackageKit
内存:193m RSS (1.6 GB VSZ)
开始时间:2020年10月30日星期五11:16:37 - 06:34前
状态:休眠,pid: 15459
解决方案
kill -s 9 15459