Nginx七层负载均衡

Nginx七层负载均衡

负载均衡介绍

为什么要使用?
当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台Web服务器组成
集群,前端使用 Nginx 负载均衡,将请求分散的打到我们的后端服务器集群中,实现负载的分发。那么会大大提升
系统的吞吐率、请求性能、高容灾

负载均衡的叫法

  • 负载均衡
  • 负载
  • Load Balance
  • LB

公有云中的叫法

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

常见的负载均衡软件

Nginx
LVS
HAproxy
F5

环境准备

主机名角色
lb01负载均衡
web01网站
web02网站

配置负载均衡

# 1.编辑负载均衡配置文件
[root@lb01 nginx]# vim /etc/nginx/conf.d/blog_upstream.conf
upstream blog_tjh_com {
   server 172.16.1.7;
   server 172.16.1.8;
}
server{
   listen 80;
   server_name blog.tjh.com;
   location / {
       proxy_pass http://blog_drz_com;
       include /etc/nginx/proxy_params;
   }
}
# 2.重新加载nginx
[root@lb01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d]# systemctl reload nginx
# upstrem模块定义连接池,只能配置在http层

负载均衡的调度算法

算法英文作用
轮询(默认)rr将请求轮流分配给后端服务器
加权轮询wrr将请求按照权重分配给后端的服务器
ip哈希ip_hash可以做会话保持
url哈希url_hash按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
最小连接数least_conn最少链接数,那个机器链接数少就分发

负载均衡常见问题优化

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

server{
   listen 80;
   server_name blog.tjh.com;
   location / {
       proxy_pass http://test_upstream_com;
       proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
       include /etc/nginx/proxy_params;
   }
}

负载均衡后端状态

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

Nginx负载均衡健康检查

在Nginx官方模块提供的模块中,没有对负载均衡后端节点的健康检查模块,但可以使用第三方模块。
nginx_upstream_check_module 来检测后端服务的健康状态。

安装依赖包

yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch 

下载nginx源码包以及nginx_upstream_check模块第三方模块

 wget http://nginx.org/download/nginx-1.14.2.tar.gz
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

解压nginx源码包以及第三方模块

tar xf nginx-1.14.2.tar.gz
 unzip master.zip

.进入nginx目录,打补丁(nginx的版本是1.14补丁就选择1.14的,p1代表在nginx目录,p0是不在nginx目录)

[root@lb02 ~]# cd nginx-1.16.1/
[root@lb02 nginx-1.16.1]# patch -p1 <../nginx_upstream_check_module-
master/check_1.16.0+.patch
./configure --prefix=/app/nginx-1.16.1 --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 && make install

在已有的负载均衡上增加健康检查的功能

vim proxy_web.conf
upstream web {
 server 172.16.1.7:80 max_fails=2 fail_timeout=10s;
 server 172.16.1.8:80 max_fails=2 fail_timeout=10s;
 check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
  #interval 检测间隔时间,单位为毫秒
  #rise   表示请求2次正常,标记此后端的状态为up
  #fall   表示请求3次失败,标记此后端的状态为down
  #type   类型为tcp
  #timeout  超时时间,单位为毫秒
}
server {
 listen 80;
 server_name web.tjh.com;
 location / {
   proxy_pass http://web;
   include proxy_params;
 }
 location /upstream_check {
   check_status;
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值