一、Squid 简介
Squid 主要提供缓存加速、应用层过滤控制的功能,能代替客户机向网站请求数据,从而可以隐藏用户的真实 IP 地址。将获得的网页数据(静态 Web 元素)保存到缓存中并发送给客户机,以便下次请求相同的数据时快速响应
二、Squid 的安装与配置
1. 编译安装
环境准备
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
安装依赖环境
yum -y install gcc gcc-c++ make
编译安装
./configure --prefix=/usr/local/squid #指定安装目录
--sysconfdir=/etc/ #指定配置文件路径
--enable-arp-acl #可在 ACL 中设置通过 MAC 地址进行管理,防止 IP 欺骗
--enable-linux-netfilter #使用内核过滤
--enable-linux-tproxy #支持透明模式
--enable-async-io=100 #异步 I/O,提升储存性能,值可修改
--enable-err-language="Simplify_Chinese" #错误信息的显示语言
--enable-underscore #允许 URL 中有下划线
--enable-poll #使用 poll() 模式,提升性能
--enable-gnuregex #使用 GNU 正则表达式
2. 修改 Squid 的配置文件
vim /etc/squid.conf
# And finally deny all other access to this proxy
#56行插入
http_access allow all #放在 http_access deny all 之前,允许任意客户机使用代理服务,控制规则自上而下匹配
http_access deny all
# Squid normally listens to port 3128
http_port 3128 #用来指定代理服务监听的地址和端口(默认的端口号为 3128)
#61行插入
cache_effective_user squid #添加,指定程序用户,用来设置初始化、运行时缓存的账户,否则启动不成功
cache_effective_group squid #添加,指定账号基本组
#68行,缓存文件目录,默认为 /usr/local/squid/var/cache/squid,这里不做修改
coredump_dir /usr/local/squid/var/cache/squid
3. Squid 的运行控制
#检查配置文件语法是否正确
squid -k parse
#-z 选项用来初始化缓存目录,第一次启动 Squid 服务时,会自动初始化缓存目录
squid -z
#启动 squid 服务
squid
#squid 端口号为 tcp 3128
netstat -anpt | grep "squid"
#定期清理 swap.state 内无效数据
squid -k rotate -f /etc/squid.conf
4. 创建 Squid 服务脚本
#!/bin/bash
##解释器
#chkconfig: 2345 90 25
##2345为默认自启动级别,如是 - 代表任何级别都不自启动;90为启动优先级,25为停止优先级,优先级范围是0-100,数字越大,优先级越低
PID="/usr/local/squid/var/run/squid.pid"
##指定进程文件路径
CONF="/etc/squid.conf"
##指定配置文件路径
CMD="/usr/local/squid/sbin/squid"
##指定程序文件路径
case "$1" in
start)
netstat -natp | 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 -natp | 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|status|reload|check|restart}"
;;
esac
#给该服务启动脚本可执行权限
chmod +x /etc/init.d/squid
#将该服务加入 chkconfig 管理
chkconfig --add squid
#能够在级别3(字符界面),级别5(视图界面)中自启动
chkconfig --level 35 squid on
#查看运行级别
chkconfig --list squid
5. 脚本安装
#!/bin/bash
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
yum -y install gcc gcc-c++ make
wget http://101.34.22.188/squid/squid-3.5.28.tar.gz -P /opt
tar zxvf /opt/squid-3.5.28.tar.gz -C /opt
cd /opt/squid-3.5.28/
./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 -j 4 && make install
ln -s /usr/local/squid/sbin/* /usr/local/sbin
useradd -M -s /sbin/nologin squid
chown -R squid:squid /usr/local/squid/var/
三、Squid部署配置
1. 修改 Squid 配置文件
vim /etc/squid.conf
# And finally deny all other access to this proxy
http_access allow all
http_access deny all
# Squid normally listens to port 3128
http_port 3128
cache_effective_user squid
cache_effective_group squid
#63行插入
cache_mem 64 MB
#指定缓存功能所使用的内存空间大小,便于保持访问较频繁的WEB对象,容量最好为4的倍数,单位为MB,建议设为物理内存的1/4
reply_body_max_size 10 MB
#允许用户下载的最大文件大小,默认以字节为单位,当下载超过指定大小的Web对象时,浏览器的报错页面中会出现“请求或访问太大”的提示默认设置0表示不进行限制
maximum_object_size 4096 KB
#允许保存到缓存空间的最大对象大小,以KB为单位,超过大小限制的文件将不被缓存,而是直接转发给用户
http_port 3128 #监听端口 (还可以只监听一个IP http_port 192.168.0.1:3128)
cache_mem 64MB #缓存占内存大小
maximum_object_size 4096KB #最大缓存块
reply_body_max_size 1024000 allow all #限定下载文件大小
access_log /var/log/squid/access.log #访问日志存放的地方
visible_hostname proxy.test.xom #可见的主机名
cache_dir ufs /var/spool/squid 100 16 256
\#ufs:缓存数据的存储格式
\#/var/spool/squid 缓存目录
\#100:缓存目录占磁盘空间大小(M)
\#16:缓存空间一级子目录个数
\#256:缓存空间二级子目录个数
cache_mgr webmaster@test.com #定义管理员邮箱
http_access deny all #访问控制
2. 修改防火墙规则
#清空原防火墙规则
iptables -F
#不指定表将默认配置filter表,设置INPUT链规则,指定协议为tcp,指定端口3128,目标动作为ACCEPT接受
iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
3. Web 服务器配置
systemctl stop firewalld.service
setenforce 0
yum -y install httpd
systemctl start httpd && systemctl enable httpd
netstat -natp | grep 80
3. Web 服务器配置
systemctl stop firewalld.service
setenforce 0
yum -y install httpd
systemctl start httpd && systemctl enable httpd
netstat -natp | grep 80
5. 查看 Squid 访问日志的新增记录
[root@squid_server ~]#tail -f /usr/local/squid/var/logs/access.log
1635941054.668 0 192.168.10.85 TCP_MISS/404 479 GET http://192.168.10.30/noindex/css/fonts/Light/OpenSans-Light.ttf - HIER_DIRECT/192.168.10.30 text/html
1635941054.682 1 192.168.10.85 TCP_MISS/404 448 GET http://192.168.10.30/favicon.ico - HIER_DIRECT/192.168.10.30 text/html
1635941054.861 573 192.168.10.85 TCP_TUNNEL/200 10451 CONNECT nav.smartscreen.microsoft.com:443 - HIER_DIRECT/13.67.52.249 -
1635941055.406 530 192.168.10.85 TCP_TUNNEL/200 13204 CONNECT smartscreen-prod.microsoft.com:443 - HIER_DIRECT/13.67.52.249 -
1635941076.365 120455 192.168.10.85 TCP_TUNNEL/200 16575 CONNECT img-operation.csdnimg.cn:443 - HIER_DIRECT/27.221.119.230 -
1635941084.927 125423 192.168.10.85 TCP_TUNNEL/200 77330 CONNECT edge.microsoft.com:443 - HIER_DIRECT/204.79.197.219 -
1635941088.210 128772 192.168.10.85 TCP_TUNNEL/200 6561 CONNECT edge.microsoft.com:443 - HIER_DIRECT/204.79.197.219 -
1635941136.325 180278 192.168.10.85 TCP_TUNNEL/200 4795 CONNECT bizapi.youkuaiyun.com:443 - HIER_DIRECT/39.97.4.86 -
1635941177.583 138594 192.168.10.85 TCP_TUNNEL/200 26684 CONNECT www.bing.com:443 - HIER_DIRECT/202.89.233.101 -
1635941178.794 139538 192.168.10.85 TCP_TUNNEL/200 24404 CONNECT cn.bing.com:443 - HIER_DIRECT/202.89.233.100 -
|
如果显示 TCP_MISS 表示 Squid 没有请求资源的 cache 拷贝。HIER_DIRECT 将请求转发至 web 服务器。 |
6. 查看 Web 访问日志的新增记录
[root@web_server ~]#tail -f /var/log/httpd/access_log
192.168.10.20 - - [03/Nov/2021:20:08:01 +0800] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.50"
192.168.10.20 - - [03/Nov/2021:20:08:01 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.woff HTTP/1.1" 404 241 "http://192.168.10.30/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.50"
192.168.10.20 - - [03/Nov/2021:20:08:01 +0800] "GET /noindex/css/fonts/Bold/OpenSans-Bold.woff HTTP/1.1" 404 239 "http://192.168.10.30/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Edg/94.0.992.50"
147

被折叠的 条评论
为什么被折叠?



