理论+实验·CDN缓存加速传统模式与透明模式

理论+实验·CDN缓存加速传统模式与透明模式

一、Squid安装介绍

1.1 缓存代理概述

  • Web代理的工作机制
    • 缓存网页对象,减少重复请求
  • 代理的基本类型
    • 传统代理:适用于Internet,需明确指定服务端
    • 透明代理:客户机不需指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将WEb访问重定向给代理服务器处理
  • 使用代理的好处
    • 提高Web访问速度
    • 隐藏客户机的真实IP地址

1.2 Squid安装及运行

  • 编译安装Squid 3.4.6
//安装前预安装环境//
[root@squid ~]# yum -y install gcc gcc-c++
//解压缩源码包//
[root@squid ~]# tar zxvf squid-3.4.6.tar.gz -C /opt/
//进行编译安装//
[root@squid ~]# cd /opt/squid-3.4.6/
[root@squid 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
[root@squid squid-3.4.6]# make && make install
//优化选项//
[root@squid squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/
//创建squid非登录账户//
[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 all			//允许所有//
#http_access deny all
...
http_port 3128
cache_effective_user squid		//管理用户设为squid//
cache_effective_group squid		//管理用户组设为squid//
...
[root@squid squid-3.4.6]# squid -k parse		//验证语法是否有问题//
[root@squid 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.

[root@squid squid-3.4.6]# squid					//开启squid//
[root@squid 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代理服务器===>传统模式//
[root@squid ~]# yum -y install gcc gcc-c++
[root@squid ~]# tar zxvf squid-3.4.6.tar.gz -C /opt/
[root@squid ~]# cd /opt/squid-3.4.6/
[root@squid 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
[root@squid squid-3.4.6]# make && 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 all			//允许所有//
#http_access deny all
...
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				//初始化代理服务器//
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.

[root@squid squid-3.4.6]# squid					//开启//
[root@squid squid-3.4.6]# netstat -ntap | grep 3128		//查看是否已开启//
tcp6       0      0 :::3128                 :::*                    LISTEN      42748/(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"
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
[root@squid init.d]# chmod +x squid 
[root@squid init.d]# chkconfig --add squid 
[root@squid init.d]# chkconfig --level 35 squid on
//传统模式//
[root@squid 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//
...
[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 3218 -j ACCEPT
[root@squid init.d]# pkill squid
[root@squid init.d]# squid

2.3 配置Web服务器

[root@web ~]# systemctl stop firewalld
[root@web ~]# setenforce 0
[root@web ~]# yum -y install httpd
[root@web ~]# 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服务器

//配置双网卡//
[root@squid ~]# cd /etc/sysconfig/network-scripts/
[root@squid network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
[root@squid 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
[root@squid network-scripts]# service network restart
Restarting network (via systemctl):                        [  确定  ]
[root@squid network-scripts]# vim /etc/sysctl.conf
...
net.ipv4.ip_forward=1
[root@squid network-scripts]# sysctl -p
net.ipv4.ip_forward = 1


[root@squid network-scripts]# vim /etc/squid.conf
...
# Squid normally listens to port 3128
http_port 20.0.10.1:3128 transparent
...

[root@squid network-scripts]# squid -k parse
[root@squid network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 20.0.10.0/24 -p tcp --dport 80 -j REDIRECT  --to 3128
[root@squid network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 20.0.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
[root@squid network-scripts]# iptables -I INPUT -p tcp --dport 3218 -j ACCEPT
[root@squid network-scripts]# pkill squid
[root@squid network-scripts]# squid

3.3 配置web服务器

之前浏览器设置过代理设置的话这里需要将其关闭

在这里插入图片描述

查看日志文件,显示代理服务器的IP就说明实验成功了
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值