缓存代理概述
Web代理的工作机制
- 缓存网页对象,减少重复请求
代理的基本类型
- 传统代理:适用于Internet,需明确指定服务端
- 透明代理:客户机不需指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将Web访问重定向给代理服务器处理
使用代理的好处
- 提高Web访问速度
- 隐藏客户机的真实IP地址
案例
20.0.0.21(squid)
[root@localhost ~]# hostnamectl set-hostname squid
[root@localhost ~]# yum -y install gcc gcc-c++ make ##安装编译工具
[root@squid ~]# tar zxvf squid-3.4.6.tar.gz
[root@squid ~]# cd 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 \ #功能的提升
--enable-gnuregex #支持正则表达式
[root@squid squid-3.4.6]# make
[root@squid squid-3.4.6]# 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
# And finally deny all other access to this proxy
http_access allow all #deny 改成allow
# Squid normally listens to port 3128 #下面插入
http_port 3128
cache_effective_user squid #添加 指定程序用户
cache_effective_group squid #添加 指定账号基本组
coredump_dir /usr/local/squid/var/cache/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 #启动服务
[root@squid squid-3.4.6]# netstat -ntap |grep 3128
tcp6 0 0 :::3128 :::* LISTEN 48744/(squid-1)
[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" ##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
[root@squid init.d]# chmod +x squid #添加执行权限
[root@squid init.d]# chkconfig --add squid #添加名称
传统代理服务器
[root@squid init.d]# vim /etc/squid.conf
# Squid normally listens to port 3128 #以下添加
cache_mem 64 MB #指定缓存的空间大小
reply_body_max_size 10 MB #允许单个下载文件最大的大小,默认设置0表示不进行限制
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 #重载squid
[root@squid init.d]# netstat -natp |grep 3128
tcp6 0 0 :::3128 :::* LISTEN 49004/(squid-1)
遇到的问题:
问题一:
iptables: No chain/target/match by that name.
解决方式(一):安装iptables
[root@squid init.d]# yum -y install iptables
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
软件包 iptables-1.4.21-18.0.1.el7.centos.x86_64 已安装并且是最新版本
无须任何处理
解决方式(二):规则需要大写
问题二:
无法进行代理
解决方式:
[root@squid squid-3.4.6]# vim /etc/squid.conf
# And finally deny all other access to this proxy
http_access allow all
20.0.0.22(web)
[root@localhost ~]# hostnamectl set-hostname web
[root@web ~]# systemctl stop firewalld.service
[root@web ~]# setenforce 0
[root@web ~]# yum -y install httpd
[root@web ~]# systemctl start httpd.service
在真机20.0.0.11测试
未做代理(20.0.0.11)
输入20.0.0.22
过程:20.0.0.11————>20.0.0.22
20.0.0.11 - - [30/Oct/2020:14:26:30 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://20.0.0.22/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36"
做代理后(20.0.0.21代理服务器)
输入20.0.0.22
过程:20.0.0.11————>20.0.0.21————>20.0.0.22
20.0.0.21 - - [30/Oct/2020:14:27:38 +0800] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36"
透明代理服务器
配置双网卡内网ens33 外网ens37
客户端20.0.0.100---------------20.0.10.1代理服务器20.0.0.21------------------web服务器20.0.0.22
现在客户端和web服务端要互相访问,需要满足下面两条
1.客户端网关指向20.0.10.1或者添加一条静态路由20.0.0.0/24 20.0.10.1
2.web服务器网关指向20.0.0.21或者添加一条静态路由20.0.10.0/24 20.0.0.21
识别不到第二个网卡的时候用systemctl start Network
[root@squid network-scripts]# vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@squid network-scripts]# sysctl -p
net.ipv4.ip_forward = 1
[root@squid init.d]# vim /etc/squid.conf
# Squid normally listens to port 3128
http_port 20.0.10.1:3128 transparent #20.0.10.1客户机的网关
[root@squid init.d]# service squid stop
[root@squid init.d]# service squid start
[root@squid init.d]# iptables -t nat -I PREROUTING -i ens37 -s 20.0.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
[root@squid init.d]# iptables -t nat -I PREROUTING -i ens37 -s 20.0.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
[root@squid init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@squid init.d]# netstat -natp |grep 3128
tcp 0 0 20.0.10.1:3128 0.0.0.0:* LISTEN 54000/(squid-1)