squid:20.0.0.10
web: 20.0.0.11
client:20.0.0.13
(1)安装依赖环境:
[root@Squid ~]# yum -y install gcc gcc-c++ make
(2)编译安装Squid服务
[root@Squid ~]# tar zxf squid-3.5.23.tar.gz -C /opt
[root@Squid ~]# cd /opt/squid-3.5.23/
[root@Squid squid-3.5.23]# ./configure --prefix=/usr/local/squid \
> --sysconfdir=/etc \ ###指定配置文件位置
> --enable-arp-acl \ ###支持acl访问控制列表
> --enable-linux-netfilter \ ###打开网络筛选
> --enable-linux-tproxy \ ###支持透明代理
> --enable-async-io=100 \ ###io优化
> --enable-err-language="Simplify_Chinese" \ ###报错显示简体中文
> --enable-underscore \ ###支持下划线
> --enable-poll \ ###默认使用poll模式,开启epoll模式时提升性能
> --enable-gnuregex ###支持正则表达式
[root@Squid squid-3.5.23]# make && make install
(3) 创建软连接
[root@Squid squid-3.5.23] ln -s /usr/local/squid/sbin/* /usr/local/sbin
(4)创建不可登录的用户,授权
[root@Squid squid-3.5.23]useradd -M -s /sbin/nologin squid
[root@Squid squid-3.5.23] chown -R squid.squid /usr/local/squid/var
(5)修改配置文件,优化启动项
[root@Squid ~] vi /etc/squid.conf
cache_effective_user squid #添加指定程序用户
cache_effective_group squid #添加指定账号基本组
[root@Squid ~] squid -k parse ###检查配置文件语法
[root@Squid ~] squid -z ###初始化缓存目录
[root@Squid ~] squid ###启动服务
[root@Squid ~]netstat -anpt | grep squid
tcp6 0 0 :::3128 :::* LISTEN 104314/(squid-1)
(6)编写squid脚本,执行
#!/bin/bash
#chkconfig: 2345 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 -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
[root@Squid ~] chmod +x /etc/init.d/squid
[root@Squid ~] chkconfig --add squid
[root@Squid ~] chkconfig --level 35 squid on
(7) 配置传统代理
[root@Squid ~] vi /etc/squid.conf
http_access allow all ##允许所有
http_access deny all
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 ~] iptables -F
[root@Squid ~] iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@Squid ~] systemctl restart squid
[root@web ~]systemctl stop firewalld ## 关闭防火墙
[root@web ~] setenforce 0 ## 关闭核心防护
[root@web ~] yum -y install httpd ## 安装apache服务
[root@web ~] systemctl start httpd
[root@web ~] systemctl enable httpd
[root@web ~] netstat -anpt | grep httpd
tcp6 0 0 :::80 :::* LISTEN 40633/httpd
(1) 看客户机能否成功访问Apache首页,可以去web服务器看到时客户机访问我的
(2)是客户机自身IP
8、设置Squid代理,查看来访IP变化
1、先清除浏览器缓存
2、在浏览器中设置代理
内网(nat) :20.0.0.10
外网(nat1): 192.168.100.10
[root@squid ~] cd /etc/sysconfig/network-scripts/
[root@squid network-scripts] cp -p ifcfg-ens33 ifcfg-ens37
[root@squid network-scripts] vim ifcfg-ens36
NAME=ens37 #名称改了
DEVICE=ens37 ###名称改了
ONBOOT=yes
IPADDR=192.168.100.10 ###ip地址改了
NETMASKE=255.255.255.0 ###子网掩码
UUID、DNS都不用设置,可以删掉
[root@squid network-scripts] systemctl restart network ###重启一下网卡
[root@squid network-scripts] vim /etc/sysctl.conf
net.ipv4.ip_forward=1 ###开启路由转发功能,1是开启,0是不开启
[root@squid network-scripts] sysctl -p ###加载一下
net.ipv4.ip_forwar
[root@squid network-scripts] vim /etc/squid.conf
http_port 20.0.0.10:3128 transparent ###把3182端口的地方改成透明模式
[root@squid network-scripts] service squid start ###重启服务
[root@squid network-scripts] iptables -F ###清除一下filter表防火墙规则(不指定表,默认是filter表)
[root@squid network-scripts] iptables -t nat -F ###清除一下nat表防火墙规则
[root@squid network-scripts] iptables -t nat -I PREROUTING -i ens33 -s 20.0.0.0/24 -p tcp --dport 80 -j REDIRECT --to 3128 ###添加一个80端口访问的规则
[root@squid network-scripts] iptables -t nat -I PREROUTING -i ens33 -s 20.0.0.0/24 -p tcp --dport 443 -j REDIRECT --to 3128 ###访问的是https的就要改成443端口
[root@squid network-scripts] iptables -I INPUT -p tcp --dport 3128 -j ACCEPT ###可以进行一个转发
[root@web ~] route add -net 20.0.0.0/24 gw 192.168.100.10
[root@squid ~] vim /etc/squid.conf
acl host src 20.0.0.13/32 ## 在squid代理服务器的acl访问列表添加一条访问20.0.0.13的记录
http_access deny host ## 然后拒绝这条访问,这样squid代理服务器就访问不了20.0.0.13了,同时也获取不到web资源
[root@squid ~] systemctl restart squid
[root@squid ~] netstat -anpt | grep 3128
tcp 0 0 192.168.100.2:3128 0.0.0.0:* LISTEN 9088/(squid-1)
[root@Squid ~] yum -y install gd ###图像处理
[root@Squid ~] mkdir /usr/local/sarg
[root@Squid ~] tar zxf sarg-2.3.7.tar.gz -C /opt
[root@Squid ~] cd /opt/sarg-2.3.7/
[root@Squid sarg-2.3.7] ./configure \
> --prefix=/usr/local/sarg \
> --sysconfdir=/etc/sarg \ ###配置文件目录,默认是/usr/local/etc
> --enable-extraprotection ###添加额外的安全保护
[root@Squid sarg-2.3.7] make && make install
[root@Squid ~]# vi /etc/sarg/sarg.conf
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 //top排序中有连接次数、访问字节、降序排列 升序是normal
注释掉)190/ user_sort_field 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 //top排行的星期周期
525/ hours 0-23 //top排行的时间周期
633/ www_document_root /var/www/html //网页根目录
[root@Squid ~] touch /usr/local/sarg/noreport
[root@squid ~] ln -s /usr/local/sarg/bin/sarg /usr/local/bin/
[root@squid ~] sarg
SARG: 纪录在文件: 312, reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/squid-reports/2020Nov11-2020Nov11
[root@Squid ~] yum -y install httpd
[root@Squid ~] systemctl start httpd
[root@squid ~]# 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)
SARG: TAG: access_log /usr/local/squid/var/logs/access.log
SARG: TAG: title "Squid User Access Reports"
SARG: TAG: output_dir /var/www/html/squid-reports
SARG: TAG: user_ip no
SARG: TAG: topuser_sort_field connect reverse
SARG: TAG: user_sort_field BYTES reverse
SARG: TAG: exclude_hosts /usr/local/sarg/noreport
SARG: TAG: overwrite_report no
SARG: TAG: mail_utility mailq.postfix
SARG: TAG: charset UTF-8
SARG: TAG: weekdays 0-6
SARG: TAG: hours 0-23
SARG: TAG: www_document_root /var/www/html
SARG: 纪录在文件: 354, reading: 100.00%
SARG: 期间被日志文件覆盖: 11/11/2020 - 11/11/2020
SARG: (info) date=11/11/2020
SARG: (info) period=2020 11月 10-2020 11月 11
SARG: (info) outdirname=/var/www/html/squid-reports//2020Nov10-2020Nov11
SARG: (info) Dansguardian report not produced because no dansguardian configuration file was provided
SARG: (info) No redirector logs provided to produce that kind of report
SARG: (info) No downloaded files to report
SARG: (info) Authentication failures report not produced because it is empty
SARG: (info) Redirector report not generated because it is empty
SARG: 成功的生成报告在 /var/www/html/squid-reports//2020Nov10-2020Nov11
[root@Squid ~]# cd /var/www/html/squid-reports/
[root@Squid squid-reports]# ll
总用量 8
drwxr-xr-x. 4 root root 201 11月 8 13:44 2020Nov07-2020Nov08
drwxr-xr-x. 4 root root 201 11月 8 12:14 2020Nov07-2020Nov08.1
drwxr-xr-x. 2 root root 92 11月 8 12:14 images
-rw-r--r--. 1 root root 4690 11月 8 13:44 index.html
在透明模式的基础上进行反向代理
因为httpd会占用80端口,所以必须关闭squid服务器中的httpd服务
[root@Squid ~] iptables -F
[root@Squid ~] iptables -t nat -F
[root@Squid ~] iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@Squid ~] vi /etc/squid.conf
# Squid normally listens to port 3128
http_port 192.168.100.10:80 accel vhost vport ###squid外网口IP
cache_peer 192.168.73.11 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
cache_peer 192.168.73.14 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 ~] systemctl restart squid
[root@Web1 ~] yum -y install httpd
[root@Web1 ~] echo "this is test1 web
" > /var/www/html/index.html
[root@Web1 ~] systemctl start httpd
[root@Web1 ~] netstat -anpt | grep httpd
tcp6 0 0 :::80 :::* LISTEN 50552/httpd
[root@Web1 ~] route add -net 20.0.0.0/24 gw 192.168.100.10 ###添加静态路由
[root@Web2 ~] yum -y install httpd
[root@Web2 ~] echo "this is test2 web
" > /var/www/html/index.html
[root@Web2 ~] systemctl start httpd
[root@Web2 ~] netstat -anpt | grep httpd
tcp6 0 0 :::80 :::* LISTEN 14645/httpd
[root@Web2 ~] route add -net 20.0.0.0/24 gw 192.168.100.10
[root@server ~] vi /etc/hosts
192.168.73.10 www.yun.com