Squid代理服务器应用——(传统代理/透明代理、acl访问控制、sarg日志、反向代理)

一.缓存代理概述

1.作用

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

2.过程示意图

在这里插入图片描述

3.代理类型

传统代理:适用于Internet,需明确指定服务端
透明代理:客户机不需要指定代理服务器的地址和端口,是通过默认路由,防火墙将web重定向给代理

4.使用代理的好处

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

二.传统代理

1.实验环境

squid服务器:192.168.52.11
web服务器1:192.168.52.12
web服务器2:192.168.52.13
客户机

2.安装squid软件

tar zxvf squid-3.5.23
cd /squid-3.5.23

./configure --prefix=/usr/local/squid \
--sysconfdir=/etc \
--enable-arp-acl \
--enable-linux-netfilter \     //Linux内核通过netfilter模块实现网络访问功能
--enable-linux-tproxy \       //实现透明代理功能
--enable-async-io=100 \         //异步传输输入输出数量
--enable-err-language="Simplify_Chinese" \              //用中文报错
--enable-underscore \           //允许下划线
--enable-poll \              //协调读写设备个数或读写设备顺序的函数
--enable-gnuregex           //c/c++常用的正则表达式

make && make install              //编译安装
ln -s /usr/local/squid/sbin/* /usr/sbin        //便于系统识别命令   
useradd -M -s /sbin/nologin squid          //创建系统用户,设为不可登录
chown -R squid.squid /usr/local/squid/var/             //给目录所有文件属主属组权限

vim /etc/squid.conf
指定程序用户
指定账号基本组

在这里插入图片描述
squid -k parse //检查语法
在这里插入图片描述

3.优化启动项

vim /etc/init.d/squid
#!/bin/bash
#chkconfig: 35 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

在这里插入图片描述

4.设置传统代理服务器

vim /etc/squid.conf
添加如下
http_access allow all         //允许所有
http_port 3128
cache_mem 64 MB       //内存空间大小
reply_body_max_size 10 MB       //允许下载最大文件大小
maximum_object_size 4096 KB         //允许保存缓存空间最大对象大小

在这里插入图片描述
systemctl stop squid.service
systemctl start squid.service

代理服务器路由配置

iptables -F
iptables -I INPUT -p tcp --dport 3128 -j ACCEPT     //允许3128端口

在这里插入图片描述

5.配置web节点服务器

web1

yum -y install httpd
echo "<h1>this is web1</h1>"  >  /var/www/html/index.html      //输入web1网页内容
curl http://localhost        //查看网页内容

在这里插入图片描述

web2

yum -y install httpd
echo "<h1>this is web2</h1>"  >  /var/www/html/index.html      //输入web1网页内容
curl http://localhost        //查看网页内

在这里插入图片描述

6.客户机验证方式

在这里插入图片描述
在这里插入图片描述

访问web1服务器地址

在这里插入图片描述

访问web2服务器地址

在这里插入图片描述

查看服务器日志验证

web1
在这里插入图片描述
web2
在这里插入图片描述

三.透明代理

1.实验环境

squid服务器 ens33:192.168.52.11
                   ens36:192.168.1.11(仅主机模式)
web服务器 192.168.52.12
client 192.168.1.14 (仅主机模式)

2.实验拓扑图

在这里插入图片描述

3.在squid服务上添加一块网卡,并设置ip地址

[]service network restart   ##重启网络服务
[] vim /etc/sysctl.conf   ##开启路由转发
   net.ipv4.ip_forward=1
[] sysctl -p   ##加载

4.在web服务器上指定静态路由

route add -net 192.168.10.0/24 gw 192.168.13.184

5.在squid服务器上设置透明代理

[] vim /etc/squid.conf   ##设置配置文件
http_port 192.168.1.11:3128 transparent   ##设置透明代理
cache_effective_user squid
cache_effective_group squid
[] service squid stop  ##关闭开启squid服务
[] service squid start
[] iptables -F  ##清空表缓存
[] iptables -t nat -F
[] iptables -t nat -I PREROUTING -i ens36 -s 192.168.1.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
##定义规则入口ens36,80端口重定向到3128
[] iptables -t nat -I PREROUTING -i ens36 -s 192.168.1.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
##https443端口
[] iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
##允许3128端口访问

6.在web服务器上查看访问日志文件

[root@web ~]# cd /var/log/httpd/
[root@web httpd]# vim access_log   ##查看访问日志信息

四.ACL访问控制

1.实验环境

squid服务器 ens33:192.168.13.184
            ens36:192.168.10.1 (仅主机模式)
web服务器 192.168.13.151
 client 192.168.10.10  (仅主机模式)

2.配置过程

1、在squid服务器上修改配置文件

[root@squid ~]# vim /etc/squid.conf  ##修改配置文件
# should be allowed
acl hostlocal src 192.168.10.10/32  ##控制hostlocal10.10的主机
# Deny requests to certain unsafe ports
http_access deny hostlocal  ##拒绝访问
[root@squid ~]# service squid reload   ##重启squid服务

2、在测试机上访问web网页

在这里插入图片描述

五、sarg日志

1,在squid服务器上安装sarg

[root@squid ~]# mount.cifs //192.168.100.3/LNMP-C7 /mnt/  ##挂载
 Password for root@//192.168.100.3/LNMP-C7:  
[root@squid ~]# cd /mnt/
[root@squid mnt]# tar zxvf sarg-2.3.7.tar.gz -C /opt/   ##解压
[root@squid mnt]# cd /opt/sarg-2.3.7/
[root@squid sarg-2.3.7]# yum install gd gd-devel -y  ##安装gd库
[root@squid sarg-2.3.7]# ./configure --prefix=/usr/local/sarg \  ##安装路径
> --sysconfdir=/etc/sarg \   ##配置文件
> --enable-extraprotection  ##开启安全防护
[root@squid sarg-2.3.7]# make && make install  ##编译安装

2,修改sarg配置文件

[root@squid sarg-2.3.7]# vim /etc/sarg/sarg.conf  ##修改sarg配置文件
##将下面的模块修改开启
access_log /usr/local/squid/var/logs/access.log  ##指定访问日志文件
title "Squid User Access Reports"  ##网页标题
output_dir /var/www/html/squid-reports  ##报告输出目录
user_ip no  ##使用用户名显示
exclude_hosts /usr/local/sarg/noreport  ##不计入排序的站点列表文件
topuser_sort_field connect reverse  
##top排序中有连接次数,访问字节,降序排列,升序是normal
overwrite_report no  ##同名日志是否覆盖
mail_utility mailq.postfix  ##发送邮件报告命令
charset UTF-8  ##使用字符集
weekdays 0-6  ##top排行的时间周期
hours 0-23  ##top排行的时间周期
www_document_root /var/www/html  ##网页根目录
[root@squid ~]# sarg   ##生成报告
SARG: 纪录在文件: 91, reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/squid-reports/2019Dec11-2019Dec12
[root@squid sarg-2.3.7]# cd /var/www/html/squid-reports/  ##切换到html目录下
[root@squid squid-reports]# ls
2019Dec11-2019Dec12   images  index.html
[root@squid squid-reports]# yum install httpd -y  ##安装httpd服务
[root@squid squid-reports]# systemctl start httpd.service  ##开启服务
[root@squid squid-reports]# systemctl stop firewalld.service   ##关闭防火墙
[root@squid squid-reports]# setenforce 0

3、用测试机访问网页查看访问记录

在这里插入图片描述

周期性计划任务执行每天生成报告crontab

sarg -l /usr/local/squid/var/logs/access.log -o /var/www/html/squid-reports/ -z -d $(date -d "1 day ago" +%d/%m/%Y)-$(date +%d/%m/%Y)

在这里插入图片描述

六、squid反向代理

1.环境

squid服务器 ens33:192.168.13.184
           ens36:192.168.10.1 (仅主机模式)
web1服务器 192.168.13.151
web2服务器 192.168.13.185
client 192.168.10.10  (仅主机模式)

2.在web1服务器上编辑一个网页内容

[root@web ~]# cd /var/www/html/
[root@web html]# vim index.html  ##编辑网页内容
<h1>this is test 01 web1!</h1>

3.在测试机上访问网页

在这里插入图片描述

4.在web2服务器上编辑一个网页内容

[root@web2 ~]# systemctl stop firewalld.service   ##关闭防火墙
[root@web2 ~]# setenforce 0
[root@web2 ~]# yum install httpd -y   ##安装httpd服务
[root@web2 ~]# cd /var/www/html/   ##创建网页内容
[root@web2 html]# vim index.html
<h1>this is test 02 web2!</h1>
[root@web2 html]# systemctl start httpd.service 

5.在squid服务上配置反向代理

[root@localhost squid]# vim /etc/squid.conf
 # Squid normally listens to port 3128
 http_port 192.168.13.184:80 accel vhost vport ##监控本机80端口
 cache_peer 192.168.13.151 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
 ##节点服务器1最大访问30,权重1,别名web1
 cache_peer 192.168.13.185 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
 cache_peer_domain web1 web2 www.yun.com  ##访问yun.com匹配web1,web2节点
[root@localhost squid]# service squid restart  ##重启squid服务

6.设置代理,测试

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45647891/article/details/111237146