Squid传统模式等等

摘要
1、Squid是基于Unix的代理服务器(proxy server),它缓存比起点源点更接近请求者的互联网内容。Squid支持缓存多种不同的网络对象,包括那些通过HTTP和FTP访问的人。缓存频繁要求网页、媒体文件和其它加速回答时间并减少带宽堵塞的内容。
2、Squid代理服务器(Squid proxy server)一般和原始文件一起安装在单独服务器而不是网络服务器上。Squid通过追踪网络中的对象运用起作用。Squid最初担当中介,仅仅是把客户要求传递到服务器并存储要求对象的拷贝。如果同一个客户或同一批客户在要求还在Squid缓存(cache)时要求相同的对象,Squid就可以立刻服务,加速下载并保存带宽。
3、squid是一种用来缓存Internet数据的软件。接受来自人们需要下载的目标(object)的请求并适当的处理这些请求。也就是说,如果一个人想下载一web界面,他请求squid为他取得这个页面。squid随之连接到远程服务器并向这个页面发出请求。然后,squid显式地聚集数据到客户端机器,而且同时复制一份。当下一次有人需要同一页面时, squid可以简单的从磁盘中读到它,那样数据会立即传输到客户机子上。

一、缓存代理概述
1.1、Squid代LI的工作机制

缓存网页对象,减少重复请求在这里插入图片描述
当代理服务器中有客户端需要的数据时:
a. 客户端向代理服务器发送数据请求;
b. 代理服务器检查自己的数据缓存;
c. 代理服务器在缓存中找到了用户想要的数据,取出数据;
d. 代理服务器将从缓存中取得的数据返回给客户端。
当代理服务器中没有客户端需要的数据时:
a. 客户端向代理服务器发送数据请求;
b. 代理服务器检查自己的数据缓存;
c. 代理服务器在缓存中没有找到用户想要的数据;
d. 代理服务器向Internet 上的远端服务器发送数据请求;
f. 远端服务器响应,返回相应的数据;
g. 代理服务器取得远端服务器的数据,返回给客户端,并保留一份到自己的数据缓存中。

1.2、代理的基本类型

1.2.1、传统代理:适用于Internet,需明确指定服务端

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

1.3、使用代理的好处

1.3.1、提高Web访问速度

1.3.2、隐藏客户机的真实IP地址

二、squid主要组成部分在这里插入图片描述
三、Squid各种代理的定义
3.1、传统代理
环境在这里插入图片描述
3.1.1、Squid服务器配置

①安装依赖环境

yum -y install gcc gcc-c++ make

②编译安装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

③优化路径

1 [root@squid squid-3.5.23]# ln -s /usr/local/squid/sbin/* /usr/local/sbin 
2 [root@squid squid-3.5.23]# useradd -M -s /sbin/nologin squid ###创建不可登录的程序用户
3 [root@squid squid-3.5.23]# chown -R squid.squid /usr/local/squid/var

④修改配置文件,优化启动项

[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)

⑤添加服务到service管理

[root@squid ~]# vi /etc/init.d/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

⑥配置传统代理

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

[root@squid ~]# setenforce 0
[root@squid ~]# iptables -F
[root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@squid ~]# systemctl restart squid

3.1.2、Web服务器配置

安装httpd,并设置默认网页内容

[root@web ~]# yum -y install httpd
[root@web ~]# cd /var/www/html/
[root@web html]# vi index.html
<h1>this is a web!!!</h1>
[root@web html]# systemctl restart httpd
[root@web html]# netstat -anpt | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      3432/httpd
[root@web html]# curl http://localhost
<h1>this is web!!!</h1>

3.1.3、客户机测试在这里插入图片描述
3.1.4、查看日志文件,看访问的IP

1 [root@web html]# cat /var/log/httpd/access_log 
2 20.0.0.30 - - [09/Nov/2020:14:42:40 +0800] "GET / HTTP/1.1" 200 24 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
3 20.0.0.30 - - [09/Nov/2020:14:42:40 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
4 20.0.0.30 - - [09/Nov/2020:14:42:40 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"

是客户机的IP
3.1.5、再网页上进行代理配置并测试在这里插入图片描述
在这里插入图片描述
3.1.6、查看日志文件,看访问的IP

1 [root@web html]# cat /var/log/httpd/access_log 
2 20.0.0.10 - - [09/Nov/2020:14:54:52 +0800] "GET / HTTP/1.1" 200 24 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
3 20.0.0.10 - - [09/Nov/2020:14:54:53 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"

成功变成Squid服务器的IP

3.2、透明代理
在搭建的传统代理基础上做如下修改:

①squid服务器添加一块网卡:192.168.100.10(仅主机模式);开启路由转发功能,开启透明代理;配置防火墙规则;

②web服务器不变;

③客户端IP地址修改为192.168.100.20,且浏览器关闭手动代理设置在这里插入图片描述
3.2.1、Squid服务器配置

①开启路由功能

1 [root@squid ~]# vi /etc/sysctl.conf
2 net.ipv4.ip_forward = 1           #末尾添加
3 [root@squid ~]# sysctl -p
4 net.ipv4.ip_forward = 1

②修改配置文件

1 [root@squid ~]# vi /etc/squid.conf
2 http_port 192.168.100.10:3128 transparent
3 [root@squid ~]# systemctl restart squid.service
4 [root@squid squid-3.5.23]# netstat -anpt | grep squid
5 tcp        0      0 192.168.100.10:3128     0.0.0.0:*               LISTEN      114617/(squid-1) 

③设置防火墙规则

1 [root@squid ~]# iptables -F
2 [root@squid ~]# iptables -t nat -F
3 [root@squid ~]# iptables -t nat -I PREROUTING -i ens37 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
4 [root@squid ~]# iptables -t nat -I PREROUTING -i ens37 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
5 [root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT

3.2.2、web端设置

1 [root@web ~]# route add -net 192.168.100.0/24 gw 20.0.0.10 ###添加一条静态路由

3.2.3、客户机测试(客户机网关要设置成Squid内网网关IP)

①网页修改为不使用代理在这里插入图片描述
②访问并查看日志在这里插入图片描述

1 [root@web html]# cat /var/log/httpd/access_log 
2 20.0.0.10 - - [09/Nov/2020:15:52:02 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
3 20.0.0.10 - - [09/Nov/2020:16:06:40 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"

四、ACL控制
使用acl访问控制列表,禁止客户机访问web服务器

4.1、修改配置文件

[root@squid squid-3.5.23]# vi /etc/squid.conf
acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines
acl host     src 192.168.100.20/32     #添加

# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny host               #添加
http_access deny manager

[root@squid squid-3.5.23]# systemctl restart squid
[root@squid squid-3.5.23]# netstat -anpt | grep squid
tcp        0      0 192.168.100.10:3128     0.0.0.0:*               LISTEN      115270/(squid-1)

4.2、测试在这里插入图片描述
五、Squid日志分析
5.1、安装依赖环境

1 [root@Squid ~]# yum -y install gd  ###图像处理

5.2、编译安装日志分析软件

[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

5.3、修改配置文件

[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          //使用用户名显示

206/ exclude_hosts /usr/local/sarg/noreport   //不计入排序的站点列表文件

184/ topuser_sort_field connect reverse   //top排序中有连接次数、访问字节、降序排列 升序是normal

(注释掉)190/ #  user_sort_field reverse    //用户访问记录 连接次数、访问字节按降序排序

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  //网页根目录

5.4、添加不计入站点文件,添加的域名将不被显示在排序中

1 [root@squid ~]# touch /usr/local/sarg/noreport

5.5、优化启动项并启动服务

1 [root@squid sarg-2.3.7]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin
2 [root@squid sarg-2.3.7]# sarg
3 SARG: 纪录在文件: 214, reading: 100.00%
4 SARG: 成功的生成报告在 /var/www/html/squid-reports/2020Nov09-2020Nov09

5.6、安装启动httpd服务

1 [root@squid ~]# yum -y install httpd
2 [root@squid ~]# systemctl start httpd

5.7、查看报告在这里插入图片描述
在这里插入图片描述
5.8、做周期性计划任务crontab使其每天生成报告

[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 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: 纪录在文件: 127, reading: 100.00%
SARG: 期间被日志文件覆盖: 07/11/2020 - 08/11/2020
SARG: (info) date=08/11/2020
SARG: (info) period=2020 11月 07-2020 11月 08
SARG: (info) outdirname=/var/www/html/squid-reports//2020Nov07-2020Nov08
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//2020Nov07-2020Nov08

[root@squid ~]# crontab -e
30 22 * * * 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)

测试在浏览器输入 20.0.0.10/squid-reports/,又会出现一条新的访问记录,然后查看/var/www/html/squid-reports文件
[root@squid ~]# cd /var/www/html/squid-reports/
[root@squid squid-reports]# ll
drwxr-xr-x. 5 root root  213 11月  9 17:34 2020Nov08-2020Nov09
drwxr-xr-x. 5 root root  213 11月  9 17:10 2020Nov09-2020Nov09
drwxr-xr-x. 2 root root   92 11月  9 17:10 images
-rw-r--r--. 1 root root 4686 11月  9 17:34 index.html

六、Squid反向代理
在透明模式的基础上进行反向代理

因为httpd会占用80端口,所以必须关闭squid服务器中的httpd服务

6.1、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 ~]# netstat -anpt | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      50552/httpd

[root@web1 ~]# route add -net 192.168.100.0/24 gw 20.0.0.10 ###添加静态路由

6.2、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 ~]# netstat -anpt | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      50552/httpd

[root@web2 ~]# route add -net 192.168.100.0/24 gw 20.0.0.10 ###添加静态路由

6.3、Squid配置

[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 20.0.0.10:80 accel vhost vport ###squid外网口IP
cache_peer 20.0.0.20 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
cache_peer 20.0.0.40 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
cache_peer_domain web1 web2 www.xuhao.com

[root@squid ~]# systemctl restart squid

6.4、测试
在这里插入图片描述
在这里插入图片描述

1 客户机需要添加hosts文件
2.vi /etc/hosts
20.0.0.10 www.xuhao.com

在这里插入图片描述
在这里插入图片描述
可以看出web1和web2是轮询访问的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值