squid代理服务
- 一、缓存代理概述
-
- 1.1 web代理的工作机制
- 1.2 阿里云CDN产品介绍与工作原理
- 1.3 代理的基本类型
- 1.4 使用代理的好处
- 二、部署squid传统代理
-
- 2.1 实验环境
- 2.2 实验拓扑![在这里插入图片描述](https://img-blog.csdnimg.cn/20200908002204337.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQ3MTYxMjk1,size_16,color_FFFFFF,t_70#pic_center)
- 2.2 实验步骤
- 三、部署squid透明代理
-
- 3.1 实验环境
- 3.2 实验拓扑![在这里插入图片描述](https://img-blog.csdnimg.cn/20200908003228130.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQ3MTYxMjk1,size_16,color_FFFFFF,t_70#pic_center)
- 3.3 实验步骤
一、缓存代理概述
1.1 web代理的工作机制
- 缓存网页对象,减少重复请求
- 它是这样实现其功能的,接受来自人们需要下载的目标(object)的请求并适当地处理这些请求
- 也就是说,如果一个人想下载一web页面,他请求Squid为他取得这个页面。Squid随之连接到远程服务器(比如:http://squid.nlanr.net/)并向这个页面发出请求
- 然后,Squid显式地聚集数据到客户端机器,而且同时复制一份。当下一次有人需要同一页面时,Squid可以简单地从磁盘中读到它,那样数据迅即就会传输到客户机上。
缓存加速CDN——squid代理服务器应用(传统代理,透明代理)一、缓存代理概述二、部署squid传统代理三、部署squid透明代理 缓存加速CDN——squid代理服务器应用(传统代理,透明代理)一、缓存代理概述二、部署squid传统代理三、部署squid透明代理
1.2 阿里云CDN产品介绍与工作原理
1.3 代理的基本类型
传统代理:适用于Internet,需明确指定服务端
透明代理:客户机不许指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将web访问重定向给代理服务器好处
1.4 使用代理的好处
提高web访问速度
隐藏客户机的真实ip地址
二、部署squid传统代理
2.1 实验环境
- VMware软件
- XSHELL
- 一台centos7虚拟机作为squid服务器,IP地址为:192.168.200.60
- 一台centos7虚拟机作为web服务器,IP地址为:192.168.200.80
- 一台win10虚拟机作为client测试机,IP地址为:192.168.200.11
2.2 实验拓扑
缓存加速CDN——squid代理服务器应用(传统代理,透明代理)一、缓存代理概述二、部署squid传统代理三、部署squid透明代理
2.2 实验步骤
1、配置squid代理服务器
[[email protected] ~]# hostnamectl set-hostname squid
[[email protected] ~]# su
[[email protected] ~]# tar zxvf squid-3.4.6.tar.gz -C /opt/
[[email protected] ~]# cd /opt/
[[email protected] opt]# yum -y install gcc gcc-c++
[[email protected] opt]# cd squid-3.4.6/
[[email protected] 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 \ 支持URL中下划线
> --enable-poll \ poll功能模块
> --enable-gnuregex 正则表达式
[[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
56 http_access allow all 允许所有
57 #http_access deny all 注释
60 http_port 3128
61 cache_effective_user squid 添加管理用户
62 cache_effective_group squid 添加管理组
[[email protected] squid-3.4.6]# squid -k parse 检查配置文件语法
[[email protected] squid-3.4.6]# squid -z 初始化缓存目录
[[email protected] squid-3.4.6]# squid 启动
[[email protected] squid-3.4.6]# netstat -ntap | grep 3128
tcp6 0 0 :::3128 :::* LISTEN 125515/(squid
[[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" '//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 '//关闭squid'
rm -rf $PID &> /dev/null '//删除PID文件'
;;
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
[[email protected] init.d]# chmod +x squid
[[email protected] init.d]# ls
functions netconsole network README 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
63 cache_mem 64 MB
64 reply_body_max_size 10 MB
65 maximum_object_size 4096 KB
[[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 3128 -j ACCEPT
[[email protected] ~]# cd /var/log/httpd/
[[email protected] httpd]# ls
access_log error_log
[[email protected] httpd]# cat access_log
2、设置web服务器
[[email protected] ~]# hostnamectl set-hostname web
[[email protected] ~]# su
[[email protected] ~]# yum -y install httpd
[[email protected] ~]# systemctl start httpd.service
[[email protected] ~]# netstat -antp | grep httpd
tcp6 0 0 :::80 :::* LISTEN 90278/httpd
[[email protected] ~]# iptables -F
[[email protected] ~]# setenforce 0
使用win10服务器,打开代理,输入ip 192.168.200.60和端口3128
访问Apache成功
查看Apache服务器日志
[[email protected] httpd]# cat access_log
192.168.200.60 - - [06/Sep/2020:14:13:35 +0800] "GET /noindex/css/fonts/Bold/OpenSans-Bold.woff HTTP/1.1" 404 239 "http://192.168.200.80/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363"
192.168.200.60 - - [06/Sep/2020:14:13:35 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://192.168.200.80/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363"
192.168.200.60 - - [06/Sep/2020:14:13:35 +0800] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1" 404 238 "http://192.168.200.80/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363"
三、部署squid透明代理
squid服务器添加网卡且上网卡都改成仅主机模式,web服务器改成仅主机模式,win10也改成仅主机模式
3.1 实验环境
- VMware软件
- XSHELL
- 一台centos7虚拟机作为squid服务器,IP地址为:192.168.200.60 192.168.10.1
- 一台centos7虚拟机作为web服务器,IP地址为:192.168.200.80
- 一台win10虚拟机作为client测试机,IP地址为:192.168.10.10
3.2 实验拓扑
缓存加速CDN——squid代理服务器应用(传统代理,透明代理)一、缓存代理概述二、部署squid传统代理三、部署squid透明代理
3.3 实验步骤
1、配置squid服务器,添加双网卡并配置,配置为仅主机
[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
NAME=ens36
DEVICE=ens36
ONBOOT=yes
IPADDR=192.168.10.1
NETMASK=255.255.255.0
[[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]# service network restart
Restarting network (via systemctl): [ 确定 ]
[[email protected] network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.200.60 netmask 255.255.255.0 broadcast 192.168.200.255
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.1
[[email protected] network-scripts]# vim /etc/squid.conf
60 http_port 192.168.10.1:3128 transparent //如果监听到这个端口就改成透明模式
[[email protected] network-scripts]# squid -k parse 检验语法
[[email protected] network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128 //80端口重定向3128端口
[[email protected] network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128 //80端口重定向3128端口
[[email protected] network-scripts]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[[email protected] network-scripts]# service squid restart
2、Apache服务器的配置为仅主机模式,配置静态路由指网关
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=192.168.200.80
GATEWAY=192.168.200.1
NETMASK=255.255.255.0
[[email protected] httpd]# route add -net 192.168.10.0/24 gw 192.168.200.60 //配置静态路由指网关
3、win10改成仅主机,访问web端,不需要设置代理,自动通过squid代理加速