squid代理

本文详细介绍了Squid代理服务器的三种模式:传统代理、透明代理和反向代理。传统代理需要客户端指定服务器地址,透明代理则在用户无感知的情况下通过防火墙规则实现。反向代理用于缓存资源并隐藏后端服务器,同时实现负载均衡。此外,还阐述了Squid的ACL访问控制和日志分析工具Sarg的使用,以及如何通过Squid进行防火墙配置和系统服务管理。
摘要由CSDN通过智能技术生成

理论

一,概念
代理分为传统代理,使用与Internet,需要明确指明服务端
透明代理:客户机不需要指明代理服务器的地址和端口,而通过默认路由,防火墙策略将web访问重定向给代理服务器处理,从而挺高web的访问速度。通过路由,防火墙规则。
反向代理:反向代理服务器中缓存了改请求的资源,则将该资源直接返回给客户端,向后台的服务器请求资源,然后将骑牛的应答返回给客户端,同时也将应答缓存在本地,供下一次使用
二,代理过程
代理的工作机制,缓存网页对象,减少重复请求,squid是一个缓存数据的一个软件,它接受用户的下载请求,并自动出来锁下载的数据,需要squid代替下载,然后squid连接锁申请网站,并请求主页,把它传递给用户,从而隐藏了客户机的真是ip。

实验

一,squid代理
第一步,编译安装squid
./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

make && make install
配置启动,授权

[root@localhost ~]# ln -s /usr/local/squid/sbin/* /usr/local/sbin 
[root@localhost ~]# useradd -M -s /sbin/nologin squid
[root@localhost ~]# chown -R squid:squid /usr/local/squid/var/

vim /etc/squid.conf
配置主配置文件

在这里插入图片描述

在这里插入图片描述
squid -k parse ##检查配置文件
squid –k rec ##重新加载配置文件
squid -zX ##初始化缓存目录
squid ##启动squid服
在这里插入图片描述
编写squid服务脚本
vim /etc/init.d/squid

#!/bin/bash
#chkconfig: 35 90 25
#config: /etc/squid.conf
#pidfile: /usr/local/squid/var/run/squid.pid
#Description: Squid - Internet Object Cache

PID="/usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"

case "$1" in
        start)
                netstat -utpln | grep squid &>/dev/null
                if [ $? -eq 0 ]
                        then
                                echo "Squid is running"
                else
                        $CMD
                fi
        ;;
        stop)
                $CMD -k kill &>/dev/null
                rm -rf $PID &>/dev/null
        ;;
        status)
                [ -f $PID ] &>/dev/null
                        if [ $? -eq 0 ]
                          then
                                netstat -utpln | 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 "用法:{start | stop | restart | reload | check | status}"
esac

授权启动

[root@localhost ~]# vim /etc/init.d/squid
[root@localhost ~]# chmod +x /etc/init.d/squid
[root@localhost ~]# chkconfig --add squid
[root@localhost ~]# chkconfig squid on
[root@localhost ~]# service squid restart
正在关闭Squid...
正在启动Squid...

squid安装完毕

搭建传统代理

一台squid服务器
两天web服务器,
一台win10

第一步,squid服务器已经如上述配置
在这里插入图片描述
修改防火墙规则
iptables -F
iptables -I INPUT -p tcp --dport 3128 -j ACCEPT

第二步:配置web服务器
关闭防火墙,核心防护,安装httpd,启动httpd
在这里插入图片描述
第三步,在win10上配置代理服务器

设置–》高级–》系统–》打开代理设置–》设置代理

在这里插入图片描述
访问http服务器
在这里插入图片描述
在http服务器上查看访问日志,
发现通过squid服务器192.168.17.30服务器进行缓存
在这里插入图片描述

透明代理

服务器需求
squid服务器192.168.17.30
web1服务器192.168.17.10
web2服务器192.168.17.20
win10 192.168.100.200 网关为192.168.100.30

第一步,设置squid服务器
配置两块网卡
192.168.17.30
192.168.100.30
在这里插入图片描述
vim /etc/squid.conf

http_access allow all
http_access deny all

http_port 192.168.100.30:3128 transparent
在这里插入图片描述
配置防火墙

root@localhost network-scripts]# iptables -F
[root@localhost network-scripts]# iptables -t nat -F
[root@localhost network-scripts]# iptables -t nat -I PREROUTING -i ens37 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
[root@localhost network-scripts]# iptables -t nat -I PREROUTING -i ens37 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
[root@localhost network-scripts]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT

第二步,在web服务器上
安装http 启动
增加一条静态路由
查看访问日志
在这里插入图片描述
第三步,在win10上查看
在这里插入图片描述

ACL访问

主squid主哦诶之文件中,ACL访问可以通过两个步走实现
使用acl定义需要配置的
通过http_access配置定义的允许或者不允许
第一种方式,直接在配置文件中修改

vim /etc/squid.conf

acl localhost src 192.168.100.30/24 #源地址为192.168.1100.30
acl MYLAN src 192.168.100.0/24 #客户机网段
acl destinationhost dst 192.168.17.30/32 #目标地址为192.168.17.30
acl MC20 maxconn 20 #最大并发连接20
acl PORT port 21 #目标端口21
acl DMBLOCK dstdomain .qq.com #目标域,匹配域内所有站点
acl BURL url_regex -i ^rtsp:// ^emule:// #以rtsp://. emule://开头的URL,-i表示忽略大小写
acl PURL urlpath_regex -i .mp3$ .mp4$ .rmvb$ #以 .mp3、.mp4、.rmvb结尾的URL路径
acl WORKTIME time MTWHF 08:30-17:30 #时间为周一-至周五8:30~17:30, "MTWHF"为每个星期的英文首字母

第一条插入:
http_access deny host

第二种方式,配置文件,然后再squid.conf中调用
mkdir /etc/squid
vim /etc/squid/dest.list
192.168.217.30 #Squid服务器IP
192.168.17.0/24 #任意需要的网段

vim /etc/squid.conf

acl destinationhost dst “/etc/squid/dest.list”
http_access deny (或allow) destinationhost

systemctl restart squid

squid日志,

sarg软件,是一款squid日志分析工具,采用html格式

具体步骤
第一步,安装依赖环境
yum install -y gd gd-devel pcre-devel

mkdir /usr/local/sarg

tar zxvf sarg-2.3.7.tar.gz -C /opt/

cd /opt/sarg-2.3.7
./configure --prefix=/usr/local/sarg --sysconfdir=/etc/sarg --enable-extraprotection
make && make install
在这里插入图片描述

[root@localhost sarg-2.3.7]# vim /etc/sarg/sarg.conf
[root@localhost sarg-2.3.7]# vim /etc/sarg/sarg.conf
[root@localhost sarg-2.3.7]# touch /usr/local/sarg/noreport
[root@localhost sarg-2.3.7]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/
[root@localhost sarg-2.3.7]# sarg 
SARG: 纪录在文件: 1525, reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/sarg/2021Jul28-2021Jul28
[root@localhost sarg-2.3.7]# yum install httpd -y

–7行–取消注释
access_log /usr/local/squid/var/logs/access.log #指定访问日志文件
–25行–取消注释
title “Squid User Access Reports” #网页标题
–120行–取消注释,修改
output_dir /var/www/html/sarg #报告输出目录
–178行–取消注释
user_ip no #使用用户名显示
–184行–取消注释,修改
topuser_sort_field connect reverse #top排序中,指定连接次数采用降序排列,升序是normal
–190行–取消注释,修改
user_sort_field connect reverse #对于用户访问记录,连接次数按降序排序
–206行–取消注释,修改
exclude_hosts /usr/local/sarg/noreport #指定不计入排序的站点列表的文件
–257行–取消注释
overwrite_report no #同名同日期的日志是否覆盖
–289行–取消注释,修改
mail_utility mailq.postfix #发送邮件报告命令
–434行–取消注释,修改
charset UTF-8 #指定字符集UTF-8
–518行–取消注释
weekdays 0-6 #top排行的星期周期
–525行–取消注释
hours 0-23 #top排行的时间周期
–633行–取消注释
www_document_root /var/www/html #指定网页根目录

在这里插入图片描述
在192.168.17.30服务器上访问

在这里插入图片描述
在这里插入图片描述
date -s
#添加计划任务,执行每天生成报告
vim /usr/local/sarg/report.sh
#!/bin/bash
#Get current date
TODAY=KaTeX parse error: Expected 'EOF', got '#' at position 18: …ate +%d/%m/%Y) #̲Get one week ag…(date -d “1 day ago” +%d/%m/%Y)
/usr/local/sarg/bin/sarg -l /usr/local/squid/var/logs/access.log -o /var/www/html/sarg -z -d Y E S T E R D A Y − YESTERDAY- YESTERDAYTODAY &> /dev/null
exit 0
chmod +x /usr/local/sarg/report.sh
crontab -e
0 0 * * * /usr/local/sarg/report.sh

反向代理

squid反向代理服务器中缓存了请求的资源,则将请求的资源直接返回给客户端,负责反向给客户点,同时缓存在本地
工作机制:
缓存网页对象,较少重复请求
将互联网请求轮询或按照权重分配到内网服务器
代理用户,避免用户直接访问web服务,提高安全
具体配置

开启防火墙
配置防火墙策略

[root@localhost sarg-2.3.7]# iptables -F
[root@localhost sarg-2.3.7]# iptables -t nat -F
[root@localhost sarg-2.3.7]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT 
[root@localhost sarg-2.3.7]# vim /etc/squid.conf
[root@localhost sarg-2.3.7]# service squid restart
正在关闭Squid...
正在启动Squid...

# Squid normally listens to port 3128

http_port 192.168.17.30:80 accel vhost vport
cache_peer 192.168.17.10 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
cache_peer 192.168.17.20 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
cache_peer_domain web1 web2 www.kgc.com
http_port 80 accel vhost vport

在web服务器上,安装http
定义主页
yum install -y httpd
systemctl start httpd

#节点1(web1):
echo “this is test01” >> /var/www/html/index.html
#节点2(web2):
echo “this is test02” >> /var/www/html/index.html

win10访问验证

在这里插入图片描述

总结

squid代理的三种模式比较
1,传统代理,需要指定客户端指定squid代理服务,客户端感知到squid代理服务的存在
2,透明代理:客户端不需要配置,利用squid服务器的防火墙规则和web服务的静态路由方式,实验代理的功能,对用户时无感知的
3,反向代理:类似于nginx的服务器反向代理功能,但是资深粗腰一个首页,基于虚拟的ip以及端口,以及虚拟的域名进行反向代理给后端真是的服务器ip而且可以采用负载均衡的方式代理

squid自身的管理功能
ACL:做acces的访问控制,增加一些允许和拒绝管理
sarg:日志分析工具,可以以天的方式将access_log中的内容以web页面的方式,展示给用户

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值