web服务之NGINX反向代理&负载均衡

3.4 nginx反负

 

3.4.1 反向代理应用

负载均衡&反向代理

在这里插入图片描述
 
 

nginx在AKF扩展立方体的应用:https://www.jianshu.com/p/d08d0c14810f

在这里插入图片描述

X轴代表无差别的克隆服务和数据库。不同的服务器可以通过不同调度算法来分发请求
Y轴基于URL对业务做拆分,如图片文本URL分离
Z轴通常基于请求或客户的信息进行分割。如根据用户IP或其他信息映射到某个特定的服务或集群

1.X轴扩展

优点:成本最低,实施简单;

缺点:受指令集多少和数据集大小的约束。当单个产品或应用过大时,服务响应变慢,无法通过X轴的水平扩展提高速度;

场景:发展初期,业务复杂度低,需要增加系统容量。

2.Y轴扩展

优点:可以解决指令集和数据集的约束,解决代码复杂度问题,可以实现隔离故障,可以提高响应时间,可以使团队聚焦更利于团队成长;

缺点:成本相对较高;

场景:业务复杂,数据量大,代码耦合度高,团队规模大。

3.Z轴扩展

优点:能解决数据集的约束,降低故障风险,实现渐进交付,可以带来最大的扩展性。

缺点:成本最昂贵,且不一定能解决指令集的问题;

场景:用户指数级快速增长。

 
 

多种协议的反向代理

在这里插入图片描述

 
 

反向代理与缓存

在这里插入图片描述

 
 

3.4.2 负载均衡模块

upstream

模块名称 http_upstream_module(默认)

模块功能 定义上游服务器集群,然后可以被各种协议引用(proxy_pass,fastcgi_pass等)

 
 

1.模块指令

upstream 定义负载均衡集群

server 定义集群中的上游服务器地址

在这里插入图片描述
参数

weight 服务器权重值,默认是1
max_conns server的最大并发连接数,用于单worker进程,默认0无限制
max_fails 在fail_timeout时间段内,最大的失败次数。当达到最大失败时,会在fail_timeout秒内这台server不允许被选择
fail_timeout 单位为秒,默认值为10秒;指定一段时间内,最大的失败次数max_fails;到达max_fails后,该server不能访问的时间

 
 

2.负载均衡算法

Round-Robin 轮流处理分发用户请求 server IP;默认weight值为1

加权Round-Robin 根据服务器性能分配权重,通过weight来定义权重值

算法配置 server xxx weigh=X …;

算法实例

配置

web server

server {
	listen 8001;
	default_type text/plain;
	return 200 '8001 server response.\n';
}

server {
	listen 8002;
	default_type text/plain;
	return 200 '8002 server response.\n';
}

upstream server

upstream rrtest {
        server 192.168.217.155:8001 weight=2 max_conns=2 max_fails=2 fail_timeout=5;
        server 192.168.217.155:8002;
        keepalive 32;

}

server {
        listen 80;
        server_name rrtest.pl.com;

        location / {
                proxy_pass http://rrtest;
                proxy_http_version 1.1;
                proxy_set_header Connection "";
        }
}

测试
tcpdump -i ens192 port 8001 -v 查看http长连接是否生效

[root@www ~]# curl rrtest.pl.com
8001 server response.
[root@www ~]# curl rrtest.pl.com
8002 server response.
[root@www ~]# curl rrtest.pl.com
8001 server response.
[root@www ~]# curl rrtest.pl.com
8001 server response.
[root@www ~]# curl rrtest.pl.com
8002 server response.

查看头

[root@www ~]# curl -I rrtest.pl.com
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Tue, 09 Jun 2020 12:15:45 GMT
Content-Type: text/plain
Content-Length: 22
Connection: keep-alive

 
 

ip_hash

模块名称

http_upstream_ip_hash_module(默认) ip_hash;

http_upstream_hash_module(默认) hash key [consistent];

 

模块功能

以客户端的IP地址作为hash算法的关键字,映射到特定上游服务器中:

对IPv4使用前3个字节作为关键字,对于ipv6使用完整地址

可以基于realip模块修改用于执行算法的IP地址

 

模块指令

ip_hash;

hash key [consistent];

 

模块实战

配置

upstream iphash {
	     server 192.168.217.155:8001 weight=2 max_conns=2 max_fails=2 fail_timeout=5;
	     server 192.168.217.155:8002;
	     ip_hash;
      #hash user_$arg_username; 
      #hash user_$arg_username consistent;  

}

server {
	listen 80;
	set_real_ip_from 192.168.217.155;
	real_ip_recursive on;
	real_ip_header X-Forwarded-For;
	server_name iphash.pl.com;

	location / {
		proxy_pass http://iphash;
		proxy_http_version 1.1;
		proxy_set_header Connection "";
	}
}
[root@www ~]# curl iphash.pl.com
8001 server response.

