Squid代理服务器工作原理与如何搭建服务

1.缓存代理概述

1.1 Web缓存代理的工作机制

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

在这里插入图片描述

1.2 代理的基本类型

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

1.3 使用代理的好处

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

2.Squid代理服务器的设置

2.1 传统代理服务器

**项目环境:**
- 一台代理服务器squid:
IP:192.168.140.20
- 两台Web站点服务器:
IP:192.168.140.21
IP:192.168.140.22
- 一台客户机:
IP:192.168.140.14

2.1.1 配置传统代理服务器

首先导入软件包
在这里插入图片描述
(1) 解包,并编译安装

[root@squid ~]# tar zxvf squid-3.5.23
[root@squid ~]# cd squid-3.5.23/
[root@squid squid-3.5.23]# ./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-underscore \
--enable-poll \
--enable-gnuregex

//--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++常用的正则表达式

[root@squid squid-3.5.23]# make && make install
[root@squid squid-3.5.23]# cd

(2) 创建squid用户并设置属主与属组

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

(3) 添加指定程序用户和账号基本组

[root@squid ~]# vi /etc/squid.conf
...//省略部分信息
cache_effective_user squid	//添加指定程序用户
cache_effective_group squid	//添加指定账号基本组
coredump_dir /usr/local/squid/var/cache/squid

(4) 检测并启动服务

[root@squid ~]# squid -k parse		//检查语法
[root@squid ~]# squid -z		//初始化缓存目录
Set Current Directory to /usr/local/squid/var/cache/squid
Creating missing swap directories
No cache_dir stores are configured.
[root@squid ~]# squid		//启动服务
[root@squid ~]# netstat -anpt | grep squid
tcp6       0      0 :::3128                 :::*                    LISTEN      96132/(squid-1)

(5) 编辑服务脚本

[root@squid ~]# vi /etc/init.d/squid
#!/bin/bash
#chkconfig: 35 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 -anpt | 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 -anpt | grep squid
        else echo "squid is not running"
        fi
;;
restart)
        $0 stop &> /dev/null
        	echo "正在关闭 squid..."
        	echo "关闭成功"
        $0 start &> /dev/null
        	echo "正在启动 squid..."
        	echo "启动成功"
;;
reload)
        $CMD -k reconfigure
;;
check)
        $CMD -k parse
;;
*)
        echo "用法: $0{start|stop|status|restart|reload|check}"
esac

