13@nginx负载均衡_linux

文章目录

Nginx负载均衡

#注意:代理只能代理一台机器
nginx做代理,一个location可以做一个代理

一、为什么要用负载均衡

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


所以说当海量用户请求过来以后,它同样是请求调度节点,调度节点将用户的请求转发给后端对应的服务节点,服务节点处理完请求后在转发给调度节点,调度节点最后响应给用户节点。这样也能实现一个均衡的作用,那么Nginx则是一个典型的SLB

二、常见的负载均衡

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


#负载均衡的称呼

三、负载均衡的常用软件

Nginx    #在1.9版本之前只能做七层,1.9版本之后既能做七层,也能做四层负载均衡
Haproxy #既能做四层,也能做七层负载均衡
LVS #只能做四层负载均衡


#LVS是最快的负载均衡软件,其他两个软件需要将请求发送到服务再转发到后端,LVS不用到服务,它相当于将服务器变成了负载均衡,直接转发请求

四、负载均衡类型

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



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


#四层负载与七层负载区别

五、Nginx负载均衡配置

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

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

10.0.0.4

172.16.1.4

负载均衡

web01

10.0.0.7

172.16.1.7

web

web02

10.0.0.8

172.16.1.8

web

3、准备站点

#web01准备

[root@web01 conf.d]# vim 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




#web02准备

[root@web02 conf.d]# vim node.conf
server {
charset 'utf-8';
listen 80;
server_name node.linux.com;

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

4、负载均衡配置文件

[root@lb01 conf.d]# vim 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

5、优化配置文件

[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;

6、nginx负载均衡常见问题

#如下:
proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;
#当其中一台返回错误码404,500...等错误时,可以分配到下一台服务器程序继续处理,提高平台访问成功率
#配置文件
upstream test {
server 172.16.1.7:80;
server 172.16.1.8:80;
server 172.16.1.9:80;
}
server {

listen 80;
server_name linux.slb.cluster.local.com;

location / {
proxy_pass http://test;
include proxy_params;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404 http_403;
}
}

六、nginx负载均衡调度算法

调度算法

概述

轮询

按时间顺序逐一分配到不同的后端服务器(默认)

weight

加权轮询,weight值越大,分配到的访问几率越高

ip_hash

每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器

url_hash

按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器

least_conn

最少链接数,那个机器链接数少就分发

# 加权,权值越小,访问概率越低,反之则越高
server 172.16.1.7:80 weight=1;
server 172.16.1.8:80 weight=5;
server 172.16.1.9:80 weight=100;

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@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch

#下载源件包
[root@lb02 ~]#wget http://nginx.org/download/nginx-1.16.1.tar.gz

[root@lb02~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

[root@lb02~]# tar xf nginx-1.16.1.tar.gz
[root@lb02~]# unzip master.zip
[root@lb02~]# cd nginx-1.16.1/

# p1:在nginx目录内 p0不在nginx目录内
[root@lb02 nginx-1.16.1]# patch -p1 <../nginx_upstream_check_module-master/check_1.16.1+.patch

[root@lb02 nginx-1.16.1]#./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@lb02 nginx-1.16.1]# make -j && make install


[root@lb02 nginx-1.16.1]# vim /etc/nginx/conf.d/proxy.conf
upstream test {
server 172.16.1.7:80;
server 172.16.1.8:80;
server 172.16.1.9:80;
check interval=3000 rise=1 fall=2 timeout=1000;
}
server {

listen 80;
server_name linux.slb.cluster.local.com;

location / {
proxy_pass http://test;
# proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404 http_403;
include proxy_params;
}

location /upstream_check {
check_status;
}
}

interval:检测间隔
rise:重试测试
fall:错误次数
timeout:超时时间



[root@lb02 nginx]# cat /etc/nginx/conf.d/test.conf
upstream test {
server 172.16.1.7:80;
server 172.16.1.8:80;
server 172.16.1.9:80;
check interval=3000 rise=1 fall=2 timeout=1000;
}
server {

listen 80;
server_name linux.slb.cluster.local.com;

location / {
proxy_pass http://test;
# proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404 http_403;
include proxy_params;
}

location /upstream_check {
check_status;
}
}