curl -H “X-Forwarded-For:1.1.1.1” rrtest.pl.com

 
问题

当上游服务器集群中某台服务器中出现问题,能在upstream中直接移除此问题server么?
不能,

 
原因

在这里插入图片描述

 
解决

3.一致性hash算法

一致性哈希用于解决分布式缓存系统中的数据选择节点存储问题和数据选择节点读取问题以及在增删节点后减少数据缓存的消失范畴,防止雪崩的发生

hash $key consistent;

一致性hash:https://www.jianshu.com/p/4163916a2a8a

扩容前

一致性hash是一个0-2^32的闭合圆,(拥有2^23个桶空间,每个桶里面可以存储很多数据,可以理解为s3的存储桶)所有节点存储的数据都是不一样的。计算一致性哈希是采用的是如下步骤:

  1. 对节点进行hash,通常使用其节点的ip或者是具有唯一标示的数据进行hash(ip),将其值分布在这个闭合圆上。
  2. 将存储的key进行hash(key),然后将其值要分布在这个闭合圆上。
  3. 从hash(key)在圆上映射的位置开始顺时针方向找到的一个节点即为存储key的节点。如果到圆上的0处都未找到节点,那么0位置后的顺时针方向的第一个节点就是key的存储节点。

在这里插入图片描述

 

扩容后

在这里插入图片描述

 
 
删除节点带来的影响
以下图为基准,删除了node2节点后,原本在node2节点上的数据就会被重新定位node4上。这样就产生一个影响:原来node2的数据转移到node4上,这样node4的内存使用率会骤增,如果node2上存在热点数据,node4会扛不住甚至会可能挂掉,挂掉之后数据又转移给node3,如此循环会造成所有节点崩溃,也就是前面所说的雪崩的情况。

在这里插入图片描述

 

哈希槽

redis cluster采用数据分片的哈希槽来进行数据存储和数据的读取。redis cluster一共有2^14(16384)个槽,所有的master节点都会有一个槽区比如0~1000,槽数是可以迁移的。master节点的slave节点不分配槽,只拥有读权限。但是注意在代码中redis cluster执行读写操作的都是master节点,并不是你想 的读是从节点,写是主节点。第一次新建redis cluster时,16384个槽是被master节点均匀分布的。

在这里插入图片描述
 
 
和一致性哈希相比

  1. 它并不是闭合的,key的定位规则是根据CRC-16(key)%16384的值来判断属于哪个槽区,从而判断该key属于哪个节点,而一致性哈希是根据hash(key)的值来顺时针找第一个hash(ip)的节点,从而确定key存储在哪个节点。
  2. 一致性哈希是创建虚拟节点来实现节点宕机后的数据转移并保证数据的安全性和集群的可用性的。redis cluster是采用master节点有多个slave节点机制来保证数据的完整性的,master节点写入数据,slave节点同步数据。当master节点挂机后,slave节点会通过选举机制选举出一个节点变成master节点,实现高可用。但是这里有一点需要考虑,如果master节点存在热点缓存,某一个时刻某个key的访问急剧增高,这时该mater节点可能操劳过度而死,随后从节点选举为主节点后,同样宕机,一次类推,造成缓存雪崩。

哈希槽:https://zackku.com/redis-cluster/

 
 

least_conn

模块名称
http_upstream_least_conn_module(默认编译)
模块功能
从所有上游服务器中,找出当前并发连接数最小的一个,将请求转发给它,如果一样则使用轮询算法
模块指令
least_conn;

 

zone

模块名称
http_upstream_zone_module
模块功能
分配共享内存将其他upstream模块定义的负载均衡策略,状态数据放在共享内存中,以对所有worker进程生效。
模块指令
zone NAME [size];

 
 

3.4.3 上游服务器keepalive模块

模块名称
http_upstream_keepalive_module默认编译
模块功能
通过复用链接,降低nginx与上游服务器建立、关闭连接的消耗,提升吞吐量的同时降低时延。
模块指令
keepalive 连接数;
proxy_http_version 1.1;
1.1版本才支持keepalive
proxy_set_header Connection “”;
发送长连接头部

 
Keepalived 简要介绍

keepalived:https://blog.csdn.net/qq_27384769/article/details/80660049

Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 web 前端服务的高可用。

Keepalived 以 VRRP 协议为实现基础,用 VRRP 协议来实现高可用性(HA)。VRRP(VirtualRouter Redundancy Protocol)协议是用于实现路由器冗余的协议,VRRP 协议将两台或多台路由器设备虚拟成一个设备,对外提供虚拟路由器 IP(一个或多个),而在路由器组内部,如果实际拥有这个对外 IP 的路由器如果工作正常的话就是 MASTER,或者是通过算法选举产生,MASTER 实现针对虚拟路由器 IP 的各种网络功能,

