Home  >  Article  >  Backend Development  >  Nginx load balancing task scheduling and health check

Nginx load balancing task scheduling and health check

WBOY
WBOYOriginal
2023-10-15 15:27:191123browse

Nginx load balancing task scheduling and health check

Nginx load balancing task scheduling and health check

Introduction:
In modern Internet applications, high concurrency and performance stability are often important considerations . As a high-performance HTTP server and reverse proxy server, Nginx not only provides static file services, it can also be used for load balancing and distributing requests to multiple back-end servers. This article will discuss the implementation method of task scheduling and health check of Nginx load balancing, and give specific code examples.

Task Scheduling:
Nginx load balancing task scheduling refers to distributing requests to different back-end servers based on different scheduling algorithms. Commonly used scheduling algorithms include polling, weighted polling, IP hash, etc. The following is a configuration example using Nginx:

http {
    upstream backend {
        server backend1.example.com;
        server backend2.example.com;
        server backend3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://backend;
        }
    }
}

In the above configuration, a load balancing backend group named backend is defined, which includes three backend servers. In the server block, request proxy_pass to backend to implement a simple polling scheduling algorithm.

Health check:
In addition to task scheduling, health check is also an important function in load balancing. By regularly checking the health status of back-end servers, Nginx can eliminate faulty servers in a timely manner to ensure system availability. The following is an example of health check configuration using Nginx:

http {
    upstream backend {
        server backend1.example.com;
        server backend2.example.com;
        server backend3.example.com;

        check interval=3000 rise=2 fall=3 timeout=1000;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://backend;
        }
    }
}

In the above configuration, the health check function can be enabled by adding the check parameter in the upstream block. Among them, interval indicates the time interval of the check, rise indicates that the server is considered healthy after several consecutive successful checks, and fall indicates that the server is considered unhealthy after several consecutive failed checks. , timeout represents the timeout for each check.

Code example:
In the Nginx configuration, in addition to the above task scheduling and health check configuration, we can also customize more complex scheduling algorithms and health check logic through Lua scripts. The following is a code example that uses Lua script to implement the weighted polling scheduling algorithm and custom health check:

http {
    upstream backend {
        server 10.0.0.1 weight=3;
        server 10.0.0.2 weight=2;
        server 10.0.0.3 weight=1;

        check interval=3000 rise=2 fall=3 timeout=1000 fail_timeout=10s;
        check_http_send "GET /health HTTP/1.0

";
        check_http_expect_alive http_2xx http_3xx;
    }

    server {
        listen 80;

        location / {
            access_by_lua_block {
                -- 加权轮询调度算法
                local peer = ngx.shared.backend:get("current_peer")
                if not peer then
                    peer = "1"
                    ngx.shared.backend:set("current_peer", peer)
                end
                ngx.var.proxy_pass = "http://backend" .. peer
                ngx.shared.backend:incr("current_peer", 1, 1)

                -- 自定义健康检查
                local status = ngx.ctx.balancer_address.status
                if status ~= ngx_balancer.BALANCER_STATUS_OK then
                    ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE)
                end
            }
            proxy_pass http://backend;
        }
    }
}

In the above code example, Lua script is used to implement the weighted polling scheduling algorithm. At the same time, global variables are maintained with the help of ngx.shared, which is used to record the currently scheduled back-end server. In addition, in access_by_lua_block, the health check logic is customized by checking the value of ngx.ctx.balancer_address.status.

Conclusion:
Nginx load balancing task scheduling and health check are one of the key factors to achieve high concurrency and performance stability. Through reasonable task scheduling algorithms and health check strategies, we can improve system performance and reliability. This article introduces the basic concepts of Nginx load balancing task scheduling and health checking, and gives specific code examples using Nginx configuration files and Lua scripts. Readers can adjust and expand according to their own needs. I hope it will be inspiring and helpful to readers.

The above is the detailed content of Nginx load balancing task scheduling and health check. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn