• 【Linux集群教程】03 多级负载均衡 & Squid


    1 多级负载均衡

    1.1 实现原理

    img

    首先,客户端会发起tcp的连接到nginx,nginx会把连接翻译成它想要获取的数据,然后nginx想后端apache服务器建立完整的tcp连接访问,获取完数据后,然后把数据传送给客户端,以反向代理方式实现负载均衡。

    Nginx 会有识别主机名和FQDN名称的能力。Nginx 可以按照域名的方式进行负载均衡(Nginx 七层负载均衡)。Nginx 需要建立起两次TCP连接。Nginx 的处理数据的压力较大。

    1.1.1 四层负载实现原理

    img

    四层采用了LVS-DR模式实现的,客户端发送数据报文到LVS,LVS去判断端口和访问的VIP(虚拟服务IP),只要这两个匹配,就会按照自己的调度规则,把请求的数据包转移后台的真实服务器上,对应服务器接收请求后,反应数据给用户。四层负载只看端口和VIP,不看域名。

    如果我们需要大并发就需要使用 四层负载均衡,如果是需要识别域名 即需要进行虚拟主机识别的情况 就使用七层负载均衡。

    1.1.2 多级挂载(四、七层)原理

    img

    需求:公司有两个不同域名的门户网站,业务高峰期访问量较大,经测试nginx未能满足并发压力,两个门户网站公网地址一致?

    解决方案:这个时候就要用到多级负载

    两个网站,不同的域名,相同的IP,所以采用以上的方法;Nginx作为反向代理服务器,LVS对 Nginx 采用DR负载均衡

    客户端发送数据报文到LVS,LVS根据算法匹配到第一个nginx或者第二个nginx,nginx里面去设置虚拟主机级别的负载均衡,判断访问的主机名是谁,如果是 .com 的话,会在服务器1和2之间负载均衡,如果是 .cn 的话,会直接去第三层。

    1.2 多级负载构建

    img

    生产环境中可以将 Nignx 服务器进行水平扩展即可。只要 LVS 的压力没有瓶颈就可以了。

    实验环境中全部关闭防火墙和selinux,配置本地yum源

    1.2.1 构建三台Apache Web 服务器

    除了网页内容不一样,其余全部一样,三台服务都执行一遍

    ###10.10.10.14,10.10.10.15,10.10.10.16
    #三台服务器同时执行
    yum install -y httpd 
    #CentOS 7启动服务httpd
    systemctl enable --now httpd &
    #CentOS 6启动服务httpd
    service httpd start
    chkconfig httpd on 
    
    #10.10.10.14编写网页
    echo "www.kubernetes.com-1" > /var/www/html/index.html
    #10.10.10.15编写网页
    echo "www.kubernetes.com-2" > /var/www/html/index.html
    #10.10.10.16编写网页
    echo "www.kubernetes.cn" > /var/www/html/index.html
    
    #各自访问各自的网页
    curl 127.0.0.1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    这样就完成了

    1.2.2 构建两台 Nginx 服务器

    ### 10.10.10.12 和 10.10.10.13 配置相同
    #CentOS 7的操作
    #CentOS 7通过在 nginx 官网进行下载
    #http://nginx.org/ | http://nginx.org/en/download.html | http://nginx.org/download/nginx-1.22.0.tar.gz
    tar -zxvf nginx-1.22.0.tar.gz -C /opt/
    cd /opt/nginx-1.22.0
    useradd -s /sbin/nologin -M nginx
    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
    make && make install
    echo $?
    
    #所需软件百度网盘
    #链接:https://pan.baidu.com/s/1wfgE3ISFsBddoO2JuMdRiA
    #提取码:ton9
    #CentOS 6的软件包进行下载上传并编译安装即可
    #CentOS 6通过所需的软件进行源码包安装
    #挂载yum光盘
    $ mount -t iso9660 /dev/cdrom /mnt/cdrom 
    #方便把软件直接拖到xshell里面
    $ yum -y install lrzsz 
    
    $ tar -zxvf nginx-1.2.6.tar.gz && cd nginx-1.2.6
    #安装依赖
    $ yum -y install pcre pcre-devel zlib zlib-devel  
    
    $ useradd -s /sbin/nologin -M nginx
    #不允许远程登陆,-M 不创建家目录
    $ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
    #编译安装
    $ make && make install 
    #修改配置文件
    $ vim /usr/local/nginx/conf/nginx.conf
    ###将多余的部分删除后,得到的内容
    #user  nobody;
    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
        keepalive_timeout  65;
    
        server {
            listen       80;
            server_name  localhost;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
        }
    }
    #对nginx.conf配置文件进行修改后得到的内容
    #user  nobody;
    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
    
        keepalive_timeout  65;
    
        upstream kubernetes.com {
            server 10.10.10.14:80;
            server 10.10.10.15:80;
        }
    
        upstream kubernetes.cn {
            server 10.10.10.16:80;
        }
    
        server {
            listen       80;
            server_name  www.kubernetes.com;
    
            location / {
                    proxy_pass http://kubernetes.com;
            }
        }
    
        server {
            listen       80;
            server_name  www.kubernetes.cn;
    
            location / {
                    proxy_pass http://kubernetes.cn;
            }
        }
    }
    #检查Nginx配置文件是否有问题
    /usr/local/nginx/sbin/nginx -t
    #手动开启Nginx服务
    /usr/local/nginx/sbin/nginx
    
    ###CentOS 6 Nginx服务操作
    service nginx start 
    service nginx stop
    ###CentOS 7 Nginx服务操作
    systemctl enable --now nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110

    修改客户端Windows 的hosts 文件

    #打开Windows 系统的 hosts 文件,并添加以下内容即可(C:\Windows\System32\drivers\etc)
    # Nginx Host BEGIN
    10.10.10.12 www.kubernetes.com
    10.10.10.12 www.kubernetes.cn
    # Nginx Host END
    
    #修改hosts文件,生产环境搭建DNS服务器
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    客户端访问 http://www.kubernetes.com(强制刷新)

    img

    img

    客户端访问 http://www.kubernetes.cn

    img

    #打开Windows 系统的 hosts 文件,并添加以下内容即可(C:\Windows\System32\drivers\etc)
    # Nginx Host BEGIN
    10.10.10.13 www.kubernetes.com
    10.10.10.13 www.kubernetes.cn
    # Nginx Host END
    
    • 1
    • 2
    • 3
    • 4
    • 5

    客户端访问 http://www.kubernetes.com(强制刷新)

    img

    img

    客户端访问 http://www.kubernetes.cn

    img

    访问效果一致即可。

    1.2.3 构建一台 LVS 负载均衡服务器

    负载均衡器 会要有一个后端健康检查的要求,对于 nginx 来说默认是自带了后端服务的健康检查机制的。

    ### 10.10.10.11 操作
    $ cd /etc/sysconfig/network-scripts
    $ cp ifcfg-eth0 ifcfg-eth0:0
    $ vim ifcfg-eth0:0
    TYPE="Ethernet"
    BOOTPROTO="none"
    NAME="eth0:0"
    DEVICE="eth0:0"
    ONBOOT="yes"
    IPADDR="10.10.10.100"
    PREFIX="32"
    
    #关闭网卡的守护进程(LVS,Nginx服务器均执行)
    #CentOS 7的关闭的操作
    systemctl stop NetworkManager
    systemctl disable NetworkManager
    #CentOS 6的关闭操作
    service NetworkManager stop
    chkconfig NetworkManager off 
    
    #启动子接口并查看
    $ ifup eth0:0
    $ ip addr show eth0
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:16:73:35 brd ff:ff:ff:ff:ff:ff
        inet 10.10.10.11/24 brd 10.10.10.255 scope global eth0
           valid_lft forever preferred_lft forever
        inet 10.10.10.100/32 brd 10.10.10.100 scope global eth0:0
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fe16:7335/64 scope link
           valid_lft forever preferred_lft forever
    
    #修改内核参数
    #配置内核参数
    $ cat >> /etc/sysctl.conf <<EOF
    #LVS-DR Kernel BEGIN
    ##禁止转发重定向报文
    net.ipv4.conf.all.send_redirects = 0
    net.ipv4.conf.default.send_redirects = 0
    net.ipv4.conf.eth0.send_redirects = 0
    #LVS-DR Kernel END
    EOF
    #刷新内核参数
    $ sysctl -p
    #ipvsadm是一个工具,同时它也是一条命令,用于管理LVS的策略规则。
    $ modprobe ip_vs
    $ yum install -y ipvsadm
    #查看ipvs的信息
    $ ipvsadm -Ln
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    
    #开始进行配置ipvs规则 
    $ ipvsadm -A -t 10.10.10.100:80 -s rr
    $ ipvsadm -a -t 10.10.10.100:80 -r 10.10.10.12:80 -g
    $ ipvsadm -a -t 10.10.10.100:80 -r 10.10.10.13:80 -g
    #查看规则
    $ ipvsadm -Ln
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  10.10.10.100:80 rr
      -> 10.10.10.12:80               Route   1      0          0
      -> 10.10.10.13:80               Route   1      0          0
    
    #使其 ipvsadm 配置永久生效(CentOS 7使用)
    ipvsadm -Sn > /etc/sysconfig/ipvsadm
    #ipvsadm-save > /etc/sysconfig/ipvsadm
    #清楚ipvs规则
    #ipvsadm -C
    #恢复命令:
    #ipvsadm-restore < /etc/sysconfig/ipvsadm
    
    #启动ipvsadm服务,并设置为开机启动
    $ systemctl enable --now ipvsadm.service
    #查看服务状态
    $ systemctl status ipvsadm.service
    #重启服务
    $ systemctl restart ipvsadm.service
    #开机自动加载集群规则
    $ echo "/usr/sbin/ipvsadm-restore < /etc/sysconfig/ipvsadm" >> /etc/rc.local
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    配置LVS 后端的服务器(Nginx服务器)

    ### 10.10.10.12,10.10.10.13 操作
    $ cat > /etc/sysconfig/network-scripts/ifcfg-lo:0 <<EOF
    DEVICE=lo:0
    IPADDR=10.10.10.100
    NETMASK=255.255.255.255
    NETWORK=127.0.0.0
    # If you're having problems with gated making 127.0.0.0/8 a martian,
    # you can change this to something else (255.255.255.255, for example)
    BROADCAST=127.255.255.255
    ONBOOT=yes
    NAME=loopback
    EOF
    
    #配置内核参数
    $ cat >> /etc/sysctl.conf <<EOF
    #LVS-DR Kernel BEGIN
    net.ipv4.conf.all.arp_ignore = 1
    net.ipv4.conf.all.arp_announce = 2 
    net.ipv4.conf.default.arp_ignore = 1 
    net.ipv4.conf.default.arp_announce = 2
    net.ipv4.conf.lo.arp_ignore = 1
    net.ipv4.conf.lo.arp_announce = 1
    #LVS-DR Kernel END
    EOF
    $ sysctl -p
    
    #启动回环网卡并配置路由信息
    $ ifup lo:0
    $ ip addr show lo
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet 10.10.10.100/32 brd 127.255.255.255 scope global lo:0
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host
           valid_lft forever preferred_lft forever
    
    $ route add -host 10.10.10.100 dev lo:0
    $ route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         10.10.10.2      0.0.0.0         UG    0      0        0 eth0
    10.10.10.0      0.0.0.0         255.255.255.0   U     0      0        0 eth0
    10.10.10.100    0.0.0.0         255.255.255.255 UH    0      0        0 lo
    #设置开机自动加载
    $ echo "/usr/sbin/route add -host 10.10.10.100 dev lo:0" >> /etc/rc.local
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    1.2.4 进行测试演示

    #打开Windows 系统的 hosts 文件,并添加以下内容即可(C:\Windows\System32\drivers\etc)
    # Nginx Host BEGIN
    10.10.10.100 www.kubernetes.com
    10.10.10.100 www.kubernetes.cn
    # Nginx Host END
    
    • 1
    • 2
    • 3
    • 4
    • 5

    客户端访问 http://www.kubernetes.com(强制刷新)

    img

    img

    客户端访问 http://www.kubernetes.cn

    img

    依旧可以实现相同的功能。

    #查看IPvs的流量情况
    $ ipvsadm -Ln --stats
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
      -> RemoteAddress:Port
    TCP  10.10.10.100:80                    18      113        0    15288        0
      -> 10.10.10.12:80                      9       53        0     7118        0
      -> 10.10.10.13:80                      9       60        0     8170        0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    这样即能识别不同的域名,又能进行负载量的增加。

    2 Squid 服务

    2.1 Squid 简介

    img

    Squid 是一种用来缓存 Internet 数据的软件。接受来自人们需要下载的目标(object)的请求并适当的处理这些请求。也就是说,如果一个人想下载一web界面,他请求squid为他取得这个页面。squid随之连接到远程服务器并向这个页面发出请求。然后,squid 显式地聚集数据到客户端机器,而且同时复制一份。当下一次有人需要同一页面时, squid可以简单的从磁盘中读到它,那样数据会立即传输到客户机上。

    • Squid 是一个缓存服务器的守护进程。(二八原则:80%的访问量是由 20%的数据完成的。所以需要将这20%的数据丢到缓存服务器进行高速加载,岂不美哉。)

    • 主要的所用和应用场景:用来做前置的Web 缓存,加快用户访问 Web 速度,代理内网用户访问互联网资源,设置访问控制策略,控制用户的上网行为。

    • 支持的代理协议:FTP、HTTP、SSL、套接字

    • 存储分类:磁盘、分区、目录、Object(缓存文件)。一般 Squid 服务器分为二级目录,没有三级目录。二级目录存放就是 Object(缓存实例,缓存文件均可)

    • 索引方式

      • Hash Tables:目录,每个 Digest 的索引信息
      • Digest Tables:索引,不同分区(使用的UnixFS文件系统)对应的 Object 大概说明

    Varnish 则是 Squid 的竞争者的产品。

    Squid cache(简称为 Squid)是一个流行的自由软件(GNU 通用公共许可证)的代理服务器和 Web 缓存服务器。Squid 有广泛的用途,从作为网页服务器的前置 cache 服务器缓存相关请求来提高 Web 服务器的速度,到为一组人共享网络资源而缓存万维网,域名系统和其他网络搜索,到通过过滤流量帮助网络安全,到局域网通过代理上网。Squid 主要设计用于在 Unix 一类系统运行。

    Squid 的发展历史相当悠久,功能也相当完善。除了 HTTP 外,对于 FTP 与 HTTPS 的支援也相当好,在 3.0 测试版中也支援了 IPv6。

    Varnish是一款高性能的开源HTTP加速器挪威 最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好。

    Varnish支持正则表达式的匹配方式来删除缓存,是通过内存分页技术存储数据。

    小总结:

    • Varnish 在数据量或者单个文件比较小的情况,速度确实很快,因为它是通过内存索引。
    • Squid 则更加偏向是单个文件比较大的缓存情况。在大文件方面Squid 还是可以跟 Varnish 一教高下的。

    Squid cache(简称为 Squid)是一个流行的自由软件(GNU 通用公共许可证)的代理服务器和 Web 缓存服务器。Squid 有广泛的用途,从作为网页服务器的前置 cache 服务器缓存相关请求来提高 Web 服务器的速度,到为一组人共享网络资源而缓存万维网,域名系统和其他网络搜索,到通过过滤流量帮助网络安全,到局域网通过代理上网。Squid 主要设计用于在 Unix 一类系统运行。

    Squid 的发展历史相当悠久,功能也相当完善。除了 HTTP 外,对于 FTP 与 HTTPS 的支援也相当好,在 3.0 测试版中也支援了 IPv6。

    ——来自开源中国社区

    2.1.1 Squid 的工作逻辑

    img

    1. 当代理服务器中有客户端需要的数据时:

    a. 客户端向代理服务器发送数据请求;

    b. 代理服务器检查自己的数据缓存;

    c. 代理服务器在缓存中找到了用户想要的数据,取出数据;

    d. 代理服务器将从缓存中取得的数据返回给客户端。

    1. 当代理服务器中没有客户端需要的数据时:

    a. 客户端向代理服务器发送数据请求;

    b. 代理服务器检查自己的数据缓存;

    c. 代理服务器在缓存中没有找到用户想要的数据;

    d. 代理服务器向Internet 上的远端服务器发送数据请求;

    e. 远端服务器响应,返回相应的数据;

    f. 代理服务器取得远端服务器的数据,返回给客户端,并保留一份到自己的数据缓存中。

    若 Squid 代理服务器一直这样向远端服务器获取数据,缓存数据。那么就会可能将远端服务器的数据全部缓存到代理服务器本地?Squid 代理服务器会设置最大的缓存量,以及缓存的约束。

    2.1.2 Squid 工作模式分类

    传统代理,透明代理,反向代理

    • 其中传统代理和透明代理都是对内网做的加速访问,反向代理则是对公网用户做的加速访问。
    • 反向代理模式:Web服务器是架设在内网的,给公网提供访问的,再中间假设Squid服务器有助于公网用户访问速度的提升。

    正向代理和反向代理的区别

    1. 概念

    正向代理: 对于原始服务器而言,就是客户端的代言人

    反向代理: 对于客户端而言,就像是原始服务器

    1. 用途

    正向代理的典型用途是为在防火墙内的局域网客户端提供访问Internet的途径。正向代理还可以使用缓冲特性减少网络使用率。

    反向代理还可以为后端的多台服务器提供负载平衡,或为后端较慢的服务器提供缓冲服务。另外,反向代理还可以启用高级URL策略和管理技术,从而使处于不同web服务器系统的web页面同时存在于同一个URL空间下。

    1. 安全性

    正向代理允许客户端通过它访问任意网站并且隐藏客户端自身,因此你必须采取安全措施以确保仅为经过授权的客户端提供服务。

    反向代理对外都是透明的,访问者并不知道自己访问的是一个代理。

    1. 应用场景

    正向和透明代理:一般用于公司内网用户访问互联网,根据需求进行访问控制

    反向代理:一般用于公司服务器集群前做web缓存,提高用户访问效率,同时可以起到负责均衡作用,为互联网提供可持续端web服务。

    2.1.2.1 传统模式(标准的代理缓存服务器)

    一个标准的代理缓冲服务被用于缓存静态的网页到本地网络上的一台主机上(即代理服务器)。当被缓存的页面被第二次访问的时候,浏览器将直接从本地代理服务器那里获取请求数据而不再向原web站点请求数据。这样就节省了宝贵的网络带宽,而且提高了访问速度。但是,要想实现这种方式,必须在每一个内部主机的浏览器上明确指名代理服务器的IP地址和端口号。客户端上网时,每次都把请求发送给代理服务器处理,代理服务器根据请求确定是否连接到远程web服务器获取数据。如果在本地缓冲区有目标文件,则直接将文件传给用户即可。如果没有的话则先取回文件,先在本地保存一份缓冲,然后将文件发送给客户端浏览器。有点类似于正向代理模式。

    模式说明: 此类工作模式为三种模式中最简单的构成,对客户端配置较不友好,主要功能为加速 内网用户的访问速度,减少出口流量。

    工作架构图

    img

    Windows 系统中的代理服务器配置,缺点是需要用户手动配置代理服务器的地址,才能访问公网。

    img

    2.1.2.2 透明模式(透明代理缓存服务器)

    透明代理缓冲服务器和标准代理服务器的功能完全相同。但是,代理操作对客户端的浏览器是透明的(即不需指明代理服务器的IP和端口)。透明代理服务器阻断网络通信,并且过滤出访问外部的HTTP(80端口)流量。如果客户端的请求在本地有缓冲则将缓冲的数据直接发给用户,如果在本地没有缓冲则向远程web服务器发出请求,其余操作和标准的代理服务器完全相同。对于linux操作系统来说,透明代理使用Iptables或者Ipchains实现。因此不需要对浏览器作任何设置,所以,透明代理对于ISP(Internet服务器提供商)特别有用。

    模式说明: 此类工作模式配置较为复杂,需要借助防火墙对端口进行重定向操作。但是,对于客户端来说配置较为友好无需进行任何配置即可使用代理功能,主要功能为加速内网用户的访问速度, 减少出口流量。

    工作架构图

    img

    2.1.2.3 反向代理模式(反向代理缓存器)

    反向代理是和前两种代理完全不同的一种代理服务。使用它可以降低原始WEB服务器的负载。反向代理服务器承担了对原始WEB服务器的静态页面的请求,防止原始服务器过载。它位于WEB服务器和Internet之间,处理所有对WEB服务器的请求,组织了WEB服务器和Internet的直接通信。如果互联网用户请求的页面在代理服务器上有缓冲的话,代理服务器直接将缓冲内容发送给用户。如果没有缓冲则先向WEB服务器发出请求,取回数据,本地缓存后再发给用户。这种方式通过降低了WEB服务器的请求数从而降低了WEB服务器的负载。

    模式说明: 反向代理服务器位于本地 WEB 服务器和互联网用户之间,处理公网用户发送的请求 并代理至内网服务中,有效减轻后端真实服务器压力,增加服务并发能力

    工作架构图

    img

    Squid 反向代理模式只支持 RR 轮询的模式,响应客户端的请求,返回给客户端数据并且缓存到本地,可以有效减轻服务器端的压力。

    2.1.3 Squid 软件及配置说明

    2.1.3.1 Squid 软件介绍

    CentOS 6的Squid 软件介绍

    软件包: squid-3.1.STABLE21-6.el6

    系统服务: squid

    主程序: /usr/sbin/squid

    配置目录: /etc/squid

    主配置文件: /etc/squid/squid.conf

    默认监听端口: TCP 3128

    默认访问日志: /var/log/squid/access.log

    特别说明:Squid 在经过 3.0 版本后使用 C 语言进行重构,效率大大提升,Centos6 标配 Squid 3.0 以后版本

    范例:CentOS 7的 Squid 软件包介绍

    $ rpm -qi squid
    Name        : squid
    Epoch       : 7
    Version     : 3.5.20
    Release     : 15.el7_8.1
    Architecture: x86_64
    Install Date: Fri 02 Sep 2022 11:33:28 AM CST
    Group       : System Environment/Daemons
    Size        : 10930187
    License     : GPLv2+ and (LGPLv2+ and MIT and BSD and Public Domain)
    Signature   : RSA/SHA256, Fri 08 May 2020 05:56:51 PM CST, Key ID 24c6a8a7f4a80eb5
    Source RPM  : squid-3.5.20-15.el7_8.1.src.rpm
    Build Date  : Fri 08 May 2020 05:10:36 AM CST
    Build Host  : x86-02.bsys.centos.org
    Relocations : (not relocatable)
    Packager    : CentOS BuildSystem <http://bugs.centos.org>
    Vendor      : CentOS
    URL         : http://www.squid-cache.org
    Summary     : The Squid proxy caching server
    Description :
    Squid is a high-performance proxy caching server for Web clients,
    supporting FTP, gopher, and HTTP data objects. Unlike traditional
    caching software, Squid handles all requests in a single,
    non-blocking, I/O-driven process. Squid keeps meta data and especially
    hot objects cached in RAM, caches DNS lookups, supports non-blocking
    DNS lookups, and implements negative caching of failed requests.
    
    Squid consists of a main server program squid, a Domain Name System
    lookup program (dnsserver), a program for retrieving FTP data
    (ftpget), and some management and client tools.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    2.1.3.2 Squid 常用配置选项

    文件位置 /etc/squid/squid.conf

    $ vim /etc/squid/squid.conf
    # Squid 启动端口为 3128
    http_port 3128
    
    #指定 Squid 进程能够使用的内存大小(默认大小)
    cache_mem	256MB
    
    #设置squid磁盘缓存最大文件,超过4M端文件不保存到磁盘
    maximum_object_size 4MB
    
    #设置squid磁盘缓存最小文件
    minimum_object_size 0 KB
    
    #设置squid内存缓存最大文件,超过4M不保存到内存
    #缓存的文件类型一般是静态资源
    maximum_object_size_in_memory 4096 KB
    
    #指定允许通过 Squid 的单个元素大小
    reply_body_max_size 10 MB 
    
    #指定 Squid 对象文件存储路径使用大小限制为 100MB ,一级目录个数为 16 个,二级目录个数为 256 个
    cache_dir ufs /var/spool/squid 100 16 256
    
    #log文件日志格式
    #logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %>Hs %h" "%{User-Agent}>h" %Ss:%Sh
    
    #log文件存放路径和日志格式
    # access_log /var/log/squid/access.log squid
    
    #设置缓存日志
    cache_log /var/log/suqid/cache.log
    
    #log轮转60天
    logfile_rotate 60
    
    #cache目录使用量大于95%时,开始清理旧的cache
    cache_swap_high 95
    
    #cache目录清理到90%时停止
    cache_swap_low 90
    
    #定义本地网段
    acl localnet src 192.168.1.0/24
    
    #允许本地网段使用
    http_access allow localnet
    
    #拒绝所有
    http_access deny all
    
    #指定 Squid 服务器自身的主机名
    visible_hostname proxy.benet.com
    
    #设置进行DNS查询测试,如果第一个站点解析成功则立即结束DNS查询测试。
    #如果你不愿意进行DNS查询测试,就不要去掉缺省的设置。
    dns_testnames www.google.com www.163.com
    
    #管理员邮箱
    cache_mgr admin@kubernetes.com
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    2.2 搭建 Squid 传统模式

    实验结构图

    img

    环境:

    Client:10.10.10.11(内网)

    Squid-Server:10.10.10.12(内网)和20.20.20.12(公网)

    Apache:20.20.20.13(公网)

    ### 所有机器网络守护进程关闭
    #CentOS 7关闭
    systemctl disable --now NetworkManager
    
    #CentOS 6关闭
    service NetworkManager stop
    chkconfig NetworkManager off
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.2.1 内网客户端(10.10.10.11)

    #客户端添加网关指向squid服务器
    $ echo "GATEWAY=10.10.10.12" >> /etc/sysconfig/network-scripts/ifcfg-eth0
    #重启网络服务
    #CentOS 7网络服务重启
    $ systemctl restart network
    #CentOS 6网络服务重启
    $ service network restart
    
    #下载elinks字符界面浏览器
    #$ yum install -y elinks
    
    ## 该客户端需要能够有图形化的终端
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.2.2 网关服务器(Squid 服务器 10.10.10.12)

    #开启第二块网卡

    $ mount -t iso9660 /dev/cdrom /mnt/cdrom 
    #挂载本地yum源
    # service NetworkManager stop # 关闭网卡守护进程
    $ cat >> /etc/sysconfig/network-scripts/ifcfg-eth1 <<EOF
    DEVICE=eth1
    TYPE=Ethernet
    ONBOOT=yes
    BOOTPROTO=static
    IPADDR=20.20.20.12
    NETMASK=255.255.255.0
    EOF
    
    #CentOS 7网络服务重启
    $ systemctl restart network
    #CentOS 6网络服务重启
    $ service network restart
    
    #报错:弹出界面 eth1: 错误:没有找到合适的设备:没有找到可用于连接 'System eth1' 的设备。
    #解决办法
    #删除网卡配置文件的mac地址,然后删除这个文件,重启就好了
    #rm -f /etc/udev/rules.d/70-persistent-net.rules 
    
    #报错:弹出界面 eth1: 错误:激活连接失败:The connection is not for this device.
    #解决办法 关闭网卡守护进程 
    #service NetworkManager stop
    
    $ cat >> /etc/sysctl.conf <<-'EOF' 
    #Squid Server BEGIN
    net.ipv4.ip_forward = 1 
    #Squid Server END
    EOF
    $ sysctl -p
    
    #查看网络配置
    $ ip addr show
    ...省略部分输出...
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:70:8a:d6 brd ff:ff:ff:ff:ff:ff
        inet 10.10.10.12/24 brd 10.10.10.255 scope global eth0
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fe70:8ad6/64 scope link
           valid_lft forever preferred_lft forever
    3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:70:8a:e0 brd ff:ff:ff:ff:ff:ff
        inet 20.20.20.12/24 brd 20.20.20.255 scope global eth1
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fe70:8ae0/64 scope link
           valid_lft forever preferred_lft forever
    
    #需要ping通Apache服务器(提前:将Apache网络配置完毕)
    $ ping -c 1 -W 1 20.20.20.13
    PING 20.20.20.13 (20.20.20.13) 56(84) bytes of data.
    64 bytes from 20.20.20.13: icmp_seq=1 ttl=64 time=0.389 ms
    
    --- 20.20.20.13 ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.389/0.389/0.389/0.000 ms
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    #安装 Squid 服务

    #安装 Squid 服务
    $ yum install -y squid
    $ vim /etc/squid/squid.conf 
    http_port 3128
    visible_hostname www.squid.com
    
    #启动Squid服务
    #CentOS 7启动
    $ systemctl start squid
    #CentOS 6启动
    $ service squid start
    
    #查看端口
    $ netstat -auntlp | grep 3128
    tcp6       0      0 :::3128                 :::*                    LISTEN      2040/(squid-1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2.2.3 模拟外网 Web 服务器(20.20.20.13)

    $ vim /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=yes
    BOOTPROTO=static
    IPADDR=20.20.20.13
    NETMASK=255.255.255.0
    
    #CentOS 7网络服务重启
    $ systemctl restart network
    #CentOS 6网络服务重启
    $ service network restart
    
    #配置Apache服务
    $ yum install -y httpd
    #CentOS 7 Apache服务操作
    $ systemctl enable --now httpd 
    #CentOS 6 Apache服务操作
    $ service httpd start && chkconfig --add httpd && chkconfig httpd on
    $ echo "This is Apache Serevr" >> /var/www/html/index.html
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    2.2.4 测试

    10.10.10.11 输入init 5 启动图形界面,然后测试;客户端输入20.20.20.13发现访问不了,这是因为,要在浏览器设置代理,代理ip地址是squid服务器地址

    img

    这时候在访问,就可以访问到

    img

    把20.20.20.13的服务器停掉(或者 httpd 服务停掉),依然可以访问到,这就是缓存。

    systemctl stop httpd
    
    • 1

    img

    2.3 搭建 Squid 透明模式

    实验结构图

    img

    环境:

    Client:10.10.10.11(内网)

    Squid-Server:10.10.10.12(内网)和20.20.20.12(公网)

    Apache:20.20.20.13(公网)

    2.3.1 内网客户端(10.10.10.11)

    #客户端添加网关指向squid服务器
    $ echo "GATEWAY=10.10.10.12" >> /etc/sysconfig/network-scripts/ifcfg-eth0
    #重启网络服务
    #CentOS 7网络服务重启
    $ systemctl restart network
    #CentOS 6网络服务重启
    $ service network restart
    
    #下载elinks字符界面浏览器
    #$ yum install -y elinks
    
    ## 该客户端需要能够有图形化的终端
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.3.2 网关服务器(Squid 服务器 10.10.10.12)

    #开启第二块网卡

    $ mount -t iso9660 /dev/cdrom /mnt/cdrom 
    #挂载本地yum源
    # service NetworkManager stop # 关闭网卡守护进程
    $ cat >> /etc/sysconfig/network-scripts/ifcfg-eth1 <<EOF
    DEVICE=eth1
    TYPE=Ethernet
    ONBOOT=yes
    BOOTPROTO=static
    IPADDR=20.20.20.12
    NETMASK=255.255.255.0
    EOF
    
    #CentOS 7网络服务重启
    $ systemctl restart network
    #CentOS 6网络服务重启
    $ service network restart
    
    #报错:弹出界面 eth1: 错误:没有找到合适的设备:没有找到可用于连接 'System eth1' 的设备。
    #解决办法
    #删除网卡配置文件的mac地址,然后删除这个文件,重启就好了
    #rm -f /etc/udev/rules.d/70-persistent-net.rules 
    
    #报错:弹出界面 eth1: 错误:激活连接失败:The connection is not for this device.
    #解决办法 关闭网卡守护进程 
    #service NetworkManager stop
    
    $ cat >> /etc/sysctl.conf <<-'EOF' 
    #Squid Server BEGIN
    net.ipv4.ip_forward = 1 
    #Squid Server END
    EOF
    $ sysctl -p
    
    #查看网络配置
    $ ip addr show
    ...省略部分输出...
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:70:8a:d6 brd ff:ff:ff:ff:ff:ff
        inet 10.10.10.12/24 brd 10.10.10.255 scope global eth0
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fe70:8ad6/64 scope link
           valid_lft forever preferred_lft forever
    3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:70:8a:e0 brd ff:ff:ff:ff:ff:ff
        inet 20.20.20.12/24 brd 20.20.20.255 scope global eth1
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fe70:8ae0/64 scope link
           valid_lft forever preferred_lft forever
    
    #需要ping通Apache服务器(提前:将Apache网络配置完毕)
    $ ping -c 1 -W 1 20.20.20.13
    PING 20.20.20.13 (20.20.20.13) 56(84) bytes of data.
    64 bytes from 20.20.20.13: icmp_seq=1 ttl=64 time=0.389 ms
    
    --- 20.20.20.13 ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.389/0.389/0.389/0.000 ms
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    #安装 Squid 服务

    #安装 Squid 服务
    $ yum install -y squid
    $ vim /etc/squid/squid.conf 
    #transparent ip 为网关内网 IP
    http_port 10.10.10.12:3128 transparent
    #指定主机名称
    visible_hostname www.squid.com
    #相比于传统代理,配置文件有变动
    
    #CentOS 6操作
    $ service iptables start && chkconfig iptables on
    #CentOS 7操作
    $ systemctl enable --now firewalld
    
    $ iptables -F && iptables -L
    $ iptables -t nat -A PREROUTING -i eth0 -s 10.10.10.0/24 -p tcp --dport 80 -j REDIRECT --to-ports 3128 # 添加路由规则
    #往nat表PREROUTING链上添加一条信息,当入站网卡是eth0(内网网卡),内网网段是10.10.10.0/24,
    #协议是tcp,目标端口是80的话,做一个端口重定向,重定向到3128
    $ iptables -t nat -vnL
    Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination
        0     0 REDIRECT   tcp  --  eth0   *       10.10.10.0/24        0.0.0.0/0            tcp dpt:80 redir ports 3128
    
    #启动Squid服务
    #CentOS 7启动或者重启服务
    $ systemctl start squid
    $ systemctl restart squid
    #CentOS 6启动
    #重新加载配置文件
    $ service squid reload 
    $ service squid start
    
    #查看端口
    $ netstat -auntlp | grep 3128
    tcp6       0      0 :::3128                 :::*                    LISTEN      2040/(squid-1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    2.3.3 模拟外网 Web 服务器(20.20.20.13)

    $ vim /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=yes
    BOOTPROTO=static
    IPADDR=20.20.20.13
    NETMASK=255.255.255.0
    
    #CentOS 7网络服务重启
    $ systemctl restart network
    #CentOS 6网络服务重启
    $ service network restart
    
    #配置Apache服务
    $ yum install -y httpd
    #CentOS 7 Apache服务操作
    $ systemctl enable --now httpd 
    #CentOS 6 Apache服务操作
    $ service httpd start && chkconfig --add httpd && chkconfig httpd on
    $ echo "This is Apache Serevr" >> /var/www/html/index.html
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    2.3.4 测试

    直接进行测试,客户端不进行代理的相关配置

    img

    测试结果:可以正常访问。

    img

    2.3.5 下载限速

    #在服务器(20.20.20.13)创建一个文件
    $ cd /var/www/html
    $ dd if=/dev/zero of=1.txt bs=1M count=256 
    $ ls -lh /var/www/html
    total 257M
    -rw-r--r-- 1 root root 256M Sep  2 12:47 1.txt
    -rw-r--r-- 1 root root   22 Sep  2 11:24 index.html
    
    $ cat >> /etc/squid/squid.conf <<-'EOF'
    #添加reply_body_max_size参数,默认是以字节为单位
    reply_body_max_size 200 MB
    EOF
    
    #CentOS 7重启方式
    #$ systemctl restart squid
    $ /bin/systemctl reload squid.service
    #CentOS 6重启方式
    $ service squid reload
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    #在不下载限速时:

    img

    可以正常的下载大文件

    测试

    10.10.10.11 输入init 5 启动图形界面,然后测试(注意缓存的问题);客户端输入20.20.20.13/1.txt访问,会报错

    1. 默认访问http://20.20.20.13可以访问到主页:this is server 1
    2. 若是访问http://20.20.20.13/1.txt文件报错ERROR,因为访问文件超过squid代理的最大文件限制。所以不可访问。

    2.4 搭建 Squid 反向代理模式

    Squid 反向代理功能主要是给公网用户缓存Web服务器的数据。Squid 缓存服务器没有后端服务健康检测的功能。并且所使用的算法只有 RR 轮询。所以Squid缓存服务器后端代理的必须是高可用的。

    实验结构图

    img

    环境

    apache1 10.10.10.11(内网)

    apache1 10.10.10.12(内网)

    squid—server: 10.10.10.13(内网) 和 20.20.20.13(公网)

    client: 20.20.20.14(公网)

    Squid 反向代理是给公网的用户进行提供缓存内网Web服务器的数据,加速公网用户的访问

    CDN 也是借助这个原理,CDN 有两个主要的服务:智能DNS 和 缓存服务器

    2.4.1 内网客户端(10.10.10.11、10.10.10.12)

    两台Apache 服务器都要操作

    #配置Apache服务
    $ yum install -y httpd
    #CentOS 7 Apache服务操作
    $ systemctl enable --now httpd 
    #CentOS 6 Apache服务操作
    $ service httpd start && chkconfig --add httpd && chkconfig httpd on
    #10.10.10.11 修改页面
    $ echo "This is Apache Serevr1" >> /var/www/html/index.html
    #10.10.10.12 修改页面
    $ echo "This is Apache Serevr2" >> /var/www/html/index.html
    
    ##生产环境中Web页面是相同的
    
    #将Web服务器的网关指向Squid服务器(网关服务器)
    $ vim /etc/sysconfig/network-scripts/ifcfg-eth0
    TYPE="Ethernet"
    BOOTPROTO="none"
    NAME="eth0"
    DEVICE="eth0"
    ONBOOT="yes"
    IPADDR="10.10.10.11"
    PREFIX="24"
    GATEWAY="10.10.10.13"
    
    $ vim /etc/sysconfig/network-scripts/ifcfg-eth0
    TYPE="Ethernet"
    BOOTPROTO="none"
    NAME="eth0"
    DEVICE="eth0"
    ONBOOT="yes"
    IPADDR="10.10.10.12"
    PREFIX="24"
    GATEWAY="10.10.10.13"
    
    #CentOS 7 Apache服务操作
    $ systemctl restart network 
    #CentOS 6 Apache服务操作
    $ service network restart
    #查看路由表信息
    $ route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         10.10.10.13     0.0.0.0         UG    0      0        0 eth0
    10.10.10.0      0.0.0.0         255.255.255.0   U     0      0        0 eth0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    2.4.2 网关服务器(Squid 服务器 10.10.10.13)

    开启第二块网卡

    #配置公网网卡信息
    $ cd /etc/sysconfig/network-scripts
    $ vim ifcfg-eth1
    TYPE="Ethernet"
    BOOTPROTO="none"
    NAME="eth1"
    DEVICE="eth1"
    ONBOOT="yes"
    IPADDR="20.20.20.13"
    PREFIX="24"
    #重启网络服务
    $ systemctl restart network 
    #报错:弹出界面 eth1: 错误:没有找到合适的设备:没有找到可用于连接 'System eth1' 的设备。
    #弹出界面 eth1: eth1 设备的 MAC 地址与预想的不符,忽略。
    #解决办法
    #删除网卡配置文件的mac地址,然后删除这个文件,重启就好了
    #rm -f /etc/udev/rules.d/70-persistent-net.rules 
    
    #报错:弹出界面 eth1: 错误:激活连接失败:The connection is not for this device.
    #解决办法 关闭网卡守护进程 
    #service NetworkManager stop
    
    #查看网卡配置
    $ ip addr show
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:06:7d:2d brd ff:ff:ff:ff:ff:ff
        inet 10.10.10.13/24 brd 10.10.10.255 scope global eth0
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fe06:7d2d/64 scope link
           valid_lft forever preferred_lft forever
    3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:06:7d:37 brd ff:ff:ff:ff:ff:ff
        inet 20.20.20.13/24 brd 20.20.20.255 scope global eth1
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fe06:7d37/64 scope link
           valid_lft forever preferred_lft forever
    
    #配置路由转发功能
    $ cat >> /etc/sysctl.conf <<EOF
    net.ipv4.ip_forward = 1
    EOF
    $ sysctl -p
    net.ipv4.ip_forward = 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    安装 Squid 服务

    #安装squid服务
    $ yum install -y squid
    
    #配置squid文件
    $ vim /etc/squid/squid.conf
    #公网ip
    #squid监听的端口,客户访问网站的端口,也可以只写端口,vhost不写也可以
    http_port 20.20.20.13:80 vhost
    
    #cache_peer 192.168.1.2(内网中网页服务器 IP) 
    #parent 80 0 (网络中是否还有其他代理) 
    #originserver round-robin (节点以轮询的方式)
    cache_peer 10.10.10.11 parent 80 0 originserver round-robin
    cache_peer 10.10.10.12 parent 80 0 originserver round-robin
    
    #开启squid服务
    $ systemctl enable --now squid
    #service squid start && chkconfig --add squid
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    2.4.3 模拟外网 Web 服务器(20.20.20.14)

    $ vim /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    TYPE=Ethernet
    ONBOOT=yes
    BOOTPROTO=static
    IPADDR=20.20.20.14
    NETMASK=255.255.255.0
    
    #Xshell可能会断,需要登录到虚拟机操作
    #也可以通过新增网卡到该网络配置即可,保留Xshell连接
    
    #重启网络配置
    $ systemctl restart network 
    #查看网卡配置
    $ ip addr show eth0
    3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:40:5f:2b brd ff:ff:ff:ff:ff:ff
        inet 20.20.20.14/24 brd 20.20.20.255 scope global eth0
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fe40:5f2b/64 scope link
           valid_lft forever preferred_lft forever
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    2.4.5 测试

    20.20.20.14 输入init 5 启动图形界面,然后测试

    客户端输入20.20.20.13访问,会报错,不能访问,这是因为ACL的设置,之前的可以访问,是因为刚好在10网段刚好在放行列表

    #Squid服务器操作
    $ vim /etc/squid/squid.conf
    #放行列表,添加20的网段
    acl localnet src 20.20.20.0/24  # RFC1918 possible internal network
    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
    
    #开启squid服务
    $ systemctl restart squid
    #$ service squid restart
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    再次访问(轮询访问)

    img

    img

    将内网的Apache服务器即使服务中断,停止;也不会影响其提供服务了。

    systemctl stop httpd
    
    • 1

    img

    2.5 Squid ACL 设置

    所谓ACL(Access Control List)就是在服务器端设定特定的上网策略,哪些用户能上网,能访问什么网站,能下载什么文件等等对用户的上网行为进行约束和管理,这个企业一般都会进行ACL上网控制,对员工的上网行为进行控制,从而提高工作效率。这里要说的就是基于redhat代理服务程序squid的ACL控制。

    ACL 访问控制列表,是Linux 四层防御其中的一层,在应用空间的。允许放行哪些规则。

    2.5.1 Squid ACL 设置

    使用访问控制特性,可以控制在访问时根据特定的时间间隔进行缓存、访问特定站点或一组站点等等。squid访问控制有两个要素:ACL元素和访问列表。访问列表可以允许或拒绝某些用户对此服务的访问。

    2.5.2 Squid ACL 配置格式

    为了使用控制功能,必须先设置ACL规则并应用。ACL声明的格式如下:

    如果出现ACL,但没有定义http_access,相当于没写

    acl  列表名称   列表类型    列表内容
    acl  localnet    src     	20.20.20.0/24
    
    列表名称:由管理员自行指定,用来识别控制条件
    
    列表类型:必须使用Squid预定义的值,对应不同类别的控制条件
    	src: 						源地址(即客户机IP地址)
      dst: 						目标地址(即服务器IP地址)
    	port: 					目标端口
    	srcdomain: 			目标源名称(即客户机名称)
    	dstdomain: 			目标名称(即服务器名称)
    	time: 					访问时间,一天中的时刻和一周内的一天(2019-09-06)
    	url_regex: 			目标URL路径,URL规则表达式匹配(范围匹配)
    	urlpath_regex: 	整个目标URL路径,URL-path规则表达式匹配,略去协议和主机名
    	proxy_auth: 		通过外部程序进行用户验证
    	maxconn: 				最大并发连接,单一IP的最大连接数(超过即拒绝)
    
    列表内容:具体控制的对象,不同的类型对应的内容也不一样,可以有多个值,用空格分隔
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    针对已定义的 acl 列表进行限制

    http_access allow 或 deny 列表名称 ...
    
    • 1

    小总结:

    ACL(Access Control List,访问控制列表):

    • 根据源地址、目标URL、文件类型等定义列表:

    ACL 列表名称 列表类型 列表内容 …

    • 针对已定义的ACL列表进行限制:

    http_access allow或deny 列表名称 …

    2.5.3 规则总结说明

    1. 如果仅仅只有 acl 设定没有 http_access 设置默认拒绝
    2. 如果 http_access deny all 未定义,并且自己定义了一个 acl 范围,默认使用最后一条规则的反向 规则

    2.5.4 Squid ACL 设置扩展

    #将20.20.20.0/24网段的ACL重新定义
    $ vim /etc/squid/squid.conf
    acl wannet src 20.20.20.0/24
    http_access allow wannet
    
    #重新加载服务
    $ systemctl restart squid
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    客户端输入 20.20.20.13 访问

    img

    img

    #将/etc/squid/squid.conf配置文件中的 http_access allow wannet 行注释或者删除
    #重启服务
    
    • 1
    • 2

    img

    访问会遭到拒绝

    #将/etc/squid/squid.conf配置文件的 http_access deny all 行注释或者删除
    #重启服务
    #再次浏览器访问的时候依旧是无法拒绝
    
    #将/etc/squid/squid.conf配置文件的 http_access allow localhost 行进行修改 http_access deny localhost
    $ vim /etc/squid/squid.conf
    http_access deny localhost
    #重启服务
    #再次浏览器访问的时候,发现可以进行访问了
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    img

    img

    当 http_access deny all 被关闭以后,如果定义了ACL,并且该ACL也没有定义 http_access 的话,他会根据最后一条 http_access 反向进行操作,也就是 http_access deny all 定义的 deny,那么未定义的 http_access 的ACL 就会变成 allow,反之同理。

    #将/etc/squid/squid.conf配置文件的 http_access deny localhost 行进行修改 http_access allow localhost
    $ vim /etc/squid/squid.conf
    http_access allow localhost
    #重启服务
    #再次浏览器访问的时候,就发现已经不能正常访问了
    
    • 1
    • 2
    • 3
    • 4
    • 5

    img

  • 相关阅读:
    LeetCode 2562. 找出数组的串联值【数组,相向双指针】1259
    Linux学习命令之source
    09【享元设计模式】
    工业交换机的“自适应”是什么意思?
    游戏服务器中定时任务的实现
    易货:一种古老而有效的商业模式
    golang保留小数点后两位,不四舍五入
    C++PrimerPlus(第6版)中文版:Chapter16.4泛型编程_概念_改进和模型_copyit.cpp
    曲线救国|基于函数计算FC3.0部署AI数字绘画stable-diffusion
    Arduino ESP8266/32 自定义IO组网页状态显示与控制
  • 原文地址:https://blog.csdn.net/weixin_40274679/article/details/126843899