Nginx负载均衡介绍、配置;调度算法;负载均衡状态;健康检查模块;常见问题解决

一、Nginx负载均衡

1.为什么做负载均衡

负载均衡:将请求平均的分配给后端服务器
当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台Web服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现负载的分发。那么会大大提升系统的吞吐率、请求性能、高容灾。往往我们接触的最多的是SLB(Server Load Balance)负载均衡,实现最多的也是SLB。那么SLB它的调度节点和服务节点通常是在一个地域里面。那么它在这个小的逻辑地域里面决定了他对部分服务的实时性、响应性是非常好的。
所以说当海量用户请求过来以后,它同样是请求调度节点,调度节点将用户的请求转发给后端对应的服务节点,服务节点处理完请求后在转发给调度节点,调度节点最后响应给用户节点。这样也能实现一个均衡的作用,那么Nginx则是一个典型的SLB,

2.负载均衡的叫法

负载均衡
负载
Load Balance
LB

3.公有云中叫法

SLB		阿里云负载均衡
LB		青云负载均衡
CLB		腾讯云负载均衡
ULB		ucloud负载均衡

4.常见的负载均衡软件

Nginx		#在1.9版本之前只能做七层,1.9版本之后既能做七层,也能做四层负载均衡
Haproxy		#既能做四层,也能做七层负载均衡
LVS			#只能做四层负载均衡
#LVS是最快的负载均衡软件,其他两个软件需要将请求发送到服务再转发到后端,LVS不用到服务,它相当于将服务器变成了负载均衡,直接转发请求

5.负载均衡类型

#四层负载均衡
所谓四层负载均衡指的是OSI七层模型中的传输层,那么传输层Nginx已经能支持TCP/IP的控制,所以只需要对客户端的请求进行TCP/IP协议的包转发就可以实现负载均衡,那么它的好处是性能非常快、只需要底层进行应用处理,而不需要进行一些复杂的逻辑。

#七层负载均衡
七层负载均衡它是在应用层,那么它可以完成很多应用方面的协议请求,比如我们说的http应用的负载均衡,它可以实现http信息的改写、头信息的改写、安全应用规则控制、URL匹配规则控制、以及转发、rewrite等等的规则,所以在应用层的服务里面,我们可以做的内容就更多,那么Nginx则是一个典型的七层负载均衡SLB

#四层负载与七层负载区别
四层负载均衡数据包在底层就进行了分发,而七层负载均衡数据包则是在最顶层进行分发、由此可以看出,七层负载均衡效率没有四负载均衡高。
但七层负载均衡更贴近于服务,如:http协议就是七层协议,我们可以用Nginx可以作会话保持,URL路径规则匹配、head头改写等等,这些是四层负载均衡无法实现的。
注意:四层负载均衡不识别域名,七层负载均衡识别域名

二、Nginx负载均衡配置

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

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

1.负载均衡配置语法 ngx_http_upstream_module

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

upstream backend {
    server backend1.example.com;
    server backend2.example.com;
}

server {
	listen 80;
	server_name www.linux.com
    location / {
        proxy_pass http://backend;
    }
}

2.环境准备

主机 外网ip 内网ip 身份
lb01 192.168.15.4 172.16.1.4 负载均衡
web01 192.168.15.7 172.16.1.7 web
web02 192.168.15.8 172.16.1.8 web

3.web01准备站点

[root@web01 ~]# vim /etc/nginx/conf.d/node.conf 
server {
    listen 80;
    server_name node.linux.com;

    location / {
        root /code/node;
        inde index.html;
    }
}
[root@web01 conf.d]# mkdir /code/node
[root@web01 conf.d]# echo "我是web01......" > /code/node/index.html
[root@web01 conf.d]# systemctl restart nginx

4.web02准备站点

[root@web02 ~]# vim /etc/nginx/conf.d/node.conf
server {
    listen 80;
    server_name node.linux.com;
    charset utf8;
    
    location / {
        root /code/node;
        index index.html;
    }
}
[root@web02 conf.d]# mkdir /code/node
[root@web02 conf.d]# echo "我是web02......" > /code/node/index.html
[root@web02 conf.d]# systemctl restart nginx

5.配置负载均衡配置文件

[root@lb01 ~]# vim /etc/nginx/conf.d/node_proxy.conf 
upstream node {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
}

