天天看点

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

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

文章目录

  • 理论+实验·CDN缓存加速传统模式与透明模式
    • 一、Squid安装介绍
      • 1.1 缓存代理概述
      • 1.2 Squid安装及运行
    • 二、传统代理
      • 2.1 实验环境
      • 2.2 配置Squid服务器
      • 2.3 配置Web服务器
      • 2.4 使用Win 10 配置
    • 三、透明代理
      • 3.1 实验环境
      • 3.2 配置squid服务器
      • 3.3 配置web服务器

一、Squid安装介绍

1.1 缓存代理概述

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

1.2 Squid安装及运行

  • 编译安装Squid 3.4.6
//安装前预安装环境//
[[email protected] ~]# yum -y install gcc gcc-c++
//解压缩源码包//
[[email protected] ~]# tar zxvf squid-3.4.6.tar.gz -C /opt/
//进行编译安装//
[[email protected] ~]# cd /opt/squid-3.4.6/
[[email protected] 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
[[email protected] squid-3.4.6]# make && make install
//优化选项//
[[email protected] squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/
//创建squid非登录账户//
[[email protected] squid-3.4.6]# useradd -M -s /sbin/nologin squid
[[email protected] squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/
[[email protected] 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//
...
[[email protected] squid-3.4.6]# squid -k parse		//验证语法是否有问题//
[[email protected] 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.

[[email protected] squid-3.4.6]# squid					//开启squid//
[[email protected] 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代理服务器===>传统模式//
[[email protected] ~]# yum -y install gcc gcc-c++
[[email protected] ~]# tar zxvf squid-3.4.6.tar.gz -C /opt/
[[email protected] ~]# cd /opt/squid-3.4.6/
[[email protected] 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
[[email protected] squid-3.4.6]# make && make install
[[email protected] squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/
[[email protected] squid-3.4.6]# useradd -M -s /sbin/nologin squid
[[email protected] squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/
[[email protected] 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
...
[[email protected] squid-3.4.6]# squid -k parse		//验证语法//
[[email protected] 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.

[[email protected] squid-3.4.6]# squid					//开启//
[[email protected] squid-3.4.6]# netstat -ntap | grep 3128		//查看是否已开启//
tcp6       0      0 :::3128                 :::*                    LISTEN      42748/(squid-1)     

           
//添加启动脚本//
[[email protected] squid-3.4.6]# cd /etc/init.d/
[[email protected] 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
[[email protected] init.d]# chmod +x squid 
[[email protected] init.d]# chkconfig --add squid 
[[email protected] init.d]# chkconfig --level 35 squid on
           
//传统模式//
[[email protected] 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//
...
[[email protected] init.d]# iptables -F
[[email protected] init.d]# iptables -t nat -F
[[email protected] init.d]# setenforce 0
[[email protected] init.d]# iptables -I INPUT -p tcp --dport 3218 -j ACCEPT
[[email protected] init.d]# pkill squid
[[email protected] init.d]# squid
           

2.3 配置Web服务器

[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum -y install httpd
[[email protected] ~]# systemctl start httpd
           

2.4 使用Win 10 配置

浏览器上面设置手动代理

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

设置好代理之后在浏览器输入"20.0.0.20"web服务器的IP地址

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

查看web服务器的日志文件可以看出是从squid代理服务器来访问的说明实验成功

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

三、透明代理

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服务器

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


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

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

3.3 配置web服务器

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

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

查看日志文件,显示代理服务器的IP就说明实验成功了

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

继续阅读