缓存的概述
web 代理的工作机制
缓存网页对象, 减少重复请求
代理的基本类型
传统代理: 适用于 Internet, 需明确指定服务端
透明代理: 客户机不需要指定代理服务 器的地址和端口, 而是通过默认路由, 防火墙策略将 Web 访问重定向给代理服务器处理
使用代理的好处
提高 Web 访问速度
隐藏客户机的真实 IP 地址
传统代理实验搭建
实验环境
squid 服务器 IP 地址: 192.168.80.179
Web 服务器 IP 地址: 192.168.80.151
client 测试机 IP 地址: 192.168.80.135
在 squid 服务器上安装 squid 服务
- [[email protected] ~]# mkdir /abc
- [[email protected] ~]# mount.cifs //192.168.80.2/LNMP-C7 /abc/ // 挂载
- [[email protected] ~]# cd /abc/
- [[email protected] abc]# tar zxvf squid-3.4.6.tar.gz -C /opt // 解压
- [[email protected] abc]# yum install gcc gcc-c++ make -y // 安装环境组件
- [[email protected] abc]# cd /opt/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 --enable-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/ // 给目录所有文件属主属组权限
修改 squid 服务配置文件, 配置优化启动方式
- [[email protected] squid-3.4.6]# VIM /etc/squid.conf // 进入修改 squid 配置文件
- # And finally deny all other access to this proxy
- http_access allow all // 添加此项
- #http_access deny all // 注释, 允许终端访问
- # Squid normally listens to port 3128
- http_port 3128
- cache_effective_user squid // 指定用户 squid
- 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 // 查看 squid 端口
- [[email protected] squid-3.4.6]# cd /etc/init.d/
- [[email protected] init.d]# VIM squid // 编辑 service 启动 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]# chkconfig --add squid // 添加到 service 管理中
- [[email protected] init.d]# chkconfig --level 35 squid on // 设置开机自启
设置传统代理配置
- [[email protected] init.d]# VIM /etc/squid.conf // 修改主配置文件
- # Squid normally listens to port 3128
- http_port 3128
- cache_mem 64 MB // 内存空间大小
- reply_body_max_size 10 MB // 允许下载最大文件大小
- maximum_object_size 4096 KB // 允许保存缓存空间最大对象大小
- [[email protected] init.d]# service squid restart
- [[email protected] init.d]# iptables -L // 查看表内容
- [[email protected] init.d]# iptables -F // 清空表缓存
- [[email protected] init.d]# setenforce 0
- [[email protected] init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT // 允许 3128 端口
- [[email protected] init.d]# service squid reload // 重载配置文件
在 Web 服务器上安装 apache 服务
- [[email protected] ~]# systemctl stop firewalld.service // 关闭防火墙
- [[email protected] ~]# setenforce 0
- [[email protected] ~]# yum install httpd -y // 安装 apache 服务
- [[email protected] ~]# systemctl start httpd.service // 启动服务
使用 client 访问 Web 网页
在 Web 服务器日志文件中查看访问日志文件
- [[email protected] ~]# cd /etc/httpd/logs/ // 查看日志文件
- [[email protected] logs]# VIM access_log // 此时是 135 地址访问的
修改客户机浏览器代理设置
再次回到 Web 服务器日志文件中查看访问日志文件
- [[email protected] ~]# cd /etc/httpd/logs/ // 查看日志文件
- [[email protected] logs]# VIM access_log // 此时是 179 代理服务器访问的
透明代理实验搭建
实验环境
quid 服务器 IP 地址: ens33:192.168.80.184
ens36:192.168.10.1 (仅主机模式)
Web 服务器 IP 地址: 192.168.80.151
clientIP 地址: 192.168.10.10 (仅主机模式)
在 squid 服务器上添加一块网卡, 并配置 IP 地址
- [[email protected] ~]# cd /etc/sysconfig/network-scripts/
- [[email protected] network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
- [[email protected] network-scripts]# VIM ifcfg-ens36 // 修改 ens36IP 信息
删除 uuid 修改 33 为 36
- BOOTPROTO=static // 设置静态
- IPADDR=192.168.10.1
- NETMASK=255.255.255.0
- [[email protected] network-scripts]# service network restart // 重启网络服务
- [[email protected] network-scripts]# VIM /etc/sysctl.conf // 开启路由转发
- net.ipv4.ip_forward=1
- [[email protected] network-scripts]# sysctl -p // 加载
在 Web 服务器上指定静态路由
[[email protected] ~]# route add -net 192.168.10.0/24 gw 192.168.80.184 ## 添加静态路由
在 squid 服务器上设置透明代理
- [email protected] network-scripts]# VIM /etc/squid.conf // 设置配置文件
- http_port 192.168.10.1:3128 transparent // 设置透明代理
- cache_effective_user squid
- cache_effective_group squid
- [[email protected] network-scripts]# service squid stop // 关闭开启 squid 服务
- [[email protected] network-scripts]# service squid start
- [[email protected] network-scripts]# iptables -F // 清空表缓存
- [[email protected] network-scripts]# iptables -t nat -F
- [[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 // 定义规则入口 ens36,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 //https443 端口
- [[email protected] network-scripts]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT // 允许 3128 端口访问
client 客户端测试
- [[email protected] ~]# cd /var/log/httpd/
- [[email protected] httpd]# VIM access_log ## 查看访问日志信息, 此时访问地址变为 184 访问 Web 服务
来源: http://www.bubuko.com/infodetail-3347206.html