Nginx 七层负载均衡

Nginx 负载均衡模块

Nginx 负载均衡的实现需要配置 ngx_http_upstream_module 模块(官方文档),使用此模块时,需要用到反向代理相关的 ngx_http_proxy_module 模块(官方文档),切记不可混淆两个模块 。

Nginx 负载均衡语法

# Example Configuration
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;            <------- 需要用 `ngx_http_proxy_module` 模块完成

}

# Directives
Syntax:	upstream name { ... }
Default:	—
Context:	http			      <-------- 只能写在 http 层


# 实例
[root@lb01 ~]# cat /etc/nginx/conf.d/blog.wqh.com.conf 
upstream blog {
	server 172.16.1.7;
	server 172.16.1.8;
}
server {
        listen  80;
        server_name blog.wqh.com;
        location / {
                proxy_pass http://blog;
			   include proxy_params;
        }
}
[root@lb01 ~]# cat /etc/nginx/conf.d/wecenter.wqh.com.conf 
upstream wecenter {
	server 172.16.1.7;
	server 172.16.1.8;
}
server {
        listen  80;
        server_name wecenter.wqh.com;
        location / {
                proxy_pass http://wecenter;
                include proxy_params;
        }
}

当访问一个网站时,nginx 代理会将请求均衡分配到每个网站服务器中:

# 也可以设置每个服务器分配请求的权重
[root@lb01 ~]# cat /etc/nginx/conf.d/wecenter.wqh.com.conf 
upstream wecenter {
	server 172.16.1.7 weight=1;
	server 172.16.1.8 weight=2;
}
server {
        listen  80;
        server_name wecenter.wqh.com;
        location / {
                proxy_pass http://wecenter;
                include proxy_params;
        }
}

分配权重后,每个服务器处理的请求按比例分配:

Nginx 负载调度算法

调度算法 简称 概述
轮询 round-robin(RR) 按请求的时间顺序,平均分配到后端服务器
加权轮询 weight-round-robin(WRR) 加权轮询,weight 值越大,分配到的请求的比重越高;该权重值,主要是针对实际工作环境中不同的后端服务器硬件配置进行调整的
ip_hash 每个请求按照访问的 IP 的 hash 结果分配,这样来自同一个 IP 的访问请求都会固定到一个后端服务器,一定程度上解决了集群部署环境下session共享的问题
url_hash 每个请求按照访问的 URL 的 hash 结果分配,这样每一个 URL 的访问请求都会固定到一个后端服务器
最小连接数 least_conn 最少连接数算法,哪个后端服务器连接数少,就分配给哪一台后端服务器
智能调整调度算法 fair 动态的根据后端服务器的请求处理到响应的时间进行均衡分配,响应时间短处理效率高的服务器分配到请求的概率高,需要 upstream_fair 模块

Nginx 负载均衡后端状态

状态 概述
down 当前的 server 暂时不参与负载均衡调度,一般用于维护(也可以使用注释)
backup 预留的备份服务器,除非其他服务器全部无法使用,否则不会生效
max_fails 允许请求失败的次数,由 fail_timeout 定义请求超时(即失败)的时间
fail_timeout 经过max_fails失败后,服务暂停时间
max_conns 限制最大的接收连接数
# Example Configuration
[root@lb01 ~]# vi /etc/nginx/conf.d/blog.wqh.com.conf
upstream blog {
        server 172.16.1.7 down;          
        server 172.16.1.8 backup;
        server 172.16.1.9 max_fails=2 fail_timeout=10s;     # 此两条配置,必须配合 proxy_next_upstream 使用,否则不起作用
}
server {
        listen  80;
        server_name blog.wqh.com;
        location / {
                proxy_pass http://blog;
                proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
                include proxy_params;
        }
}

解决常见故障

如果后台服务连接超时,Nginx是本身是有机制的,如果出现一个节点宕停机的时候,Nginx 会更据你具体负载均衡的设置,将请求转移到其他的节点上,但是,如果后台服务连接没有停机,但是返回错误异常码(504,502,500)的时候,你需要加一个负载均衡的设置,如下:

proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;

意思是,当其中一台返回错误码 404,502 等错误时,可以分配到下一台后端服务器程序处理,提高平台访问成功率 。

# 添加 proxy_next_upstream 字段
[root@lb01 ~]# cat /etc/nginx/conf.d/blog.wqh.com.conf
upstream blog {
	server 172.16.1.7;
	server 172.16.1.8;
}
server {
        listen  80;
        server_name blog.wqh.com;
        location / {
        	 proxy_pass http://blog;
        	 # 如果出现 500 502 503 504 错误,则分配请求到下一台后端服务器处理
			proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
			include proxy_params;
        }
}
原文地址:https://www.cnblogs.com/zzzwqh/p/12963454.html