Squid 的作用
1.通过缓存的方式为用户提供web访问加速
2.对用户的web访问进行过滤控制
缓存代理服务器又分为普通代理服务器,透明代理服务器,和反向代理服务器。
普通代理服务
即指标准的,传统的代理服务,需要客户机在浏览器中指定代理服务器的地址,端口
透明代理服务
适合企业的网关主机,客户机不需要指定代理服务器地址,端口等信息,需要设置防火墙策略将客户机的web访问数据转交给代理服务器
反向代理服务
为用户访问网络内网站点提供缓存加速的同时,还能为后端的web服务器进行负载均衡。提高访问效率。
实验,配置缓存代理
环境
服务器 主机名 软件版本 ip地址
squid缓存代理服务器 squid squid-3.4.6 192.168.100.11
web服务器 web httpd 192.168.100.12
squid服务器配置
[root@squid ~]# yum -y install gcc gcc-c++ make #安装编译环境
[root@squid ~]# tar zxvf squid-3.4.6.tar.gz -C /opt
[root@squid ~]# cd /opt/squid-3.4.6/
[root@squid 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
########################
–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 #支持下划线
–enable-poll #poll功能
–enable-gnuregex #支持正则表达式
[root@squid squid-3.4.6]# make -j2 && makeinstall
[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
56 http_access allow all #添加
57 # http_access deny all #注释掉
61 cache_effective_user squid #添加
62 cache_effective_group squid #添加
[root@squid squid-3.4.6]# squid -k parse #检测配置文件语法
[root@squid squid-3.4.6]# squid -z #初始化
[root@squid squid-3.4.6]# 2020/10/30 11:53:36 kid1| Set Current Directory to /usr/local/squid/var/cache/squid
2020/10/30 11:53:36 kid1| Creating missing swap directories
2020/10/30 11:53:36 kid1| No cache_dir stores are configured.
[root@squid squid-3.4.6]# squid #启动服务
[root@squid squid-3.4.6]# netstat -ntap | grep 3128 #查看确认服务启动成功
tcp6 0 0 :::3128 :::* LISTEN 46354/(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"
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|reload|check|restart}"
;;
esac
[root@squid init.d]# chmod +x squid #授予权限
[root@squid init.d]# chkconfig --add squid #添加系统管理
[root@squid init.d]# chkconfig --level 35 squid on #35模式自启动
[root@squid init.d]# systemctl stop squid
[root@squid init.d]# systemctl status squid
[root@squid init.d]# systemctl start squid
普通代理配置
[root@squid init.d]# vim /etc/squid.conf
63 cache_mem 64 MB #添加,指定缓存功能使用的内存空间大小
64 reply_body_max_size 10 MB #允许用户下载的最大文件大小,默认0表示不限制
65 maximum_object_size 4096 KB #允许保持到缓存空间最大对象大小,超过则不被缓存,直接找服务器
[root@squid init.d]# iptables -F
[root@squid init.d]# iptables -t nat -F
[root@squid init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@squid init.d]# service squid reload
[root@squid init.d]# netstat -anutp | grep 3128
tcp6 0 0 :::3128 :::* LISTEN 46354/(squid-1)
web服务器端
[root@web ~]# systemctl stop firewalld
[root@web ~]# yum -y install httpd
[root@web ~]# systemctl start httpd
普通代理配置完成
即可测试
真机浏览器先直接打开192.168.100.12。
正常访问后,我们查看web服务器的access.log日志
[root@web ~]# cd /etc/httpd/logs/
[root@web logs]# vim access_log
我们可以看到访问来源是192.168.100.1,是真机ip地址
打开浏览器 以google浏览器为例
打开设置–高级—系统
在浏览器上设置代理服务器192.168.100.11 端口号3128.
保存后在用浏览器先清缓存在访问192.168.100.12
再次查看access_log,发现访问来源是192.168.100.11
透明代理配置
透明代理访问时,客户端12.0.0.1/24 访问web端192.168.100.12,中间有代理服务器转发
代理服务器双网卡
客户端12.0.0.1---------------12.0.0.11代理服务器192.168.100.11------------------web服务器192.168.100.12
现在客户端和web服务端要互相访问,需要满足下面两条
1.客户端网关指向12.0.0.11或者添加一条静态路由192.168.100.0/24 12.0.0.11
2.web服务器网关指向192.168.100.11或者添加一条静态路由12.0.0.0/24 192.168.100.11
这里我们选择添加静态路由
[root@web ~]# systemctl stop firewalld
[root@web logs]# route add -net 12.0.0.0/24 gw 192.168.100.11
[root@web logs]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.100.2 0.0.0.0 UG 100 0 0 ens33
12.0.0.0 192.168.100.11 255.255.255.0 UG 0 0 0 ens33
192.168.100.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
[root@squid ~]# vim /etc/squid.conf
60 http_port 12.0.0.11:3128 transparent #修改,支持透明代理模式,监听访问入口的网卡地址,即外网口
[root@squid ~]# systemctl stop firewalld
[root@squid ~]# iptables -t nat -I PREROUTING -i ens36 -s 12.0.0.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
[root@squid ~]# iptables -t nat -I PREROUTING -i ens36 -s 12.0.0.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
[root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
!!!注意,web服务器的firewalld要关闭 代理服务器的firewalld也要关闭,关闭后添加iptables的策略
测试,客户机直接访问web服务器,web服务器查看apache日志,查看访问来源地址为代理服务器,实验成功
反向代理配置
重开一个web服务器,名web2 192.168.100.13
[root@web2 ~]# yum -y install httpd
[root@web2 ~]# cd /var/www/html/
[root@web2 html]# vim index.html
this is test2
web服务器192.168.100.12
[root@web ~]# cd /var/www/html/
[root@web html]# vim index.html
this is test 1
代理服务器192.168.100.11
[root@squid ~]# systemctl start firewalld
[root@squid ~]# setenforce 0
[root@squid ~]# iptables -F
[root@squid ~]# iptables -t nat -F
[root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@squid ~]# vim /etc/squid.conf
http_port 192.168.100.11:80 accel vhost vport
cache_peer 192.168.100.12 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
cache_peer 192.168.100.13 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
cache_peer_domain web1 web2 www.yun.com
登陆客户机,开启超级用户,设置密码。
更换超级用户登录,修改hosts文件添加 #只有超级管理员账户才有权限更改此文件
192.168.100.11 www.yun.com
浏览器直接访问www.yun.com,多次刷新,可看到网页在web1和web2上轮询切换。反向代理实验完成
注意:只有在访问域名时才会轮询,直接访问ip地址时不会轮询的