部署Squid 代理服务器(传统、透明代理服务器
Squid 代理服务器
Squid 主要提供缓存加速、应用层过滤控制的功能。
代理的工作机制
代替客户机向网站请求数据,从而可以隐藏用户的真实IP地址。
将获得的网页数据(静态 Web 元素)保存到缓存中并发送给客户机,以便下次请求相同的数据时快速响应。
Squid 代理的类型
传统代理:适用于Internet,需在客户机指定代理服务器的地址和端口。
透明代理:客户机不需指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将Web访问重定向给代理服务器处理。
反向代理:如果 Squid 反向代理服务器中缓存了该请求的资源,则将该请求的资源直接返回给客户端;否则反向代理服务器将向后台的 WEB 服务器请求资源,然后将请求的应答返回给客户端,同时也将该应答缓存在本地,供下一个请求者使用。
安装 Squid 服务
systemctl stop firewalld
systemctl disable firewalld 关闭防火墙
setenforce 0
yum -y install gcc gcc-c++ make
cd /opt
tar zxvf squid-3.5.28.tar.gz
cd /opt/squid-3.5.28
./configure --prefix=/usr/local/squid \
--prefix=/usr/local/squid #指定安装目录路径
--sysconfdir=/etc #指定配置文件路径
--enable-arp-acl #MAC地址管控,防止客户端使用ip欺骗
--enable-linux-netfilter #使用内核过滤
--enable-linux-tproxy #支持透明模式
--enable-async-io=100 #异步1o,提升存储性能
--enable-err-language="Simplify_Chinese" #错误信息的显示语言
--enable-underscore #允许URL中有下划线
--disable-poll \ #关闭默认使用的poll 模式
--enable-epoll \ #开启epoll模式,提升性能
--enable-gnuregex #使用GNU正则表达式
make && make install
ln -s /usr/local/squid/sbin/* /usr/local/sbin/
useradd -M -s /sbin/nologin squid
chown -R squid:squid /usr/local/squid/var/
#/usr/local/squid/var/ 此目录是用于存放缓存文件
修改 Squid 的配置文件
vim /etc/squid.conf
......
#---------56行,插入----------------
http_access allow all #放在 http_access deny all 之前,允许任意客户机使用代理服务,控制规则自上而下匹配
http_access deny all
http_port 3128 #用来指定代理服务监听的地址和端口(默认的端口号为 3128)
#---------61行插入-----------------
cache_effective_user squid #添加,指定程序用户,用来设置初始化、运行时缓存的账号,否则启动不成功
cache_effective_group squid #添加,指定账号基本组
coredump_dir /usr/local/squid/var/cache/squid #指定缓存文件目录
Squid 的运行控制
启动 Squid,第一次启动squid服务时,会自动初始化缓存目录
squid -z #-z 选项用来初始化缓存目录
squid #启动 squid 服务
netstat -anpt | grep "squid"
Squid 服务脚本
vim /etc/init.d/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
chmod +x /etc/init.d/squid
chkconfig --add squid
chkconfig --level 35 squid on
#查看squid服务在哪些级别中自启
chkconfig --list squid
构建传统代理服务器
vim /etc/squid.conf
......
http_access allow all
http_access deny all
http_port 3128
cache_effective_user squid
cache_effective_group squid
#---------63行插入-----------------------------------------------
cache_mem 64 MB
reply_body_max_size 10 MB
maximum_object_size 4096 KB
#---------添加内容解释------------------------------------------
cache_mem 64 MB #指定缓存功能所使用的内存空间大小,便于保持访问较频繁的WEB对象,容量最好为4的倍数,单位为MB,建议设为物理内存的1/4
reply_body_max_size 10 MB #允许用户下载的最大文件大小,以字节为单位,当下载超过指定大小的Web对象时,浏览器的报错页面中会出现“请求或访问太大”的提示默认设置0表示不进行限制
maximum_object_size 4096 KB #允许保存到缓存空间的最大对象大小,以KB为单位,超过大小限制的文件将不被缓存,而是直接转发给用户
#-------------------------------------------------------------------
service squid restart
#或systemctl restart squid
web1
systemctl stop firewalld.service
setenforce 0
yum -y install httpd
systemctl start httpd
netstat -natp | grep 80
最后在客户机配置IP地址进行测试
构建透明代理服务器
Squid服务器配置(ens33:192.168.37.10 、ens37:12.0.0.1)
配置双网卡
直接按照上面步骤安装squid
vim /etc/squid.conf
......
http_access allow all
http_access deny all
#------60行修改添加提供内网服务的IP地址,和支持透明代理选项 transparent
http_port 192.168.37.10:3128 transparent
systemctl restart squid
netstat -natp | grep 3128
开启路由转发,实现本机中不同网段的地址转发
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
sysctl -p
修改防火墙规则
iptables -F
iptables -t nat -F
iptables -t nat -I PREROUTING -i ens33 -s 192.168.37.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
iptables -t nat -I PREROUTING -i ens33 -s 192.168.37.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
Web1(12.0.0.12)
systemctl stop firewalld.service
setenforce 0
vim /etc/sysconfig/network-scripts/ifcfg-ens33
systemctl restart network
ifconfig
yum -y install httpd
systemctl restart httpd.service 安装服务
最后在客户机进行测试