一:squid代理服务器概述
1.1:squid简介
- squid是一个支持http、https、ftp等服务的web缓存代理软件,通过缓存页面实现降低宽带占用提高页面响应时间。
- 另外,squid提供访问控制,squid的缓存页面存放在内存和硬盘中,在选择服务器时候内存和硬盘要求高,对于数据过期更新,需要不定期的清理缓存的数据。
1.2:squid代理请求流程
- 客户端访问squid代理服务器
- 代理服务器代表客户端访问后端真实服务器
- 真实服务器把数据返回给squid代理服务器
- 代理服务器吧响应数据发送给客户端,并把页面缓存在本地的内存及磁盘中
- 客户端再次请求相同数据是,代理服务器直接将缓存的数据返回给客户端
1.3:squid代理服务类型
- 正向代理:主要应用于内部网络访问外部网络时缓存页面数据,提供统一网络接口连接到外网,所有的内网客户端无需配置外网IP即可通过squid上网,在这种模式下,squid主要负责提供缓存加速及访问控制的功能
- 透明代理:与正向代理类似,不需要终端用户进行特殊设置,需要结合网管部署,所有操作均由管理员在网关服务器和代理服务器进行设置,这些对用户透明的。
- 反向代理:反向代理集合智能DNS可以实现基本的CDN框架,此时的squid代理服务器可以直接代表后端服务器提供页面访问,用户并感觉不到自己是直接访问代理服务器,有利于减轻真实服务器压力,提高并发和响应速度。
二:部署环境实验操作
2.1:环境案例
主机 | IP地址 |
---|
squid | 20.0.0.20 |
web1 | 20.0.0.21 |
web2 | 20.0.0.22 |
2.2:安装squid服务
--------------------------------------------安装squid服务--------------------------------------------------------------------------------------------------------------------------------------------------
squid服务器:
[root@localhost ~]
[root@localhost ~]
web服务器:
[root@localhost ~]
[root@localhost ~]
squid服务器配置:
将软件包上传到服务器中
[root@squid ~]
[root@squid ~]
[root@squid squid-3.4.6]
[root@squid squid-3.4.6]
--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
--------------------------------------------上面配置的解释---------------------------------------------------------
./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 \
----------------------------------------------------------------------------------------------------------
[root@squid squid-3.4.6]
[root@squid squid-3.4.6]
[root@squid squid-3.4.6]
[root@squid squid-3.4.6]
[root@squid squid-3.4.6]
http_access allow all
http_port 3128
cache_effective_user squid
cache_effective_group squid
[root@squid squid-3.4.6]
[root@squid squid-3.4.6]
[root@squid squid-3.4.6]
[root@squid squid-3.4.6]
tcp6 0 0 :::3128 :::* LISTEN 40026/(squid-1)
[root@squid squid-3.4.6]
[root@squid init.d]
PID="/usr/local/squid/var/run/squid.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
rm -rf $PID &> /dev/null
;;
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]
[root@squid init.d]
[root@squid init.d]
[root@squid init.d]
[root@squid init.d]
正在启动 squid....
2.3:传统代理模式(正向)
------------------------------------------配置传统代理服务器(传统模式),还在squid服务器上配置---------------------------------------------------------------------------------------------------------------
[root@squid init.d]
cache_mem 64 MB
reply_body_max_size 10 MB
maximum_object_size 4096 KB
[root@squid init.d]
[root@squid init.d]
[root@squid init.d]
[root@squid init.d]
[root@squid init.d]
[root@web ~]
[root@web ~]
[root@web ~]
[root@web ~]
1、打开谷歌浏览器
2、打开设置
3、点开高级
4、点击左下角系统
5、点击页面上的 打开您计算机的代理设置
6、手动设置代理下面设置,使用代理,地址是你的代理服务器的ip地址,端口是3128,点击保存
7、然后浏览器输入20.0.0.21,去访问web服务器
[root@web httpd]
[root@web httpd]
20.0.0.20 - - [30/Oct/2020:00:28:16 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1" 404 238 "http://20.0.0.21/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"
20.0.0.20 - - [30/Oct/2020:00:28:16 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://20.0.0.21/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"
-------从上面的就能看出,访问web服务器的是代理服务器的ip,并不是真机的ip地址
2.4:透明模式
------------------------------------------配置透明代理服务器,还在squid服务器配置---------------------------------------------------------------------------------------------------------------
在代理服务器上添加一块网卡,设置成主机模式
[root@squid ~]
[root@squid network-scripts]
[root@squid network-scripts]
NAME=ens36
DEVICE=ens36
ONBOOT=yes
IPADDR=192.168.10.3
NETMASKE=255.255.255.0
UUID、DNS都不用设置,可以删掉
[root@squid network-scripts]
[root@squid network-scripts]
net.ipv4.ip_forward=1
[root@squid network-scripts]
-------------------------------------web1端上也要配置一个回去的路由-----------------------------------------------------------------------------------------------------------------------
[root@web ~]
----------------------------------------squid服务器配置---------------------------------------------------------------------------------------------------------------------------------------------------
[root@squid network-scripts]
http_port 192.168.10.3:3128 transparent
[root@squid network-scripts]
[root@squid network-scripts]
[root@squid network-scripts]
[root@squid network-scripts]
[root@squid network-scripts]
[root@squid network-scripts]
客户端的网关填写刚刚配置的代理服务器ens36网卡的ip地址,192.168.10.3
然后浏览器输入20.0.0.21访问apache网站
在Apache服务器查看
[root@web httpd]
[root@web httpd]
20.0.0.20 - - [30/Oct/2020:03:03:55 -0400] "GET /favicon.ico HTTP/1.1" 404 209 "http://20.0.0.21/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"
只要访问的ip地址是代理服务器的就可以了!!!
2.5:ACL访问控制
--------------------------------------squid代理服务器操作,在透明模式之后做--------------------------------------------------------------------------------------
[root@squid ~]
acl hostlocal src 192.168.10.99/32
http_access deny hostlocal
[root@squid ~]
测试:客户端浏览器输入20.0.0.21就显示访问被拒绝了!
2.6:反向代理(客户端访问代理服务器就能到web服务器的网站上)
--------------------------------------------------反向代理---在web1服务器-------------------------------------------------------------------------------------------------
[root@web httpd]
[root@web html]
测试:浏览器输入:20.0.0.21,就能看见 this is test1 web
-------------------------------------------------squid代理服务器操作----------------------------------------------------------------------------------------------------------
[root@web html]
[root@web html]
[root@web html]
[root@web html]
-----------------------------------------------web2服务器操作------------------------------------------------------------------------------------------------------------
[root@localhost ~]
[root@localhost ~]
[root@web2 ~]
[root@web2 ~]
[root@web2 ~]
[root@web2 ~]
[root@web2 html]
[root@web2 html]
[root@web2 html]
-------------------------------------------------squid代理服务器操作-----------------------------------------------------------------------------------------------------------------
[root@squid ~]
http_port 20.0.0.20:80 accel vhost vport
cache_peer 20.0.0.21 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
cache_peer 20.0.0.22 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
cache_peer_domain web1 web2 www.yun.com
[root@squid ~]
测试:这个时候两个web服务器节点,通过浏览器输入20.0.0.21或20.0.0.22的网站都能访问各自的网站
----------------------------------------------------------客户端操作配置----------------------------------------------------------------------------------------------------------------------
打开目录:C:\Windows\System32\drivers\etc
编辑hosts文件
20.0.0.20 www.yun.com
------------------------------------------------------------------------squid代理服务器操作-----------------------------------------------------------------------------------------------------------------
[root@squid ~]
[root@squid ~]
[root@squid ~]
正在启动 squid....
[root@squid ~]
tcp 0 0 20.0.0.20:80 0.0.0.0:* LISTEN 45728/(squid-1)
- 测试:客户端浏览器输入www.yun.com就会显示this is test1 web,在刷新就是this is test2 web,轮询显示!!!
2.7:sarg日志分析
[root@squid ~]
[root@squid ~]
[root@squid sarg-2.3.7]
[root@squid sarg-2.3.7]
[root@squid sarg-2.3.7]
--sysconfdir=/etc/sarg \
--enable-extraprotection
[root@squid sarg-2.3.7]
[root@squid sarg-2.3.7]
[root@squid sarg]
shift+: 输入set nu,开启行数显示,这样更快定位
7行 access_log /usr/local/squid/var/logs/access.log
25行 title "Squid User Access Reports"
120行 output_dir /var/www/html/squid-reports
178行 user_ip no
184行 topuser_sort_field connect reverse
206行 exclude_hosts /usr/local/sarg/noreport
257行 overwrite_report no
289行 mail_utility mailq.postfix
434行 charset UTF-8
518行 weekdays 0-6
525行 hours 0-23
633行 www_document_root /var/www/html
添加不计入站点文件,添加的域名将不被显示在排序中
[root@squid sarg]
[root@squid sarg]
[root@squid sarg]
SARG: Records in file: 1796, reading: 100.00%
SARG: Successful report generated on /var/www/html/squid-reports/2020Oct30-2020Oct31
[root@squid ]
[root@squid squid-reports]
-----------------------------------------周期性计划任务执行每天生成报告----------------------------------
每敲下面一次代码就生成一次报告,比较麻烦
[root@squid ~]
-o:输出/var/www/html/squid-reports/目录当中
-d:指定时间和日期,这里是一天的时间,但是后面加上了-$(date +%d/%m/%Y),就是前一天的
-------------------------------------想定时生成报告要用crontab -e计划性周期任务--------------------------------------------
crontab -e 计划性周期任务
0 0 * * * /bin/bash /usr/local/bin/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)
客户端浏览器输入192.168.10.3/squid-reports/index.html