Squid proxy server-traditional proxy, transparent proxy
1. Overview of Caching Proxy
1. Function
Cache web page objects to reduce repeated requests
2. Process diagram
3. Agency type
传统代理:适用于Internet,需明确指定服务端
透明代理:客户机不需要指定代理服务器的地址和端口,是通过默认路由,防火墙将web重定向给代理
4. The benefits of using a proxy
提高web访问速度
隐藏客户机的真实IP地址
2. Traditional agency
1. Experimental environment
squid服务器:192.168.52.11
web服务器1:192.168.52.12
web服务器2:192.168.52.13
客户机
2. Install squid software
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 //Check grammar
3. Optimize startup items
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. Set up a traditional proxy server
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
Proxy server routing configuration
iptables -F
iptables -I INPUT -p tcp --dport 3128 -j ACCEPT //允许3128端口
5. Configure the web node server
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. Client authentication method
Visit web1 server address
Visit web2 server address
View server log verification
web1
web2
3. Transparent agency
1. Experimental environment
squid服务器 ens33:192.168.52.11
ens36:192.168.1.11(仅主机模式)
web服务器 192.168.52.12
client 192.168.1.14 (仅主机模式)
2. Experimental topology
3. Add a network card to the squid service and set the ip address
[]service network restart ##重启网络服务
[] vim /etc/sysctl.conf ##开启路由转发
net.ipv4.ip_forward=1
[] sysctl -p ##加载
4. Specify a static route on the web server
route add -net 192.168.10.0/24 gw 192.168.13.184
5. Set up a transparent proxy on the squid server
[] 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. View the access log file on the web server
[root@web ~]# cd /var/log/httpd/
[root@web httpd]# vim access_log ##查看访问日志信息
Four. ACL access control
1. Experimental environment
squid服务器 ens33:192.168.13.184
ens36:192.168.10.1 (仅主机模式)
web服务器 192.168.13.151
client 192.168.10.10 (仅主机模式)
2. Configuration process
1. Modify the configuration file on the squid server
[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. Visit the web page on the test machine
Five, sarg log
1. Install sarg on the squid server
[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. Modify the sarg configuration file
[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. Use the test machine to access the web page to view the access record
Periodic scheduled task execution generates crontab report every day
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)
Six, squid reverse proxy
1. Environment
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. Edit the content of a web page on the web1 server
[root@web ~]# cd /var/www/html/
[root@web html]# vim index.html ##编辑网页内容
<h1>this is test 01 web1!</h1>
3. Visit the web page on the test machine
4. Edit the content of a web page on the web2 server
[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. Configure the reverse proxy on the squid service
[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服务