理论+实验·CDN缓存加速传统模式
程序员文章站
2022-07-06 12:38:29
...
理论+实验·CDN缓存加速传统模式
文章目录
一、Squid安装介绍
1.1 缓存代理概述
- Web代理的工作机制
- 缓存网页对象,减少重复请求
- 代理的基本类型
- 传统代理:适用于Internet,需明确指定服务端
- 透明代理:客户机不需指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将WEb访问重定向给代理服务器处理
- 使用代理的好处
- 提高Web访问速度
- 隐藏客户机的真实IP地址
1.2 Squid安装及运行
- 编译安装Squid 3.4.6
//安装前预安装环境//
[aaa@qq.com ~]# yum -y install gcc gcc-c++
//解压缩源码包//
[aaa@qq.com ~]# tar zxvf squid-3.4.6.tar.gz -C /opt/
//进行编译安装//
[aaa@qq.com ~]# cd /opt/squid-3.4.6/
[aaa@qq.com squid-3.4.6]# ./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-gunregex
[aaa@qq.com squid-3.4.6]# make && make install
//优化选项//
[aaa@qq.com squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/
//创建squid非登录账户//
[aaa@qq.com squid-3.4.6]# useradd -M -s /sbin/nologin squid
[aaa@qq.com squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/
[aaa@qq.com squid-3.4.6]# vim /etc/squid.conf
...
http_access allow all //允许所有//
#http_access deny all
...
http_port 3128
cache_effective_user squid //管理用户设为squid//
cache_effective_group squid //管理用户组设为squid//
...
[aaa@qq.com squid-3.4.6]# squid -k parse //验证语法是否有问题//
[aaa@qq.com squid-3.4.6]# squid -z //初始化squid//
2020/09/06 10:44:07 kid1| Set Current Directory to /usr/local/squid/var/cache/squid
2020/09/06 10:44:07 kid1| Creating missing swap directories
2020/09/06 10:44:07 kid1| No cache_dir stores are configured.
[aaa@qq.com squid-3.4.6]# squid //开启squid//
[aaa@qq.com squid-3.4.6]# netstat -ntap | grep 3128 //查看端口是否已开启//
tcp6 0 0 :::3128 :::* LISTEN 42748/(squid-1)
二、传统代理
2.1 实验环境
squid 20.0.0.10
web 20.0.0.20
Win10 20.0.0.200
需要准备好squid-3.4.6源码包
2.2 配置Squid服务器
//手工编译安装squid代理服务器===>传统模式//
[aaa@qq.com ~]# yum -y install gcc gcc-c++
[aaa@qq.com ~]# tar zxvf squid-3.4.6.tar.gz -C /opt/
[aaa@qq.com ~]# cd /opt/squid-3.4.6/
[aaa@qq.com squid-3.4.6]# ./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-gunregex
[aaa@qq.com squid-3.4.6]# make && make install
[aaa@qq.com squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/
[aaa@qq.com squid-3.4.6]# useradd -M -s /sbin/nologin squid
[aaa@qq.com squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/
[aaa@qq.com squid-3.4.6]# vim /etc/squid.conf
...
http_access allow all //允许所有//
#http_access deny all
...
http_port 3128
cache_effective_user squid //管理用户//
cache_effective_group squid
...
[aaa@qq.com squid-3.4.6]# squid -k parse //验证语法//
[aaa@qq.com squid-3.4.6]# squid -z //初始化代理服务器//
2020/09/06 10:44:07 kid1| Set Current Directory to /usr/local/squid/var/cache/squid
2020/09/06 10:44:07 kid1| Creating missing swap directories
2020/09/06 10:44:07 kid1| No cache_dir stores are configured.
[aaa@qq.com squid-3.4.6]# squid //开启//
[aaa@qq.com squid-3.4.6]# netstat -ntap | grep 3128 //查看是否已开启//
tcp6 0 0 :::3128 :::* LISTEN 42748/(squid-1)
//添加启动脚本//
[aaa@qq.com squid-3.4.6]# cd /etc/init.d/
[aaa@qq.com init.d]# vim 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 -ntap | grep squid &> /dev/null
if [ $? -eq 0 ]
then
echo "squid is runing"
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 -ntap | grep squid
else
echo "squid is not runing"
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|restart|reload|check}"
;;
esac
[aaa@qq.com init.d]# chmod +x squid
[aaa@qq.com init.d]# chkconfig --add squid
[aaa@qq.com init.d]# chkconfig --level 35 squid on
//传统模式//
[aaa@qq.com init.d]# vim /etc/squid.conf
...
http_port 3128
cache_effective_user squid
cache_effective_group squid
cache_mem 64 MB //缓存空间//
reply_body_max_size 10 MB //允许用户下载的最大文件//
maximum_object_size 4096 KB //存储文件的大小最大时4096KB//
...
[aaa@qq.com init.d]# iptables -F
[aaa@qq.com init.d]# iptables -t nat -F
[aaa@qq.com init.d]# setenforce 0
[aaa@qq.com init.d]# iptables -I INPUT -p tcp --dport 3218 -j ACCEPT
[aaa@qq.com init.d]# pkill squid
[aaa@qq.com init.d]# squid
2.3 配置Web服务器
[aaa@qq.com ~]# systemctl stop firewalld
[aaa@qq.com ~]# setenforce 0
[aaa@qq.com ~]# yum -y install httpd
[aaa@qq.com ~]# systemctl start httpd
2.4 使用Win 10 配置
浏览器上面设置手动代理
设置好代理之后在浏览器输入"20.0.0.20"web服务器的IP地址
查看web服务器的日志文件可以看出是从squid代理服务器来访问的说明实验成功
三、透明代理
3.1 实验环境
squid 20.0.0.10|20.0.10.1
web 20.0.0.20
Win10 20.0.0.200
需要准备好squid-3.4.6源码包
需要在squid服务器设置双网卡
ens33: 20.0.0.10
ens36: 20.0.10.1
3.2 配置squid服务器
//配置双网卡//
[aaa@qq.com ~]# cd /etc/sysconfig/network-scripts/
[aaa@qq.com network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
[aaa@qq.com network-scripts]# vim ifcfg-ens36
...
NAME=ens36 //改成ens36//
UUID=4ad6e1fd-8713-4e99-9a0b-08f11c6ed8be //UUID删除//
DEVICE=ens36 //改成ens36//
ONBOOT=yes
IPADDR=20.0.10.1
NETMASK=255.255.255.0
[aaa@qq.com network-scripts]# service network restart
Restarting network (via systemctl): [ 确定 ]
[aaa@qq.com network-scripts]# vim /etc/sysctl.conf
...
net.ipv4.ip_forward=1
[aaa@qq.com network-scripts]# sysctl -p
net.ipv4.ip_forward = 1
[aaa@qq.com network-scripts]# vim /etc/squid.conf
...
# Squid normally listens to port 3128
http_port 20.0.10.1:3128 transparent
...
[aaa@qq.com network-scripts]# squid -k parse
[aaa@qq.com network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 20.0.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
[aaa@qq.com network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 20.0.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
[aaa@qq.com network-scripts]# iptables -I INPUT -p tcp --dport 3218 -j ACCEPT
[aaa@qq.com network-scripts]# pkill squid
[aaa@qq.com network-scripts]# squid
3.3 配置web服务器
之前浏览器设置过代理设置的话这里需要将其关闭
查看日志文件,显示代理服务器的IP就说明实验成功了
推荐阅读