Squid代理服务器的应用--传统代理和透明代理的部署

缓存代理概述

●缓存网页对象,减少重复请求

●www.163.com: 源站服务器

●squid代理服务器 :缓存加速服务器,会有单独的缓存空间,存放源服务器的页面信息,也就是cache

●Cache:单独的缓存空间,存放源服务器的页面信息
在这里插入图片描述

代理的基本类型

●传统代理:适用于Internet,需明确指定服务端

●透明代理:客户机不需要指定代理服务器的地址和端口,而是通过默认路由,防火墙策略将Web访问重定向给代理服务器处理

使用代理的好处

  • 提高Web访问速度
  • 隐藏客户机的真实IP地址

Squid传统代理实验

实验环境

  • 代理服务器虚拟机的IP地址:192.168.148.135 squid服务
  • Web端的IP地址:192.168.148.136 httpd服务
  • 客户端的IP地址:192.168.148.150 做访问测试

推荐步骤

1.将两台虚拟机的主机名进行修改方便区分
squid服务器:
在这里插入图片描述
web服务器:
在这里插入图片描述
2.先配置squid代理服务器

[root@squid ~]# setenforce 0
[root@squid ~]# tar zxvf squid-3.4.6.tar.gz -C /opt/        ##将工具拷贝到/opt目录下
[root@squid squid-3.4.6]# yum -y install gcc gcc-c++     ##安装手动编译工具

在这里插入图片描述

[root@squid /]# cd /opt/
[root@squid opt]# ls
rh  squid-3.4.6
[root@squid opt]# cd squid-3.4.6/
[root@squid squid-3.4.6]# 
[root@squid 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 \      ##关闭默认使用poll模式,开启epoll模式提升性能
 --enable-gnuregex     ##支持正则表达
[root@squid squid-3.4.6]# make&&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/   ##设置目录的属主和属组

3.修改squid配置文件,优化启动项

[root@squid squid-3.4.6]# vim /etc/squid.conf
 55 # And finally deny all other access to this proxy
 56 #http_access deny all     ##将拒绝所有注释掉
 57 http_access allow all    ##57行加入允许所有访问
 58 
 59 # Squid normally listens to port 3128
 60 http_port 3128
 61 cache_effective_user squid   ##61行添加指定用户squid
 62 cache_effective_group squid   ##添加指定组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    ##检查服务是否开启

在这里插入图片描述
4.添加服务到service管理

[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    ##增加service管理
[root@squid init.d]# chkconfig --level 35 squid on     ##设置3 5级别开机自启

5.配置传统代理服务

[root@squid init.d]# vim /etc/squid.conf
# Squid normally listens to port 3128
http_port 3128
cache_effective_user squid
cache_effective_group squid
cache_mem 64 MB   ##自定义缓存空间大小,容量最好为4的倍数
reply_body_max_size 10 MB   ##允许下载最大文件大小,以字节为单位,默认设置0表示不进行限制
maximum_object_size 4096 KB   ##允许保存到缓存空间的最大对象的大小,以KB为单位,超过限制不会缓存,直接转到web端
[root@squid init.d]# iptables -F    ##清空防火规则
[root@squid init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT   ##添加规则,允许3128端口
[root@squid init.d]# service squid reload

6.在web服务器上安装http服务

[root@web ~]# setenforce 0      
[root@web ~]# iptables -F
[root@web ~]# yum -y install httpd    ##安装httpd服务
[root@web ~]# systemctl start httpd.service 

在这里插入图片描述
7.打开win10虚拟机的浏览器访问web页面
在这里插入图片描述
8.回到web虚拟机查看访问日志,发现来访地址为客户端的IP地址

[root@web ~]# cd /var/log/httpd/
[root@web httpd]# ls
access_log  error_log
[root@web httpd]# cat access_log    ##查看访问日志

在这里插入图片描述
9.在客户端的浏览器上配置代理服务器
在这里插入图片描述
在这里插入图片描述
配置IP地址为代理服务器的IP地址:192.168.148.135;端口号为:3128;点击保存
在这里插入图片描述
回到web端再次查看访问日志发现来访地址为代理服务器的地址
在这里插入图片描述

Squid透明代理实验

实验环境

  • 继承刚刚传统代理的环境需要做如下修改
  • squid服务器添加一块网卡:192.168.10.1(仅主机模式)
  • web服务器不变
  • client客户端修改网卡为仅主机模式,IP地址修改为192.168.10.10,且浏览器关闭代理功能

推荐步骤

一.先配置代理服务器
1.先将代理服务配置双网卡,都选为仅主机模式
在这里插入图片描述
2.修改squid服务器的网卡地址,并配置透明代理服务

[root@squid ~]# cd /etc/sysconfig/network-scripts/
[root@squid network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36      
[root@squid network-scripts]# vim ifcfg-ens36

在这里插入图片描述

[root@squid network-scripts]# vim /etc/sysctl.conf    ##配置开启路由转发功能
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_forward=1      ##添加这一行
[root@squid network-scripts]# sysctl -p     ##让其生效
[root@squid network-scripts]# service network restart     ##重启网卡 

在这里插入图片描述

[root@squid network-scripts]# vim /etc/squid.conf    ##修改配置文件
# Squid normally listens to port 3128
http_port 192.168.10.1:3128 transparent     ##修改此行,开启透明代理
cache_effective_user squid
cache_effective_group squid
cache_mem 64 MB
reply_body_max_size 10 MB
maximum_object_size 4096 KB
[root@squid network-scripts]# squid -k parse    ##检查语法结构
[root@squid network-scripts]# iptables -F    ##清空防火墙规则
[root@squid network-scripts]# iptables -t nat -F    ##清空nat表的规则
[root@squid network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128     ##定义规则入口ens36网卡,http协议80端口重定向到3128端口
[root@squid network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128    ##定义规则入口ens36网卡,https协议443端口重定向到3128端口
[root@squid network-scripts]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT   ##添加规则,允许3128端口
[root@squid network-scripts]# service squid reload     ##重新加载服务
[root@squid network-scripts]# service squid stop     ##停止服务
[root@squid network-scripts]# service squid start     ##开启服务
正在启动 squid....

3.修改客户端的IP地址,关闭代理功能
在这里插入图片描述
在这里插入图片描述
4.再次访问httpd,并且在web服务器上查看访问日志文件,查看来访地址

[root@web ~]# cd /var/log/httpd/
[root@web httpd]# ls
access_log  error_log
[root@web httpd]# cat access_log    ##查看访问日志

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值