[root@squid ~]# chmod +x /etc/init.d/squid
[root@squid ~]# chkconfig --add /etc/init.d/squid
[root@squid ~]# chkconfig --level 35 squid on
[root@squid ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole     	0:1:2:3:4:5:6:关
network        	0:1:2:3:4:5:6:关
squid          	0:1:2:3:4:5:6:[root@squid ~]# systemctl restart squid
[root@squid ~]# service squid restart
正在关闭 squid...
关闭成功
正在启动 squid...
启动成功
[root@squid ~]# systemctl status squid

在这里插入图片描述
(6) 设置传统代理服务器

[root@squid ~]# vi /etc/squid.conf
...//省略部分信息
http_port 3128
cache_mem 64 MB			
'//指定缓存功能所使用的内存空间大小,便于保持访问较频繁的WEB对象,容量最好是4的倍数,单位为MB,建议设为物理内存的1/4'
reply_body_max_size 10 MB	
'//允许用户下载的最大文件大小,以字节为单位。默认设置0,表示不进行限制'
maximum_object_size 4096 KB	
'//允许保存到缓存空间的最大对象大小,以KB为单位,超过大小限制的文件将不被缓存,而是直接转发给用户'

[root@squid ~]# squid -k parse	'//检测语法'
[root@squid ~]# systemctl stop squid
[root@squid ~]# systemctl start squid

2.1.2 关闭防火墙

发送给所有会话

[root@squid ~]# systemctl stop firewalld
[root@squid ~]# setenforce 0
[root@squid ~]# systemctl disable firewalld
[root@squid ~]# grep -v "#" /etc/selinux/config

SELINUX=disabled
SELINUXTYPE=targeted 

2.1.3 Web站点的配置

  • 在web1服务器上
[root@web1 ~]# yum -y install httpd
[root@web1 ~]# echo "<h1>This is Web1</h1>" > /var/www/html/index.html
[root@web1 ~]# systemctl start httpd
[root@web1 ~]# curl http://localhost
<h1>This is Web1</h1>
  • 在Web2服务器上
[root@web2 ~]# yum -y install httpd
[root@web2 ~]# echo "<h1>This is Web2</h1>" > /var/www/html/index.html
[root@web2 ~]# systemctl start httpd
[root@web2 ~]# curl http://localhost
<h1>This is Web2</h1>

2.1.4 验证结果

  • 浏览器上未设置代理服务器时

此时在客户机上访问web地址,在web上查看日志明显,看到均由客户机发起请求

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

tail -f /var/log/httpd/access_log	'//查看动态日志'

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

  • 在客户机浏览器上设置代理服务器
squid服务器上
[root@squid ~]# iptables -F
[root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@squid ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:squid

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 
(1)在客户机浏览器上,选择首选项进入高级配置,选择网络设置

(2)设置手动配置代理,填写代理服务器的地址及其端口号,并确定修改的配置

(3)然后清空浏览器缓存,访问web站点地址,查看日志

在这里插入图片描述
在这里插入图片描述
发现是从squid代理服务器发起的请求

此时表示传统代理服务器设置成功

2.2 配置透明代理服务器

在这里插入图片描述

**项目环境:**
- 一台代理服务器squid,双网卡:
IP:192.168.70.20(VM1)
IP:192.168.80.21(VM2)
- 两台Web站点服务器:
IP:192.168.80.22(VM2)
IP:192.168.80.23(VM2)
Web服务器不需要指网关
- 一台客户机:
IP:192.168.70.21(VM1)
客户机需要指网关,指向代理服务器VM1的IP

2.2.1 搭建网络环境

  • 在squid服务器上开启路由,确保客户机能 ping 通Web服务器
[root@squid ~]# vi /etc/sysctl.conf
...
net.ipv4.ip_forward=1
[root@squid ~]# sysctl -p
  • 在web1和web2上均进行以下配置
route add -net 192.168.70.0/24 gw 192.168.80.21
route -n	'//查看路由'

2.2.2 在squid代理服务器上的配置

  • 设置代理IP和端口
[root@squid ~]# vi /etc/squid.conf
...
http_port 192.168.70.20:3128 transparent

[root@squid ~]# systemctl restart squid
[root@squid ~]# netstat -anpt | grep squid
tcp        0      0 192.168.70.20:3128      0.0.0.0:*               LISTEN      98583/(squid-1)  
  • 修改添加策略
[root@squid ~]# iptables -F		'//清空防火墙策略'
[root@squid ~]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.70.0/24 -p tcp --dport=80 -j REDIRECT --to 3128
[root@squid ~]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.70.0/24 -p tcp --dport=443 -j REDIRECT --to 3128
[root@squid ~]# iptables -I INPUT -p tcp --dport=3128 -j ACCEPT

在这里插入图片描述

  • 验证结果

(1)在客户机浏览器上,选择首选项设置不使用代理,访问web站点服务器

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

(2)在web站点服务器上,查看日志

在这里插入图片描述
在这里插入图片描述
访问的地址为squid代理服务器的VM1地址

(3)在squid服务器上,查看日志

在这里插入图片描述
此刻表示透明代理服务器设置成功

3.ACL访问控制

3.1 常用的ACL列表类型

  • src → 源地址
  • dst → 目标地址
  • port → 端口号
  • dstdomain → 目标域
  • time → 访问时间
  • maxconn → 最大并发连接
  • url_regex → 目标URL地址
  • Urlpath regex → 整个目标URL路径

3.2 ACL的特点

  • 先定义,再调用
squid 定义 acl
acl	列表名称	类型	控制的对象
squid 调用 acl
http_access allow/deny 列表名称

3.3 Squid中ACL的应用

  • 这里我们可以在之前设置的透明代理上,进行相关验证

(1) 设置访问控制,拒绝客户机访问

[root@squid ~]# vi /etc/squid.conf
...
acl client   src 192.168.70.21/32
http_access deny client

在这里插入图片描述

(2) 重启服务

[root@squid ~]# systemctl stop squid
[root@squid ~]# systemctl start squid

(3) 验证结果

  • 在客户机上访问Web服务器

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

  • 查看日志

在这里插入图片描述

4.Squid日志分析工具

4.1 配置sarg日志

  • 在根目录下导入软件包
    在这里插入图片描述
  • 在联网的环境下
[root@squid ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

'//可以从阿里云镜像仓库,Epel 镜像中获取'

在这里插入图片描述

[root@squid ~]# yum -y install gd gd-devel	'//图像处理'

注意:

  • 若无法安装 gd-deve,需在yum源目录下导入以下配置文件
  • 配置文件从网易开源镜像站获取

在这里插入图片描述

  • 再重新安装
[root@squid ~]# yum -y install gd gd-devel
  • 创建目录,并解包编译
[root@squid ~]# mkdir /usr/local/sarg
[root@squid ~]# tar zxvf sarg-2.3.7.tar.gz
[root@squid ~]# cd sarg-2.3.7/
[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
[root@squid sarg-2.3.7]# cd
  • 配置sarg信息
[root@squid ~]# vi /etc/sarg/sarg.conf
...
access_log /usr/local/squid/var/logs/access.log		//7,指定访问日志文件
title "Squid User Access Reports"					//25,网页标题
output_dir /var/www/html/squid-reports		//120,报告输出目录
user_ip no									//178,使用用户名 显示
topuser_sort_field connect reverse			//184,top排序中有连接次数、访问字节、降序排列,升序是normal
user_sort_field reverse						//190,用户访问记录连接次数、访问字节按降序排序
exclude_hosts /usr/local/sarg/noreport		//206,不计入排序的站点列表文件
overwrite_report no			//257,同名日志是否覆盖
mail_utility mailx.postfix	//289,发送邮件报告命令
charset UTF-8	//434,使用字符集
weekdays 0-6	//518,top排行的星期周期
hours 0-23		//525,top排行的时间周期
www_document_root /var/www/html		//633,网页根目录
  • 生成sarg报告
[root@squid ~]# touch /usr/local/sarg/noreport
[root@squid ~]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/
[root@squid ~]# cd /usr/local/squid/var/logs/
[root@squid logs]# ls -lh
总用量 44K
-rw-r-----. 1 squid squid 2.7K 1216 00:18 access.log
-rw-r-----. 1 squid squid  37K 1216 00:19 cache.log
[root@squid logs]# cd
[root@squid ~]# sarg
SARG: 纪录在文件: 22, reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/squid-reports/2020Dec15-2020Dec16
  • 安装并启动http服务
[root@squid ~]# yum -y install httpd
[root@squid ~]# systemctl start httpd.service

4.2 验证结果

  • 在客户机上访问 http://192.168.70.20/squid-reports/

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

注意:如果客户机无法访问,查看代理服务器的ACL访问控制,是否该IP被禁止

4.3 周期性计划任务执行

  • 每天凌晨两点生成报告 crontab
[root@squid ~]# which sarg
/usr/local/bin/sarg
[root@squid ~]# crontab -e
* 2 * * * /usr/local/bin/sarg -l /usr/local/var/logs/access.log -o /var/www/html/squid-reports/-z -d $(date -d "-1 day" +%Y-%m-%d)-$(date +%Y-%m-%d)

5.反向代理

5.1 概述

  • 如果 Squid 反向代理服务器中缓存了该请求的资源,则将该请求的资源直接返回给客户端;否则反向代理服务器将向后台的WEB服务器请求资源,然后将请求的应答返回给客户端,同时也将该应答缓存在本地,供下一个请求者使用。
    在这里插入图片描述

5.2 反向代理网站加速

工作机制

  • 缓存网页对象,减少重复请求;
  • 将互联网请求轮询或按权重分配到内网Web服务器;
  • 代理用户请求,避免用户直接访问Web服务器,提高安全。
    在这里插入图片描述

5.3 反向代理的应用

[root@squid ~]# systemctl stop httpd.service	'//因为在squid反向代理中会用到80端口,所以要先停止http服务'
[root@squid ~]# vi /etc/squid.conf
http_port 192.168.70.20:80 accel vhost vport
cache_peer 192.168.80.22 parent 80 0 no-query originserver round-robin max_conn=100 weight=1 name=web1
cache_peer 192.168.80.23 parent 80 0 no-query originserver round-robin max_conn=100 weight=1 name=web2
cache_peer_domain web1 web2 www.yun.com		'//添加设置域名访问'
[root@squid ~]# iptables -F		'//清除所有防火墙策略'
[root@squid ~]# iptables -F -t nat
[root@squid ~]# iptables -L -t nat     '//查看nat上的所有规则'
[root@squid ~]# systemctl stop squid
[root@squid ~]# systemctl start squid
[root@squid ~]# netstat -anpt | grep squid
tcp        0      0 192.168.70.20:80        0.0.0.0:*               LISTEN      102906/(squid-1)    
  • 在浏览器上访问,并验证
    在这里插入图片描述
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42449832/article/details/111210773
今日推荐