Squid代理服务器应用(传统模式与透明模式实操配置)

前言

又俗称CDN,CDN全称是Content Delivery Network,即内容分发网络。CDN是构建在现有网络基础之上的智能虚拟网络,依靠部署在各地的边缘服务器,通过中心平台的负载均衡、内容分发、调度等功能模块,使用户就近获取所需内容,降低网络拥塞,提高用户访问响应速度和命中率。CDN的关键技术主要有内容存储和分发技术。

一:缓存代理概述

1.1:Web代理的工作机制

  • 缓存网页对象,减少重复请求

  • 我们都知道只有通过访问web服务器才能获取到网页信息,如果访问量在几百、几千…服务器还有可能承受的住,如果访问并发量在上万可能性能再好的服务器也顶不住这巨大的访问请求。所以为了减缓服务器的压力,就有了squid代理服务器。也就是说用户不是直接访问web服务器,而是通过访问squid代理服务器,代理去访问web,将网页缓存在代理中。这样用户就避免了直接访问web服务器,也会大大减少由于大量访问所导致的web端的延时。
    在这里插入图片描述

1.2:代理的基本类型

  • 传统代理:适用于Internet,需明确指定服务端
  • 透明代理:客户机不需指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将Web访问重定向给代理服务器处理

1.3:使用代理的好处

  • 提高Web访问速度
  • 隐藏客户机的真实IP地址

二:搭建squid实验配置

2.1:实验环境

  • 三台centos 7
    • 一台做squid代理服务器:20.0.0.51
    • 一台做web服务器:20.0.0.52
    • 一台做客户端:20.0.0.20

2.2:实验目的

  • 通过查看日志文件可以看到地址的转换,从客户端IP转换成squid代理服务器地址,进而隐藏源IP地址

2.3:实验步骤

  • 准备squid软件包
  • 解压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]# yum install gcc gcc-c++ -y
  • 编译安装
[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 \'支持下划线字符'
--enable-poll \'poll功能模块'
--enable-gnuregex '支持正则'
[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
'在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 '启动服务'
  • 配置squid启动脚本
[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 -natp | grep squid  &> /dev/null
            if [ $? -eq 0 ]
            then
 	echo "squid is running"
            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 -natp | 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|status|reload|check|restart}"
          ;;
esac
  • 添加脚本权限,增加服务和开启服务
[root@squid init.d]# chmod +x squid
[root@squid init.d]# ls
functions  netconsole  network  README  squid
[root@squid init.d]# chkconfig --add squid
[root@squid init.d]# chkocnfig --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...
[root@squid init.d]# netstat -ntap |grep 3128
tcp6       0      0 :::3128                 :::*                    LISTEN      47369/(squid-1)     

2.4:Squid传统代理模式配置

  • 修改主配置文件
vim /etc/squid.conf
http_access allow all   
http_access deny all   '在这一行上添加'
'在3128端口下面加'
cache_mem 64 MB
reply_body_max_size 10 MB
maximum_object_size 4096 KB
  • 清空防火墙规则和设置防火墙规则
[root@squid init.d]# iptables -F
[root@squid init.d]# setenforce 0
[root@squid init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@squid init.d]# service squid reload    '重新加载服务'

2.5:客户端配置

  • 需要制定代理服务器地址和端口

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nLKRzg39-1599382323550)(C:\Users\kevin\AppData\Roaming\Typora\typora-user-images\image-20200906153418000.png)]

在这里插入图片描述

2.6:Web服务器而配置

[root@web ~]# yum install httpd -y
[root@web ~]# systemctl start httpd
[root@web ~]# netstat -ntap | grep 80
[root@web ~]# cd /var/log/httpd/
[root@web httpd]# cat access_log   '查看日志'
  • 客户端访问web服务器地址才会产生日志

在这里插入图片描述

在这里插入图片描述

2.7:透明模式配置

  • 原理图

在这里插入图片描述

  • 在配置透明模式中不需要指定地址和端口,这边要去掉

在这里插入图片描述

  • 配置双网卡,设置网卡参数,都选择仅主机模式
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
[root@localhost network-scripts]# vim ifcfg-ens36
IPADDR=20.0.10.1
  • 开启路由转发功能
[root@localhost network-scripts]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
[root@localhost network-scripts]# sysctl -p
net.ipv4.ip_forward = 1
  • 配置squid支持透明代理
[root@localhost network-scripts]# vim /etc/squid.conf
http_port 20.0.10.1:3128 transparent
[root@localhost network-scripts]# squid -k parse
  • 设置防火墙规则
[root@localhost network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 20.0.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128  '80表示http端口'
[root@localhost network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 20.0.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128   '443表示https的端口'
iptables -I INPUT -p tcp --dport 3128 -j ACCEPT   '如过之前没有做过,需要敲,这边是基于上一个实验,不需要'
  • 重新加载服务
[root@localhost network-scripts]# service squid reload
[root@localhost network-scripts]# netstat -ntap | grep squid
tcp        0      0 20.0.10.1:3128          0.0.0.0:*               LISTEN      47369/(squid-1)   
  • 查看web服务器日志
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_47219942/article/details/108433500