热门!!Squid缓存加速——传统及透明模式服务搭建

一、缓存代理概述

官方地址:http://www.squid-cache.org/
软件下载地址:http://www.squid-cache.org/Versions/

1. Web代理的工作机制

  • 缓存网页对象,减少重复请求
    在这里插入图片描述

2.代理的基本类型

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

3.使用代理的好处

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

二、squid传统模式

实验设计:一台squid代理服务器,一台Web服务器,一台Client端
实验拓扑:

在这里插入图片描述

【squid服务器】

1.设置主机名

[root@localhost~]# hostnamectl set-hostname squid
[root@localhost~]# su

2.编译安装squid

[root@squid~]# yum install gcc gcc-c++ -y
[root@squid~]# tar zxvf squid-3.4.6.tar.gz -C /opt
[root@squid~]# cd /opt/squid-3.4.6/
[root@squidsquid-3.4.6]# ./configure \
--prefix=/usr/local/squid \          ##安装路径
--sysconfdir=/etc \        		   ##配置文件目录
--enable-arp-acl \      		   ##支持acl访问控制列表
--enable-linux-netfilter \  		   ##支持网络筛选
--enable-linux-tproxy \  		   ##支持透明
--enable-async-io=100 \ 			   ## I/O优化
--enable-err-language="Simplify_Chinese" \   ##报错显示简体中文
--enable-underscore \      			 ##支持下划线
--enable-poll \	    		##关闭默认使用poll模式,开启epoll模式提提升性能
--enable-gnuregex   		##支持正则表达
[root@squid squid-3.4.6]# make -j3 && make install

3.优化路径