如 ARP 请求,ICMP,以及数据的转发等;其他设备不拥有该虚拟 IP,状态是 BACKUP,除了接收 MASTER 的VRRP 状态通告信息外,不执行对外的网络功能。当主机失效时,BACKUP 将接管原先 MASTER 的网络功能。VRRP 协议使用多播数据来传输 VRRP 数据,VRRP 数据使用特殊的虚拟源 MAC 地址发送数据而不是自身网卡的 MAC 地址,VRRP 运行时只有 MASTER 路由器定时发送 VRRP 通告信息,表示 MASTER 工作正常以及虚拟路由器 IP(组),BACKUP 只接收 VRRP 数据,不发送数据,如果一定时间内没有接收到 MASTER 的通告信息,各 BACKUP 将宣告自己成为 MASTER,发送通告信息,重新进行 MASTER 选举状态。

 

3.4.4 keepalived+nginx实现高可用负载均衡

实验拓扑

在这里插入图片描述

keepalived配置

[root@www ~]# yum install -y keepalived

[root@mg-nginx-01 keepalived]# cp -a keepalived.conf keepalived.conf.bak      #养成好习惯,先备份默认配置文件
[root@mg-nginx-01 keepalived]# vi keepalived.conf
[root@mg-nginx-01 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
  router_id mg-nginx-01                #与备节点的配置不同
}

vrrp_instance VI_1 {
    state MASTER                            #与备节点的配置不同
    interface ens33
    virtual_router_id 51
    priority 150                                #与备节点的配置不同
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.217.211/24
    }
}

备节点

[root@mg-nginx-02 keepalived]# cp -a keepalived.conf keepalived.conf.bak        #养成好习惯,先备份默认配置文件
[root@mg-nginx-02 keepalived]# vi keepalived.conf
[root@mg-nginx-02 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
  router_id mg-nginx-02                     #与主节点的配置不同
}

vrrp_instance VI_1 {
    state BACKUP                                  #与主节点的配置不同
    interface ens33
    virtual_router_id 51
    priority 100                                     #与主节点的配置不同
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    192.168.217.211/24
    }
}

完成

查看日志

[root@mg-nginx-01 ~]# tail -f /var/log/messages
Jun  9 23:57:39 localhost Keepalived_vrrp[1114]: Sending gratuitous ARP on ens33 for 192.168.217.211
Jun  9 23:57:39 localhost Keepalived_vrrp[1114]: Sending gratuitous ARP on ens33 for 192.168.217.211
Jun  9 23:57:39 localhost Keepalived_vrrp[1114]: Sending gratuitous ARP on ens33 for 192.168.217.211
Jun  9 23:57:39 localhost Keepalived_vrrp[1114]: Sending gratuitous ARP on ens33 for 192.168.217.211
Jun  9 23:57:44 localhost Keepalived_vrrp[1114]: Sending gratuitous ARP on ens33 for 192.168.217.211

 
 

3.4.5 反向代理模块
1.proxy

1.模块名称

http_proxy_module(默认)

http_proxy_module:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

 

2.模块功能

对上游服务器使用http/https进行反向代理

 

3.模块指令
proxy_pass URL;

  • URL参数规则
    必须以http://或https://开头,接下来是域名,IP,unix socket地址,upstream,且可加端口
    当URL参数中携带URI与否,会导致向上游请求的URL不同
    • 不携带URI 直接转发
    • 携带URI 根据location替换
  • URL参数可以携带变量
  • 更复杂的URL替换可以在location中添加rewrite break语句

proxy_method
修改请求方法

proxy_http_version 1.0|1.1;
修改http版本,默认1.0

proxy_set_header FIELD VALUE;
如果值为空,则不发送此头部

在这里插入图片描述

 

4.模块实战

配置

web server

server {
	listen 8001;
	default_type text/plain;
	return 200 '8001 server response.\n';
}

server {
	listen 8002;
	default_type text/plain;
	return 200 '8002 server response. uri: $uri\n';
}

upstream server

upstream rrtest {
        server 192.168.217.155:8001 weight=2 max_conns=2 max_fails=2 fail_timeout=5;
        server 192.168.217.155:8002;
        keepalive 32;

}

server {
        listen 80;
        server_name rrtest.pl.com;

        location / {
                proxy_pass http://rrtest;
                #proxy_pass http://rrtest/test/;
                proxy_http_version 1.1;
                proxy_set_header Connection "";
        }
}

测试 curl proxy.pl.com/test/test1/test2

proxy_pass http://rrtest;

