迹忆客 EN >

当前位置:主页 > 学无止境 > 网络 >

Nginx负载均衡设置

作者:迹忆 最近更新:2022/11/19 浏览次数:

在现在阶段,负载均衡是一个被普遍应用的技术。Nginx作为http的负载均衡服务器,被应用的越来越广泛。

Nginx设置负载均衡有三种方式:

round-robin——这种方式是对于访问请求来说,将请求循环分发到应用服务器上。

least-connect——下一个请求被分发到当前活动连接数最少的应用服务器上。

ip-hash——通过hash方法决定将下一个请求分发到哪一个应用服务器上。

下面我们分别来看这三种方式

Round-robin 默认方式

这是最简单的分发方式,配置也最简单。我们看下面的例子。

例一

http {
    upstream onmpw {
        server 192.168.144.128;
        server 192.168.144.132;
        server 192.168.144.131;
    }
    server {
        listen 80;
        location / {
            proxy_pass http://onmpw;
        }
    }
}

在例一中,有三个应用实例。因为在upstream中我们没有指定负载方式,所以默认为round-robin方式。也就是说,在第一次请求的时候,nginx负载均衡将请求分发到128应用服务器上;第二次请求会被分发到132应用上;第三次请求被分发到131应用上;第四次的时候就又被分发到128上面。就这样循环往复将请求在各应用服务器上分发。

Least connect 负载均衡设定方式

另一种负载均衡方式就是least-connect。这种方式看起来似乎对于应用服务器来说更公平。因为在一些请求需要花费很长时间才能完成的情况下,这种方式就体现出了它的优势了。使用这种方式,负载服务尽量不会去是一台超负荷的应用服务器区响应过多的请求。它会把新的请求分发到负载量较小的应用服务上面。

其设置方式如下(只需在例一基础上稍加改动)

例二

upstream onmpw {
least_conn;
    server 192.168.144.128;
    server 192.168.144.132;
    server 192.168.144.131;
}

会话持久性——负载均衡

在这里需要注意一下,不管是round-robin也好还是least-connect也罢。这两种方式在处理请求的时候,都有可能会把后续的请求分发到其他的不同应用服务器上。它们是不能保证同一个客户端的请求总是转向同一个应用服务器。

如果我们需要将一个客户端绑定到一个特定的应用服务器上面——也就是通过将特定的客户端请求分发到特定的应用服务器上面,使客户端的会话(session)具有持久性。这时候需要用到的就是ip-hash的负载方式了。

使用这种方式,通过将客户端的ip地址作为hash键去决定将客户端的请求分发到哪一台应用服务器上面。这种方式保证了来自同一客户端的请求总是会被分发到特定的应用服务器上面去。除非这个特定的服务器停止工作了。

想要配置ip-hash的负载方式,只需在例一的upstream中添加 ip_hash即可

例三

upstream onmpw {
ip_hash;
    server 192.168.144.128;
    server 192.168.144.132;
    server 192.168.144.131;
}

临时移除某台应用服务器

如果某一台应用服务器需要临时移除,不允许请求访问。我们可以在其后面使用 down 来标记此台服务器,这是nginx不会将请求分发到这台应用服务器上面。当先前由这台服务器响应的客户端再次发起请求的时候,nginx会自动将其分发到其他的应用服务器上面。

例四

upstream onmpw {
    server 192.168.144.128;
    server 192.168.144.132;
    server 192.168.144.131 down;
}  

应用服务器的权重

默认情况下,Nginx是按照round-robind的方式循环向应用服务器组的分发请求的。我们可以通过weight参数来设置每个应用服务器的权重。默认情况下每台应用的权重为1。

例五

upstream onmpw {
    server 192.168.144.128 weight=5;
    server 192.168.144.132;
    server 192.168.144.131;
}

在例五中,128的权重设为5,其他的两台应用服务器的权重则默认为1。按照此种情况,每有7个请求,有5个会分发到128应用上,其他两个则分别分发到132和131应用上。
这就是给服务器设置权重的作用。

备用应用服务器

在实际应用中,并不是我们所有的应用服务器都要参与。其中,我们可以将一台或者几台应用作为备用服务器。当其他的应用服务器出现问题不能访问的时候,Nginx会自动启动备用的应用服务器。

下面我们在例五的基础上稍加改动

例六

upstream onmpw {
    server 192.168.144.128 weight=5;
    server 192.168.144.132;
    server 192.168.144.131 backup;
}

这个例子和例五的作用不同的是,131这台应用作为备用服务器。当没有6个请求的时候,有5个会分发给128应用,1个分发到132上面。只有当128和132应用服务器都不能访问的时候,Nginx会自动把请求分发到131应用上面。

上面是对负载均衡配置几种方式的简单介绍,希望对大家有帮助。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

How to Install Nginx on Ubuntu 20.04?

发布时间:2025/04/07 浏览次数:157 分类:OPERATING SYSTEM

Nginx is one of the most popular web servers in the world, responsible for hosting some of the largest and most trafficked sites on the Internet. It is a lightweight application software that can be used as a web server or a reverse proxy.

Install WordPress with Nginx on Ubuntu 18.04

发布时间:2025/04/07 浏览次数:86 分类:OPERATING SYSTEM

WordPress is one of the most popular open source content management systems (CMS) with a market share of up to 60% compared to other CMS like Drupal or Joomla. WordPress can be used to develop any type of website, be it a blog, a small busi

Solution to incorrect access log time when deploying Nginx in Docker

发布时间:2025/03/26 浏览次数:167 分类:Docker

In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t

Docker deploys nginx php application

发布时间:2025/03/26 浏览次数:132 分类:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

Nginx load balancing settings

发布时间:2025/03/18 浏览次数:198 分类:NETWORK

At this stage, load balancing is a widely used technology. Nginx, as a load balancing server for http, is being used more and more widely. There are three ways to set up Nginx load balancing: Round-robin - This method distributes access req

Nginx load balancing health_check analysis

发布时间:2025/03/18 浏览次数:56 分类:NETWORK

In Nginx load balancing, it is difficult to guarantee that every application server can run normally all the time. However, we can set Nginx to detect these application servers and detect which of them are inaccessible. There are two ways t

HTTP2 Tutorial - How to Configure HTTP2 with Nginx

发布时间:2025/03/17 浏览次数:195 分类:NETWORK

HTTP2 was officially released in 2015. If your website is still using HTTP/1.1, you may be out of date. Don't worry, here we will see how to use Nginx to upgrade your website to HTTP2. Install Nginx I feel that this column is redundant. Sin

Deep understanding of Nginx's server block selection algorithm

发布时间:2025/03/17 浏览次数:98 分类:NETWORK

Nginx is one of the most popular web servers in the world. It can successfully handle high loads with many concurrent client connections and can be used as a web server, mail server, or reverse proxy server. In this article, we will discuss

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便