[root@squid squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/	##创建命令软连接,方便系统识别'

4.创建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/ ##设置目录的属主和属组

5.修改squid配置

[root@squid squid-3.4.6]# vim /etc/squid.conf
http_access allow all     ##添加此行允许所有访问(565行左右)
#http_access deny all    ##将拒绝所有注释掉(不注释也一样,上面允许所有,这个就失去了意义)
http_port 3128         ##默认是3128端口(可以不用改)
cache_effective_user squid   ##添加指定用户squid (可以在60行下插入这两行) 
cache_effective_group squid 	##添加指定组 squid
……
[root@squid squid-3.4.6]# squid -k parse   ##检查配置文件中的语法问题
[root@squid squid-3.4.6]# squid -z   ##初始化缓存(需要等一会儿)
############################################################################################
squid -z 初始化错误,提示没有缓存目录
[root@squid squid-3.4.6]#  squid -z
[root@squid squid-3.4.6]# 2020/10/30 19:12:28 kid1| Set Current Directory to /usr/local/squid/var/cache/squid
2020/10/30 19:12:28 kid1| Creating missing swap directories
2020/10/30 19:12:28 kid1| No cache_dir stores are configured.
解决办法
去掉”cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256  ”前面的注释,大概62行左右,否则无法squid -z 初始化
[root@squid squid-3.4.6]#  vim /etc/squid.conf
cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256   ##去掉注释
#############################################################################################

6.开启服务

[root@squid squid]# squid 
[root@squid squid]# netstat -anupt |grep 3128   ##查看监听端口
tcp6       0      0 :::3128                 :::*                    LISTEN      68246/(squid-1)     

7.设置系统服务项

[root@squid squid]# cd /etc/init.d/
[root@squid init.d]# vi 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 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 -ntap | 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|reload|status|check|restart}"
                ;;
esac
[root@squid init.d]# chmod +x squid 
[root@squid init.d]# chkconfig --add squid    ##加入到service管理
[root@squid init.d]# chkconfig --list squid
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
     ……省略部分
squid          	0:off	1:off	2:on	3:on	4:on	5:on	6:off

[root@squid init.d]# chkconfig --level 35 squid on    ##35级别自启
[root@squid init.d]# chkconfig --list

……省略部分
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
squid          	0:off	1:off	2:on	3:on	4:on	5:on	6:off     ##加入到启动项
[root@squid init.d]# service squid stop   ##关闭squid
[root@squid init.d]# netstat -anupt |grep 3128    ##监听端口已关闭
[root@squid init.d]# service squid start   ##启动squid
正在启动 squid....
[root@squid init.d]# netstat -anupt |grep 3128
tcp6       0      0 :::3128                 :::*                    LISTEN      68446/(squid-1)     

8.传统代理服务器需要配置的选项

[root@squid init.d]# vim /etc/squid.conf   ## 插入以下几行
cache_mem 64 MB    ##指定缓存使用的空间大小,容量最好为4的倍数
reply_body_max_size 10 MB    ##允许用户下载的最大文件大小,以字节为单位,默认设置为0表示不限制    
maximum_object_size 4096 KB     ##允许保存到缓存空间的最大对象大小,以KB为单位,超过限制不会缓存,直接转到web端
[root@squid init.d]# service squid reload   ##重新加载服务

9.放通防火墙规则

[root@squid init.d]# iptables -F
[root@squid init.d]# setenforce 0
setenforce: SELinux is disabled
[root@squid init.d]# iptables -I INPUT -p tcp --dport 3218 -j ACCEPT 
[root@squid init.d]# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:smartpackets

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

【Web服务器】

1.安装Apache服务

[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd

在这里插入图片描述

【客户端浏览器访问】访问验证

1.直接访问Apache http://192.168.10.20

[root@localhost ~]# cd /var/log/httpd/
[root@localhost httpd]# ls
access_log error_log
[root@localhost httpd]# cat access_log ##查看访问日志,日志显示来自192.168.10.1的请求
192.168.10.1 - - [30/Oct/2020:19:26:00 +0800] “GET / HTTP/1.1” 403 4897 “-” “Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko”
192.168.10.1 - - [30/Oct/2020:19:26:00 +0800] “GET /noindex/css/bootstrap.min.css HTTP/1.1” 200 19341 “http://192.168.10.20/” “Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko”
……省略部分

2.指定代理服务器后再次访问Apache

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

3.查看web服务器的访问日志

请求是来自squid的

[root@localhost ~]# cd /var/log/httpd/
[root@localhost httpd]# ls
access_log  error_log
[root@localhost httpd]# cat access_log  ##查看访问日志,日志显示来自代理服务器192.168.10.10的请求
……省略部分
192.168.10.10 - - [30/Oct/2020:19:39:58 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://192.168.10.20/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
192.168.10.10 - - [30/Oct/2020:19:39:58 +0800] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1" 404 238 "http://192.168.10.20/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
192.168.10.10 - - [30/Oct/2020:19:39:58 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "http://192.168.10.20/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"

三、squid透明模式

Squid透明模式挂载原理

在这里插入图片描述

1.实验拓扑

基于上面传统模式上进行配置修改
在这里插入图片描述

2.配置双网卡

[root@squid ~]# nmcli connection   ##查询新增网卡的UUID
NAME                UUID                                  TYPE            DEVICE 
ens33               decf0e5b-f858-4432-9518-eda561739439  802-3-ethernet  ens33  
virbr0              fcc9e96f-22f7-48a9-8e47-409a6961bef3  bridge          virbr0 
Wired connection 1  282144f0-c96e-309a-9557-b735e4f84518  802-3-ethernet  --    
[root@squid ~]# cd /etc/sysconfig/network-scripts/
[root@squid network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
[root@squid network-scripts]# vi ifcfg-ens36
NAME=ens36     ##修改网卡名称
UUID=282144f0-c96e-309a-9557-b735e4f84518   ##修改UUID
DEVICE=ens36    ##修改设备名称
IPADDR=20.0.0.10   ##修改ip
GATEWAY=20.0.0.2   ##修改网关
[root@squid network-scripts]# ifup ens36   ##开启网卡
[root@squid network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.10  netmask 255.255.255.0  broadcast 192.168.10.255
       ……省略部分
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 20.0.0.2  netmask 255.255.255.0  broadcast 20.0.0.255
……省略部分

3.设置路由转发

[root@squid ~]# vi /etc/sysctl.conf 
net.ipv4.ip_forward = 1
[root@squid ~]# sysctl -p    ##生效内核参数修改
net.ipv4.ip_forward = 1

4.修改squid.conf配置

[root@squid ~]# vim /etc/squid.conf
http_port 192.168.10.1:3128 transparent   ##对http_port 3128字段进行修改,改成透明模式
[root@squid ~]# service squid stop 
[root@squid ~]# service squid start    ##重启squid

5.配置iptables转发规则

[root@squid ~]# iptables -t nat -F
[root@squid ~]# iptables -F
[root@squid ~]# iptables -t nat -I PREROUTING -i ens33 -s 20.0.0.0/24 -p tcp --dport 80 -j REDIRECT --to 3128   ##原地址是20.0.0.0网段80端口转换成3128端口发送出去
[root@squid ~]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
[root@squid network-scripts]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT   ##允许入口方向访问3128端口

6.在Web服务器端配置静态回程路由

[root@squid ~]# route add -net 20.0.0.0/24 gw 192.168.10.10  
                ##添加路由,来自20.0.0.0网段的数据往192.168.10.10接口(Squid外网口)发送
[root@squid ~]# route -n    ##路由条目添加成功
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.10.2    0.0.0.0         UG    100    0        0 ens33
20.0.0.0        192.168.10.10   255.255.255.0   UG    0      0        0 ens33
192.168.10.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

7.客户端(20.0.0.30)访问web服务器(192.168.10.20)测试

直接访问,不需要在浏览器上配置代理
在这里插入图片描述

8.查看Apache访问日志

请求是来自squid代理服务器的

[root@localhost ~]# tail -f /var/log/httpd/access_log   ##请求是来自squid服务器的地址
192.168.10.10 - - [30/Oct/2020:23:37:16 +0800] "GET /noindex/css/fonts/Bold/OpenSans-Bold.woff HTTP/1.1" 404 239 "http://192.168.10.20/noindex/css/open-sans.css" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
192.168.10.10 - - [30/Oct/2020:23:37:16 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://192.168.10.20/noindex/css/open-sans.css" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
192.168.10.10 - - [30/Oct/2020:23:37:16 +0800] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1" 404 238 "http://192.168.10.20/noindex/css/open-sans.css" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
192.168.10.1 - - [30/Oct/2020:23:37:40 +0800] "-" 408 - "-" "-"
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值