[root@mg-nginx-01 ~]# curl rrtest.pl.com
8001 server response.
[root@mg-nginx-01 ~]# curl rrtest.pl.com
8002 server response. uri: /
[root@mg-nginx-01 ~]# curl rrtest.pl.com
8001 server response.
[root@mg-nginx-01 ~]# curl rrtest.pl.com
8001 server response.
[root@mg-nginx-01 ~]# curl rrtest.pl.com
8002 server response. uri: /
[root@mg-nginx-01 ~]# curl rrtest.pl.com/test/test1/test2
8001 server response.
[root@mg-nginx-01 ~]# curl rrtest.pl.com/test/test1/test2
8002 server response. uri: /test/test1/test2

proxy_pass http://rrtest/test/

[root@mg-nginx-01 ~]# curl rrtest.pl.com
8001 server response.
[root@mg-nginx-01 ~]# curl rrtest.pl.com
8002 server response. uri: /test
[root@mg-nginx-01 ~]# curl rrtest.pl.com
8001 server response.
[root@mg-nginx-01 ~]# curl rrtest.pl.com
8001 server response.
[root@mg-nginx-01 ~]# curl rrtest.pl.com
8002 server response. uri: /test
[root@mg-nginx-01 ~]# curl rrtest.pl.com/test/test1/test2
8001 server response.
[root@mg-nginx-01 ~]# curl rrtest.pl.com/test/test1/test2
8002 server response. uri: /test/test/test1/test2

 
 

2.缓存功能

浏览器缓存

  • 优点

    使用有效缓存时,没有网络消耗,速度最快
    即使有网络消耗,但对失效缓存使用304做到消耗最小化

  • 缺点

    仅提升一个用户的体验

浏览器缓存相关

在这里插入图片描述

  • Etag
    ETag HTTP响应头是资源的特定版本的标识符。可以让缓存更高效,并节省带宽。
    如果给定URL中的资源更改,则一定要生产新的ETag值。

  • If-None-Match
    条件式首部,当且仅当服务器上没有任何资源的ETag与这个首部中的相匹配时,服务器才返回请求资源并响应200
    如果匹配失败则返回304 Not Modified

  • If-Modified-Since
    条件式首部,服务器只在所请求的资源在给定的日期之后对内容进行修改过的情况下才将资源返回,状态码200
    如果未经修改则返回304

  • Expires

    根据expires指令自动计算时间添加Expires和Cache-Control到响应头部
    max时间最大
    Expires:根据Cache-Control计算
    Cache-Control:315360000(10年)
    epoch时间最小
    Cache-Control:no-cache
    Expires:根据Cache-Control计算
    time设定具体时间
    @20h30m
    正数
    负数
    Cache-Control:no-cache
    Expires:根据Cache-Control计算

在这里插入图片描述

 

实战

配置

server {
        listen 80;
        server_name expire.pl.com;
        root html;
        location / {
                expires 1h;
                #expires -1h;
                #expires @20h30m;
        }

}

测试 curl -I expire.pl.com

expires 1h;

[root@mg-nginx-01 ~]# curl -I expire.pl.com
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Wed, 10 Jun 2020 11:28:40 GMT
Content-Type: text/html
Content-Length: 642
Last-Modified: Sat, 30 May 2020 01:57:11 GMT
Connection: keep-alive
ETag: "5ed1bd77-282"
Expires: Wed, 10 Jun 2020 12:28:40 GMT
Cache-Control: max-age=3600
Accept-Ranges: bytes

expires -1h;

[root@mg-nginx-01 ~]# curl -I expire.pl.com
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Wed, 10 Jun 2020 11:30:22 GMT
Content-Type: text/html
Content-Length: 642
Last-Modified: Sat, 30 May 2020 01:57:11 GMT
Connection: keep-alive
ETag: "5ed1bd77-282"
Expires: Wed, 10 Jun 2020 10:30:22 GMT
Cache-Control: no-cache
Accept-Ranges: bytes

expires @20h30m;

[root@mg-nginx-01 ~]# curl -I expire.pl.com
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Wed, 10 Jun 2020 11:31:17 GMT
Content-Type: text/html
Content-Length: 642
Last-Modified: Sat, 30 May 2020 01:57:11 GMT
Connection: keep-alive
ETag: "5ed1bd77-282"
Expires: Wed, 10 Jun 2020 12:30:00 GMT
Cache-Control: max-age=3523
Accept-Ranges: bytes

无缓存

在这里插入图片描述

有缓存后

在这里插入图片描述

 
 

nginx缓存

  • 优点
    提升所有用户体验
    相比浏览器缓存,有效降低上游服务器负载
    通过304响应减少与上游服务器之间的网络消耗
  • 缺点
    用户仍然有网络消耗

proxy_cache

nginx缓存流程

在这里插入图片描述

结论:同时使用浏览器和nginx缓存

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值