Nginx实战应用-负载均衡的配置和算法该怎么做?


一、什么是负载均衡?

负载均衡: 将用户的访问请求均衡的分散到后端的真正提供服务的机器上
负载均衡器: 实现负载均衡功能的一个机器
load balancer --》LB
load balancing

二、为什么需要负载均衡?

1.能够将大量的请求比较均匀的分散到后端,不会导致某台访问量过大,某个服务又没有访问量

2.高可用(对后端的服务器进行健康检测,如果后端那台服务器出现问题,就不会再将请求转发给它,从而避免用户访问不了服务器,启动一个容错的功能)

[root@mysql-server ~]# hostnamectl  set-hostname load-balancer
[root@mysql-server ~]# su
[root@load-balancer ~]#

nginx 来实现负载均衡功能
nginx是一个web服务器,可用实现http功能,但是它也可以对http访问进行负载均衡

1.编译安装nginx

[root@load-balancer ~]# cat onekey_install_shediao_nginx_v10.sh
#!/bin/bash

2.解决软件的依赖关系,需要安装的软件包

yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make psmisc net-tools lsof vim wget

3.新建luogan用户和组

id  sanchuang || useradd sanchuang -s /sbin/nologin

4.下载nginx软件

mkdir  /sanchuang99 -p
cd /sanchuang99
wget  http://nginx.org/download/nginx-1.21.1.tar.gz

5.解压软件

tar xf nginx-1.21.1.tar.gz
#进入解压后的文件夹
cd nginx-1.21.1

6.编译前的配置

./configure --prefix=/usr/local/scsanchuang99  --user=sanchuang --group=sanchuang  --with-http_ssl_module   --with-threads  --with-http_v2_module  --with-http_stub_status_module  --with-stream

7.如果上面的编译前的配置失败,直接退出脚本

if (( $? != 0));then
  exit
fi
#编译
make -j 2
#编译安装
make  install

#修改PATH变量
echo  "PATH=$PATH:/usr/local/scsanchuang99/sbin" >>/root/.bashrc
#执行修改了环境变量的脚本
source /root/.bashrc


#firewalld and selinux

#stop firewall和设置下次开机不启动firewalld
service firewalld stop
systemctl disable firewalld

#临时停止selinux和永久停止selinux
setenforce 0
sed  -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config

8.开机启动

chmod +x /etc/rc.d/rc.local
echo  "/usr/local/scsanchuang99/sbin/nginx" >>/etc/rc.local

9.运行安装脚本

[root@load-balancer ~]# bash onekey_install_shediao_nginx_v10.sh
[root@load-balancer ~]# su - root  切换用户,加载修改了的PATH变量
上一次登录:二 8月 24 11:26:16 CST 2021pts/2 上
[root@load-balancer ~]# which nginx  查看nginx的命令
/usr/local/scsanchuang99/sbin/nginx

[root@load-balancer ~]# nginx  启动nginx
[root@load-balancer ~]# nginx -s stop  关闭nginx
[root@load-balancer ~]# nginx  启动nginx

[root@load-balancer ~]# ps aux|grep nginx  查看nginx的进程
root       7569  0.0  0.0  46220  1160 ?        Ss   11:28   0:00 nginx: master process nginx
sanchua+   7570  0.0  0.1  46680  1912 ?        S    11:28   0:00 nginx: worker process
root       7572  0.0  0.0 112824   980 pts/2    S+   11:29   0:00 grep --color=auto nginx
[root@load-balancer ~]# ss -anplut|grep nginx  查看nginx的端口
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=7570,fd=6),("nginx",pid=7569,fd=6))
[root@load-balancer ~]#

10.配置nginx里的负载均衡功能

[root@load-balancer ~]# cd /usr/local/scsanchuang99/  进入nginx编译安装指定的目录
[root@load-balancer scsanchuang99]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@load-balancer scsanchuang99]# cd conf/  进入配置文件的命令
[root@load-balancer conf]# ls
fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
fastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf
[root@load-balancer conf]#
  nginx.conf  是nginx的配置文件
  [root@load-balancer conf]# vim nginx.conf

upstream  上游-->实现负载均衡的指令
 proxy 代理
 pass 通过

⚠️附赠一个学习的网址哈☝️:
nginx.org

三、负载均衡的算法(方法)

1.轮询

roundrobin  --》rr  --》默认,默认情况下所有的服务器的权重值都是1 ,值越大优先级越好
      加权轮询
        server 192.168.0.17  weight=1;
        server 192.168.0.18 weight=2;
        server 192.168.0.19;
        server 192.168.0.7;

2.ip_hash 基于客户端的ip地址

ip_hash 基于客户端的ip地址做负载均衡,相同的ip地址转发到同一个服务器 --》用户的会话信息需要保存的,尽量让这个客户机每次都访问相同的一台
会话信息–》登录信息,购物车里

3.least-connected 最小连接数

http{
  
   upstream  scweb {     #定义一个负载均衡器名字叫scweb
        server 192.168.0.17:8080;
        server 192.168.0.18:8080;
        server 192.168.0.19:8080;
        server 192.168.0.7:8080;

   }
 server {
        listen       80;          #监听80端口
        server_name  www.sc.com;  #为www.sc.com 域名服务
        location / {
                proxy_pass http://scweb ;     #调用负载均衡器
        }
.....省略很多配置
}
[root@load-balancer conf]# nginx -s reload 重新加载配置文件--》相当于重启了nginx服务
[root@load-balancer conf]#

[root@load-balancer conf]# ps axu|grep nginx
root       7569  0.0  0.1  46356  2016 ?        Ss   11:28   0:00 nginx: master process nginx
sanchua+  12158  0.0  0.1  46816  2044 ?        S    11:58   0:00 nginx: worker process
root      12160  0.0  0.0 112824   980 pts/2    S+   11:59   0:00 grep --color=auto nginx
[root@load-balancer conf]#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Nginx负载均衡模块使用的算法是根据请求的分发策略来决定将请求转发给哪台后端服务器。Nginx支持多种负载均衡算法,常见的策略有以下几种: 1. 轮询(Round Robin):默认的负载均衡算法Nginx按照请求的顺序依次将请求分发给后端服务器,实现请求的轮流分配。适用于后端服务器性能相当的情况。 2. IP哈希(IP Hash):根据客户端的IP地址进行哈希计算,将同一客户端的请求分发到同一台后端服务器。这样可以保证同一用户的请求始终被转发到同一台服务器,适用于需要保持会话状态的应用。 3. 最少连接(Least Connections):将请求分发给当前连接数最少的后端服务器,实现负载均衡。适用于后端服务器处理能力不均衡的情况。 4. 加权轮询(Weighted Round Robin):根据后端服务器的权重分配请求。权重越高的服务器,接收到的请求越多。适用于后端服务器性能不均衡或者需要更精细的负载均衡控制的情况。 5. 加权最少连接(Weighted Least Connections):根据后端服务器的权重和当前连接数进行计算,将请求分发给当前连接数最少且权重最高的服务器。适用于后端服务器性能不均衡或者需要更精细的负载均衡控制的情况。 除了以上常见的负载均衡策略,Nginx还支持自定义的负载均衡策略,通过配置文件或者第三方模块可以实现更复杂的负载均衡算法。 通过选择合适的负载均衡策略,可以根据具体应用场景和需求来实现后端服务器的负载均衡和性能优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

未末0902

你的鼓励与支持是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值