迹忆客 EN >

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

Nginx负载均衡health_check分析

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

在Nginx负载均衡中,我们很难保证说每一台应用服务器都能一直正常的运行下去。但是我们可以通过设置Nginx来检测这些应用服务器,检测这些服务器当中不能访问的。

Nginx的检测方式分为两种,一种是被动监测,另一种是主动监测。下面我们分别看一下这两种方式。

被动监测

当Nginx认为一台应用服务器不能被访问的时候,它会暂时停止向这台应用上面分发请求。直到Nginx认为该应用服务器可以再次被访问的时候才会再向这台应用服务器上面分发请求。
要实现对应用服务器的监测,需要通过两个参数来帮助。

fail_timeout——该参数表示停止分发请求至该应用服务器的时间。也就是说,如果Nginx认为一台应用服务器不能被访问了,则Nginx就会停止向这台应用服务器上分发请求。那需要多长时间Nginx才会认为该服务器可以被访问从而向其分发请求呢。这就需要通过该参数来设置这个时间了。

max_fails——设置访问失败的最大次数。当Nginx向一台服务器分发请求,如果失败的次数达到该参数设置的数量,则Nginx认为该应用服务器不能访问。在接下来的请求就不会再发给该应用服务器。直到达到fail_timeout设置的时间才会再次向这台应用分发请求。

例一

http {
    upstream onmpw {
        server 192.168.144.128;
        server 192.168.144.132 max_fails=3 fail_timeout=30s;
        server 192.168.144.131 max_fails=2;
    }
    server {
        listen 80;
        location / {
            proxy_pass http://onmpw;
        }
    }
}

对于fail_timeout和max_fails的默认值分别为10s和1次。也就是说,当Nginx向一台应用服务器发送请求,如果失败则认为该应用服务器不可访问。接下来的10s中请求不再分发给该应用服务器。直到10s以后会再次将请求分发给该应用服务器。

对于例一,我们看到对于132应用,当请求失败次数达到3次。Nginx会在30s内不再向该应用分发请求。直到30s以后会再次分发新的请求到该应用服务器上。对于131应用,当请求次数达到2次,Nginx就会在10s内(因为没有设置fail_timeout,所以默认为10s)不再向这台应用发送请求。

这种方式需要我们在每台应用服务器对应的信息后面设置,所以称其为被动监测。

主动监测

由Nginx定期的向每台应用服务器发送特殊的请求,来监测应用服务器是否可以正常访问。这种方式称为主动监测。

为了实现主动监测这种方式,我们需要在Nginx负载均衡的配置文件中加入health_check指令。除此之外,我们还需要在设置应用服务器信息的组里加入zone指令。

例二

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

在这里我们设置了一组应用服务器。通过一个单一的location,将所有的请求都分发到这组应用服务器上。在这种情况下,每隔5s Nginx Plus就会向每一台应用服务器发送’/’请求。任何一台应用服务器连接错误或者响应超时亦或者是被代理的服务器响应了一个状态码2xx或者是3xx,health_check机制就会认为是失败的。对于任何一台应用服务器,如果health_check失败,则就会被认为是不稳定的。那么Nginx Plus就不再向这台应用服务器分发访问请求。

zone指令定义了一块儿内存空间。这块儿空间存储在各个工作进程中共享的运行环境的状态和应用服务器组的配置信息。这块儿空间应该根据实际情况尽量申请的大一些,要保证能存下这些信息。

下面我们再看这样的一个例子

例三

location / {
    proxy_pass http://onmpw;
    health_check interval=10 fails=3 passes=2;
}

在上面的例三中,interval=10表示两次进行health_check的间隔为10s,如果不设置默认两次的间隔是5s。fails=3表示一台应用服务器如果请求失败次数达到3次,则该应用服务器被认为不能访问。最后是passes=2表示,被认定为不能访问的服务器需要再次进行两次health_check 以后才会再次被认为是可以正常访问的。

在health_check中,我们可以指定请求的url。

例四

location / {
    proxy_pass http://onmpw;
    health_check uri=/some/path;
}

对于onmpw组中的第一台应用服务器128来说,一次health check请求的url是http://192.168.144.128/some/path。

上面两种监测方式是普遍被使用的,希望本文对大家有所帮助。

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便