Nginx实现七层负载均衡

Nginx实现七层负载均衡

为什么要使用负载均衡

1.解决web服务器单点故障,让web服务器做成一个集群
2.将请求平均下发给后端的web服务器

# 负载均衡的叫法
LB :Load Balance
SLB : Server Load Balance

# 公有云中的叫法
阿里云 :SLB
腾讯云 :CLB
ucloud :ULB
AWS :ELB

# 负载均衡产品
软件
	-nginx
	-HAproxy
	-LVS
硬件
	-F5

四层负载均衡和七层负载均衡的区别

1.一个是四层(传输层),一个是七层(应用层)
2.四层传输速度要比七层块
3.四层无法识别域名,七层可以识别域名

负载均衡实现场景

Nginx要实现负载均衡要用到proxy_pass代理模块配置。

Nginx负载均衡与Nginx代理不同地方在于,nginx的一个location仅能代理一台服务器,而nginx负载均衡则是将客户端请求代理转发至一组upstream虚拟服务池。

负载均衡配置语法

Syntax:	upstream name { ... }
Default:	—
Context:	http

uostream backend {
	server xxx;
	server xxx;
}

官方案例配置

# upstream模块配置

模块名   后端主机池:(根据域名来取名)
upstream backend {
    server backend1.example.com       weight=5;
    server backend2.example.com:8080;
    server unix:/tmp/backend3;

    server backup1.example.com:8080   backup;
    server backup2.example.com:8080   backup;
}

server {
    location / {
        proxy_pass http://backend;
    }
}

配置负载均衡

环境准备

主机名 WanIP LanIP 角色 应用
lb01 10.0.0.5 172.16.1.5 负载均衡 nginx
web01 10.0.0.7 172.16.1.7 web网站 nginx、php
web02 10.0.0.8 172.16.1.8 web网站 nginx、php

编辑nginx配置文件

[root@web01 conf.d]# vim lb.zh.com.conf 
server{
        listen 888;
        server_name lb.zh.com;
        root /code/lb;
        index index.html;
}

server{
        listen 888;
        server_name lb.zh.com;
        root /code/lb;
        index index.html;
}

# 创建站点目录
[root@web01 conf.d]# mkdir -p /code/lb 
[root@web02 conf.d]# mkdir -p /code/lb  

# 编辑html文件
[root@web02 conf.d]# echo 'web02' > /code/lb/index.html
[root@web01 conf.d]# echo 'web01' > /code/lb/index.html

配置负载均衡

[root@ib01 conf.d]# vim lb.zh.com.conf 
upstream lb.zh.com {
        server 172.16.1.7:888;
        server 172.16.1.8:888;

}
server{
        listen 80;
        server_name lb.zh.com;

        location /{
                proxy_pass http://lb.zh.com;
        }
}

负载均衡常见典型故障

如果后台服务器连接超时,nginx是本身有机制的,如果出现一个节点down掉的时候,Nginx是会根据你具体负载均衡的设置,将请求转移到其他的节点上,但是,如果后台服务器连接没有down掉,但是返回错误异常错误码,这个时候你需要加一个负载均衡的设置,如下:proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;意思是,当其中一台返回错误码时,可以分配到下一台服务器程序继续处理,提高平台访问成功率
# 解决方案


upstream lb.zh.com {
        server 172.16.1.7:888;
        server 172.16.1.8:888;

}
server{
        listen 80;
        server_name lb.zh.com;

        location /{
                proxy_pass http://lb.zh.com;
                # 遇到如下状态码的机器,跳过请求的下发,直接发到其他正常的服务器
                proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
        }

}

负载均衡调度算法

调度算法 概述
轮询(rr) nginx做负载均衡默认使用轮询的调度算法:将请求下发到后端的web服务器
加权轮询(wrr) 增减权重,根据服务器的配置,给轮询加上权重
源Ip(ip_hash) 根据用户的ip,将同一ip地址的请求下发到同一台服务器上
源url(url_hash) 根据用户访问的URL,将同一URL的请求下发到同一台服务器上
最小连接数(least_conn) 哪台服务器的连接数最少,就将请求下发到该服务器上

