一.架构图

二.正向代理和反向代理


三.Nginx解析过程

四.Nginx 有master和worker进程
 修改配置文件:   “master"进程只能有一个,而"worker"进程可以有多个,真正负责处理请求的进程就是你看到的"worker"进程,“master"进程其实是负责管理"worker"进程的,除了管理” worker"进程,master"进程还负责读取配置文件、判断配置文件语法的工作. worker_ processes的值通常不会大于服务器中cpu的核心数量,尽量避免了多个worker进程抢占同一个cpu的情况,我们也可以将worker_ processes的值设置为"auto”
https://blog.csdn.net/yrx420909/article/details/104513013/

- accept_mutex如果打开,worker串行的连接请求,就会出现worker压力不均,少量的worker的cpu利用率达到了98%,大部分的worker的压力只有1%左右。
-accept_mutex如果关闭,则所以woker争抢连接,只有一个连接成功,其他的循环等待。所有的worker的压力差别就不大,而且QPS会有大幅提升
默认accept_mutex关闭,通过其他配置
 
https://baijiahao.baidu.com/s?id=1654536346655792143&wfr=spider&for=pc
https://blog.csdn.net/adams_wu/article/details/51669203
五.Nginx事件处理
使用liunx的epoll模型,多路复用器
 如果client1,client2都阻塞了,woker1会处理client3请求,默认每一个work能处理1024个连接请求。
传统的服务器,就是同步阻塞的方式,如果只有1个worker,只有client1处理完,才能处理client2,client3 如果多个worker,client1阻塞了,worker1就不能处理其他的请求,client2只能连接worker2.不阻塞了才能处理其他的 
DNS域名解析
域名经过DNS解析器,解析到Nginx对应的IP上面,然后Nginx进行转发到对应的后端服务上 

六.四层负载与七层负载
四层负载均衡
就是基于IP+端口的负载均衡:在三层负载均衡的基础上,通过发布三层的IP地址(VIP),然后加上四层的端口号,来决定哪些流量需要做负载均衡,对需要处理的流量进行NAT处理,转发至后台服务器,并记录下这个TCP或UDP的流量时由哪台服务器处理的,后续这个连接的所有流量都会同样转发到同一台服务器处理
实现四层负载均衡的软件有:
F5:硬件负载均衡器:功能强大,但成本高
LVS:重量级四层负载软件
nginx:轻量级四层负载软件,带缓存功能,正则表达式较灵活
haproxy:模拟四层转发,较灵活
七层负载均衡
除了根据IP+80端口辨别是否需要处理的流量,还可根据七层的URL、浏览器类别、语言来决定是否要进行负载均衡。举个例子,如果你的Web服务器分两组:一组是中文的,一组是英文的,那么七层负载均衡就可以当用户来访问你的域名时,自动辨别用户语言,然后选择对应语言的服务器组进行负载均衡处理
haproxy:天生负载均衡技能,全面支持七层代理,会话保持,标记,路径转移。
nginx:只在http协议还mail协议上功能比较好,性能与haproxy差不多。
apache:功能较差
MySQL proxy:功能尚可
总结
四层和七层最大的区别就是要效率还是要功能: 四层架构简单,无需解析消息内容,在网络吞吐量及处理性能上高于七层。 而七层负载优势在于功能多,控制灵活强大。
详情参考 https://blog.csdn.net/shijinghan1126/article/details/108756805
八. 参数
1.weight权重 2.max_conns 单个服务最大连接 3.slow_start 刚启动后多少秒后完全恢复连接 4.down 下线 5.backup 当所有服务不可用时启用 6.max_fails 求server错误次数,如果达到了max_fails次数之后server会被标记为故障状态 7.fail_timeout server进入故障状态,而后在fail_timeout时间之后会被重新标记为正常状态 8,expries 静态文件缓存时间
九.负载均衡的方式
1.轮询(默认) 2.weight(权重) 3.ip_hash 4. fair (响应时间短的优先分配) 5. url_hash(按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器) 6. least_conn (最少的连接数)
参考
https://www.cnblogs.com/51core/articles/14303460.html
十.高可用Keepalived
1.双机主备

Keepalived大概的配置参数,下面是backup的配置,每个keepalived都有一个唯一的主键 配置参考:https://www.bilibili.com/video/BV1KP4y1M7qU?p=43  Keepalived 双机主从节点靠心态来通信
Keepalived主节点配置成功了,主节点网卡下面会多一个虚拟Ip,从节点没有虚拟ip, 如果Keepalived主节点挂了,从节点网卡下面会多一个虚拟Ip,如果主节点恢复了,虚拟Ip在主节点,从节点没有, 
如果keepalived节点没有挂,但是Nginx挂了,服务也是不能正常访问的
可以在keepalived中配置脚本来自动的重启Nginx,如果Nginx启动不了,就会把当前的keepalived节点给kill,来切换备用的keepalived节点   
2.双机热备
由于双机主备需要两个云服务器,如果主节点的服务永远不挂,那样从节点的服务器就浪费了,每年就多付出了设备成本。
如果用两组虚拟Ip,对应两个Nginx,互为主备,通过DNS来分配,就不会造成资源浪费了 
在主从的配置文件中,都增加一组配置,分别为对方的主备。注意 Virtual_router_id不能跟之前那一组一致 
如果一个keepalived挂了,另个一个节点的Keepalived就会绑定2个ip 
DNS负载配置
比如一个域名,记录类型都是A,用www形式访问,会有2个IP对应。

可以修改权重比例 
十一.LVS
LVS:是重量级四层负载软件,速度比Nginx快几十倍。数据包返回可以不经过LB,因此高效,几十万并发,Ip+端口转发请求,单功能Nginx单一 Nginx:可以根据内容来进行转发,接收请求来回,效率不太高,1-5万并发,提供很多功能 

1. LVS 三种模式
1.1 NAT模式
跟Nginx类似,LVS会有一个公网的虚拟Ip,请求和返回都经过LVS,这种情况会有性能影响 
1.2 TUN模式
后端服务器需要有网卡,暴露在公网上

1.3 DR模式
后端服务不会直接暴露在公网, 
十二. Nginx+keepalived+Lvs 配置
https://www.bilibili.com/video/BV1KP4y1M7qU?p=59&spm_id_from=pageDriver&vd_source=d9a52d82a1f11ceeeb7021c93269e8d2
|