squid代理服务器应用

squid代理服务器应用

缓存代理概述

web代理的工作机制

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

在这里插入图片描述

CDN产品解释

阿里云内容分发网络(Alibaba Cloud Content Delivery Network,简称CDN)将用户源
站资源缓存至阿里云遍布全球的加速节点上。当终端用户请求访问和获取这些资源时,无需
回源,系统将就近调用CDN节点上已经缓存的资源。
在这里插入图片描述

CDN工作原理

LDNS:本地的DNS

CNAM:别名

放回2.2.2.2为cdn地址
在这里插入图片描述

缓存代理概述

  • 代理的基本类型

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

    使用代理的好处

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

传统代理

实验环境:3台服务器
1squid代理服务 192.168.136.88
2httpd 网页服务器192.168.136.60
3win10验证服务器192.168.136.40

安装squit服务

关闭防火墙

[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0

解压squit软件包

[root@promote opt]# tar zxvf squid-3.4.6.tar.gz -C /opt/

配置安装环境

 [root@promote squid-3.4.6]# yum -y install gcc gcc-c++

编译

[root@promote 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-gnuregex

./configure --prefix=/usr/local/squid
–sysconfdir=/etc \ 配置文件目录
–enable-arp-acl \ 启动访问列表
–enable-linux-netfilter \ 内核过滤
–enable-linux-tproxy \ 透明模式
–enable-async-io=100 \ io优化
–enable-err-language=“Simplify_Chinese” \ 报错提示
–enable-underscore \ 支持下划线
–enable-poll \ 功能提升
–enable-gnuregex 正则表达式

进行编译

make && make install

让系统识别命令

[root@localhost squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/

添加指程序用户

[root@localhost squid-3.4.6]# useradd -M -s /sbin/nologin squid

修改文件权限

[root@localhost squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/

配置squid文件

vim /etc/squid.conf
 59 http_port 3128  在下面填写配置
 cache_effective_user squid    #程序用户指向squid
 cache_effective_group squid   #用户组指向squid

在这里插入图片描述

检查语法

[root@promote squid-3.4.6]# squid -k parse 
2020/09/06 16:33:45| Processing: refresh_pattern .		0	20%	4320

初始化目录

[root@promote squid-3.4.6]# squid -z
2020/09/06 16:34:40 kid1| Creating missing swap directories
2020/09/06 16:34:40 kid1| No cache_dir stores are configure

开服务并查看

[root@localhost squid-3.4.6]# squid 
[root@localhost squid-3.4.6]# netstat -ntap | grep squid
tcp6       0      0 :::3128                 :::*                    LISTEN      46595/(squid-1)     

编译优化脚本

[root@localhost squid-3.4.6]# cd /etc/init.d/
[root@localhost 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@localhost init.d]# chmod +x squid 

加入到service管理列表

[root@localhost init.d]# chkconfig --add squid 

开机自启动

[root@localhost init.d]# chkconfig --level 35 squid on

开启服务

[root@localhost init.d]# service squid stop
[root@localhost init.d]# service squid start 
正在启动 squid...
[root@localhost init.d]# netstat -ntap | grep squid
tcp6       0      0 :::3128                 :::*                    LISTEN      46820/(squid-1)  

或者直接在配置文件下直接squid开启

配置传统代理服务器

在squid中配置

[root@localhost init.d]# vim /etc/squid.conf
56 http_access allow all   运行所有来访问

# Squid normally listens to port 3128 在下面配置

在这里插入图片描述

cache_mem  64 MB   #缓存空间 
reply_body_max_size 10 MB   #单个文件大小
maximum_object_size 4096  KB  #设置最大

在这里插入图片描述

添加防火墙规则

[root@localhost init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT

重启服务

 [root@localhost init.d]# service squid reload

httpd 网页服务器配置

[root@localhost httpd]# systemctl start httpd
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd

查看日志(没有发现日志)

[root@localhost ~]# cd /var/log/httpd/
[root@localhost httpd]# cat access_log 

win10上面配置

添加手动代理,开启代理,输入http服务器地址

在这里插入图片描述
在这里插入图片描述

查一下网页服务器日志查看

[root@localhost ~]# cd /var/log/httpd/
[root@localhost httpd]# cat access_log 

在这里插入图片描述

透明代理

为什么要有透明代理因为:在每台客户机设置代理服务器节点麻烦,所以需要透明代理

实验环境:3台服务器(都是仅主机)
1squid代理服务(添加双网卡) 192.168.136.88 192.168.10.1
2httpd 网页服务器192.168.136.60
3win10验证服务器192.168.136.40

模型解释

在这里插入图片描述

squid服务上配置双网卡

root@localhost network-scripts]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
 [root@localhost network-scripts]# vim fcfg-ens36
192.168.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

配置监听端口(#开启透明模式)

http_port 192.168.10.1:3128 transparent  

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

验证语法

[root@localhost network-scripts]# squid -k parse  
2020/09/06 20:55:03| Processing: coredump_dir /usr/local/squid/var/cache/squid
2020/09/06 20:55:03| Processing: refresh_pattern ^ftp:		1440	20%	10080
2020/09/06 20:55:03| Processing: refresh_pattern ^gopher:	1440	0%	1440
2020/09/06 20:55:03| Processing: refresh_pattern -i (/cgi-bin/|\?) 0	0%	0
2020/09/06 20:55:03| Processing: refresh_pattern .		0	20%	4320

防火墙做端口映射

[root@localhost network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128

[root@localhost network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128

localhost init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT

ps:主要目的把80端口映射为3128,因为监听端口无法监听80

httpd 网页服务器设置

在http中设置静态路由

[root@localhost httpd]# route add -net  192.168.10.0/24 gw 192.168.136.88

在web10中配置地址

关闭代理

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

配置网关

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

访问web网站

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

查看访问日志

[root@localhost httpd]# cd /var/log/httpd/
[root@localhost httpd]# cat access_log

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值