server {
    listen 80;
    server_name node.linux.com;

    location / {
        proxy_pass http://node;
        include proxy_params;
    }
}
#检查include文件是否存在
[root@lb01 conf.d]# ll /etc/nginx/proxy_params 
-rw-r--r-- 1 root root 275 Feb 28 15:24 /etc/nginx/proxy_params
[root@lb01 conf.d]# systemctl restart nginx

6.配置优化文件

[root@Nginx ~]# vim /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
 
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;

7.访问页面测试

三、nginx 调度算法

调度算法 概述
轮询 * 按时间顺序逐一分配到不同的后端服务器(默认)
weight 加权轮询,weight值越大,分配到的访问几率越高
ip_hash 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器
url_hash 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
least_conn 最少链接数,那个机器链接数少就分发

1.轮询配置方法

upstream node {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
}

2.加权轮询配置方法

#访问根据配置的权重比例进行分配
upstream node {
    server 172.16.1.7:80 weight=5;
    server 172.16.1.8:80 weight=1;
}

3.ip_hash的配置方法

#根据访问的来源IP分配至同一台服务器
upstream node {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    ip_hash;
}
#经常使用这种方式进行会话保持

四、nginx负载均衡状态

状态 概述
down 当前的server暂时不参与负载均衡
backup 预留的备份服务器
max_fails 允许请求失败的次数
fail_timeout 经过max_fails失败后, 服务暂停时间
max_conns 限制最大的接收连接数

1.down状态配置测试

upstream node {
	#不参与负载均衡任何调度,一般停机维护或者上线的时候使用
    server 172.16.1.7:80 down;
    server 172.16.1.8:80;
}

2.backup状态配置测试

upstream node {
    server 172.16.1.7:80;
    server 172.16.1.8:80 backup;
}

3.访问错误状态

upstream node {
    server 172.16.1.7:80;
    server 172.16.1.8:80 max_fails=3 fail_timeout 10s;
}

4.max_conns限制最大连接数状态配置

upstream node {
    server 172.16.1.7:80;
    server 172.16.1.8:80 max_conns=120;
}

五、nginx健康检查模块(扩展)

[root@lb01 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
[root@lb01 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@lb01 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
[root@lb01 ~]# tar xf nginx-1.14.2.tar.gz
[root@lb01 ~]# unzip master.zip
[root@lb01 ~]# cd nginx-1.14.2/
[root@lb01 nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch

./configure --prefix=/data/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/data/nginx/nginx.conf --error-log-path=/data/log/nginx/error.log --http-log-path=/data/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --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 --add-module=/root/nginx_upstream_check_module-master --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'
[root@lb01 nginx-1.14.2]# make && make install

[root@lb01 nginx-1.14.2]# vim /etc/nginx/conf.d/proxy.conf 
upstream web {
    server 10.0.0.7;
    server 10.0.0.8;
    check interval=3000 rise=1 fall=2 timeout=1000;
}
server {
    listen 80;
    server_name proxy.linux.com;

    location / {
        proxy_pass http://web;
        proxy_set_header host $http_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_http_version 1.1;
    }
    location /upstream_check {
        check_status;
    }
}

六、负载均衡常见问题

1.问题

如果后端服务器出现问题,负载均衡仍然会将请求发送给相应机器 如果后台服务连接超时,Nginx是本身是有机制的,如果出现一个节点down掉的时候,Nginx会更据你具体负载均衡的设置, 将请求转移到其他的节点上,但是,如果后台服务连接没有down掉,但是返回错误异常码了如:504、502、500,这个时候你 需要加一个负载均衡的设置,如下:proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;
意思是,当其中一台返回错误码404,500...等错误时,可以分配到下一台服务器程序继续处理,提高平台访问 成功率。

2.解决问题的模块

Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off ...; Default: proxy_next_upstream error timeout; Context: http, server, location

3.配置

[root@lb01 ~]# vim /etc/nginx/conf.d/wp.com.conf 
upstream blog { 
	server 172.16.1.7; 
	server 172.16.1.8;
}
server { 
	listen 80; 
	server_name wp.com; 
	location / { 
		proxy_pass http://blog;
		include proxy_params;
		#下面这行可以配置,也可以写到include里
		proxy_next_upstream http_500 | http_502 | http_503 | http_504 | http_403 | http_404;
		}
}
posted @ 2021-05-07 20:12  小绵  阅读(481)  评论(0编辑  收藏  举报