Web代理的工作机制
- 缓存网页对象,减少重复请求
1. 工作原理:
- 客户端在本地设置一个代理ip,客户机在请求访问163服务器的时候,会首先请求squid的代理服务器,如果代理服务器里面有缓存信息,会直接从缓存服务器发送数据给客户端,如果缓存服务器没有数据的话,squid就会请求163服务器,把数据拉取到squid服务器中,下次客户再次请求相同的数据时,就直接从squid服务器直接访问
- 这种方式也隐藏了可客户的真实的ip,因为客户去访问163服务器的时候,显示的是代理服务器的ip地址
2.代理的基本类型
● 传统代理:适用于Internet,需明确指定服务端
● 透明代理:客户机不需指定代理服务器的地址和端口,而是通过默认路由、防火墙策略将Web访问重定向给代理服务器处理
3.使用代理的好处
- 提高Web访问速度
- 隐藏客户机的真实IP地址
二、squid传统模式
实验设计:一台squid代理服务器,一台Web服务器,一台Client端
1.设置主机名
[root@localhost~]# hostnamectl set-hostname squid
[root@localhost~]# bash
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 ##初始化缓存(需要等一会儿)
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
[root@squid init.d]# chkconfig --level 35 squid on ##35级别自启
[root@squid init.d]# chkconfig --list
[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 -L 查询路由规则
[root@squid init.d]# iptables -F
[root@squid init.d]# iptables -t nat -L 清空nat的路由规则
[root@squid init.d]# iptables -t nat -F 清空nat的表
[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
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://20.0.0.32
[root@localhost ~]# cd /var/log/httpd/
[root@localhost httpd]# ls
access_log error_log
[root@localhost httpd]# cat access_log ##查看访问日志,日志显示来自20.0.0.1的请求
2.指定代理服务器后再次访问Apache
[root@localhost ~]# cd /var/log/httpd/
[root@localhost httpd]# ls
access_log error_log
[root@localhost httpd]# cat access_log ##查看访问日志,日志显示来自代理服务器20.0.0.35的请求
实验squid透明模式
1.配置双网卡
[root@squid ~]# nmcli connection ##查询新增网卡的UUID
NAME UUID TYPE DEVICE
Wired connection 1 beb0b3a7-a26c-3994-b655-ad434e147a2f 802-3-ethernet ens36
ens33 ae861a65-3f85-4d6e-9425-aa8d307a47b4 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=beb0b3a7-a26c-3994-b655-ad434e147a2f ##修改UUID
DEVICE=ens36 ##修改设备名称
IPADDR=20.0.0.2 ##修改ip
NETWORK=255.255.255.0
[root@squid network-scripts]# ifup ens36 ##开启网卡
[root@squid network-scripts]# ifconfig
2.设置路由转发
[root@squid ~]# vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@squid ~]# sysctl -p ##生效内核参数修改
net.ipv4.ip_forward = 1
因为数据是有来有回的,在代理服务器访问web服务器的时候,在数据返回squid服务器的时候,因为不是同一个网段,所以要配置一个回程路由route add -net 20.0.0.0/24 gw 192.168.10.66
操作完之后一定要测试一下,就是用web主机去ping代理服务器上的俩张网卡的ip地址,正常是都能通讯了。
[root@squid ~]# vim /etc/squid.conf
http_port 192.168.10.2:3128 transparent ##对http_port 3128字段进行修改,改成透明模式
[root@squid ~]# service squid stop
[root@squid ~]# service squid start ##重启squid
[root@squid ~]# iptables -t nat -I PREROUTING -i ens33 -s 20.0.0.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
[root@squid ~]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
注释:目标端口443是https
[root@squid network-scripts]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
客户访问web服务器测试
[root@web httpd]# iptables -F 清空表规则
[root@web httpd]# iptables -t nat -F 清空nat表的guize
[root@web httpd]# iptables -L 查看表的规则
[root@web httpd]# iptables -t nat -L 查看nat表的规则
5.查看Apache访问日志
[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