调度算法配置文件

# 1.加权轮询
upstream lb.zh.com {
        server 172.16.1.7:888 weight=5;
        server 172.16.1.8:888;
}         
# 2.ip_hash
upstream lb.zh.com {
        ip_hash;
        server 172.16.1.7:888;
        server 172.16.1.8:888;
}        

负载均衡后端配置

# 1.down 状态:只是负载均衡不对该标识的服务器下发请求,后端服务器并没有真正宕机
upstream lb.zh.com {
        server 172.16.1.7:888 down;
        server 172.16.1.8:888;
}          

# 2.backup 状态:备份,当其他没有backup标识机器都宕机是,才会给服务器发送请求
upstream lb.zh.com {
        server 172.16.1.7:888 backup;
        server 172.16.1.8:888;
} 
# 3.额外参数
max_fails :负载均衡访问后端,最大错误数,到该指定次数后,不在给该服务器发送请求
fail_timeout :配合max_fails使用,规定不发请求的时间段

upstream lb.zh.com {
        server 172.16.1.7:888 max_fails=3 fail_timeout=10s;
        server 172.16.1.8:888 max_faols=3 fail_timeout=10s;
} 

# 4.max_conn :限制该后端web服务器最大连接数为100个
upstream lb.zh.com {
        server 172.16.1.7:888 max_fails=3 fail_timeout=10s max_conns=100;
        server 172.16.1.8:888 max_faols=3 fail_timeout=10s;
}

nginx负载均衡健康检查模块

# 安装环境依赖
[root@ib01 nginx-1.22.0]# yum -y install pcre-devel openssl-devel

# 1.停掉yum安装的nginx
[root@ib01 ~]# systemctl stop nginx                   

# 2.下载nginx源码包
[root@ib01 opt]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

# 3.下载nginx源码包和第三方模块包
[root@ib01 opt]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

# 4.解压nginx源码包和第三方模块包
[root@ib01 opt]# tar xf nginx-1.22.0.tar.gz
[root@ib01 opt]# unzip master.zip 

# 5.创建nginx下载目录
[root@ib01 opt]# mkdir /app

# 6.打补丁
[root@ib01 opt]# cd nginx-1.22.0/
[root@ib01 opt]# patch -p1 <../nginx_upstream_check_module-master/check_1.20.1+.patch 

# 7.生成
[root@ib01 opt]#  ./configure --prefix=/app/nginx-1.22.0 --with-compat -- with-file-aio --with-threads --with-http_addition_module --with- http_auth_request_module --with-http_dav_module --with-http_flv_module --with- http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with- http_random_index_module --with-http_realip_module --with-http_secure_link_module -- with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with- http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with- stream --with-stream_realip_module --with-stream_ssl_module --with- stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 - fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add- module=/opt/nginx_upstream_check_module-maste

# 8.编译&&安装
[root@ib01 opt]# make && make install

# 9.nginx主配置文件添加conf.d
[root@ib01 app]# vim nginx-1.22.0/conf/nginx.conf
include /app/nginx-1.22.0/conf/conf.d/*.conf;

# 10.创建虚拟主机配置文件
[root@ib01 app]# mkdir /app/nginx-1.22.0/conf/conf.d

# 11.编辑负载均衡配置文件,添加location
upstream lb.zh.com {
        server 172.16.1.7:888 max_fails=3 fail_timeout=10s max_conns=100;
        server 172.16.1.8:888 max_fails=3 fail_timeout=10s;
        check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
        # interval  检测间隔时间,单位毫秒
        # rise  表示请求两次正常,标记此后端为up
        # fall  表示请求3次失败,标记此后端状态为down
        # type   类型为tcp
        # timeout  超时时间,单位为毫秒
}
server{
        listen 80;
        server_name lb.zh.com;
        location /{
                proxy_pass http://lb.zh.com;
        }
        location /ooo{
                check_status;
        }
}

# 12.检测语法
[root@ib01 app]# ./nginx-1.22.0/sbin/nginx -t 

# 13.启动nginx
[root@ib01 app]# ./nginx-1.22.0/sbin/nginx 

posted @   FYytfg  阅读(174)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示