Table of Contents
Introduction to load balancing
What is load balancing
Why load balancing is needed
Load balancing scheduling algorithm
Load balancing backend node status
Home Operation and Maintenance Nginx How Nginx uses ngx_http_upstream_module to implement load balancing function

How Nginx uses ngx_http_upstream_module to implement load balancing function

May 18, 2023 pm 07:01 PM
nginx

    Introduction to load balancing

    What is load balancing

    Load balancing (Load Balance) means to load (work tasks, access Requests) are balanced and distributed to multiple operating units (servers, components) for execution.

    Why load balancing is needed

    When a single web server faces users directly, it may carry a large number of concurrent requests. A single server may be difficult to load. We need to use multiple web servers to form a The cluster uses the Nginx load balancing function to distribute requests to different back-end servers to achieve load traffic distribution, improve overall performance, and the system's disaster recovery capability.

    • What is the difference between load balancing and proxy

    A proxy is a proxy for a server to be scheduled based on URI and dispatched to application nodes with different functions

    Load balancing is to proxy client requests to a set of upstream resource pools through proxy_pass

    • To achieve load balancing scenarios

    To achieve load The balancing function requires the use of two modules:

    • proxy_pass: proxy module

    • upstream: virtual resource pool

    Example: An official load balancing display

    upstream backend {
        server backend1.example.com       weight=5;
        server backend2.example.com:8080;
        server unix:/tmp/backend3;
    
        server backup1.example.com:8080   backup;
        server backup2.example.com:8080   backup;
    }
    
    server {
        location / {
            proxy_pass http://backend;
        }
    }
    Copy after login

    Example: Complete a small example by yourself

    upstream node {
        server 192.168.10.3:80;
        server 192.168.10.4:80;
    }
    server {
        listen 80;
        server_name www.yyang.com;
        location / {
            proxy_pass http://node;
            include prxoy_params;
        }
    }
    Copy after login

    Load balancing scheduling algorithm

    Polling scheduling

    Assigned to different backend nodes one by one in order, which is also the default algorithm. (Simply speaking, it is 1:1:1)

    Weighted polling
    Considering the different performance of different servers, different weights are given to the nodes so that they can receive the corresponding Number of weighted requests

    server 192.168.10.3:80 weight=3;
    server 192.168.10.4:80 weight=1;
    Copy after login

    The above example means that every 4 requests will be allocated to three requests for 10.3 and one for 10.4, and so on.

    ip_hash

    According to the IP requested by the user, perform a hash operation on the IP, and assign the request to a specific node on the backend for processing based on the calculated value.

    The value range is the first three 8 bits of the ipv4 address or the entire address of ipv6 as the hash key, ensuring that the IP from one client is always passed to the same server, unless the secondary server is unavailable. To put it simply, the first three sets of numbers of 172.16.20.1 and 172.16.20.2 are the same (all 172.16.20)

    ip_hash operation formula: hash (ip) %node_counts=index

    Problems caused by ip_hash:
    A large number of requests for the same IP will cause excessive traffic on a node
    If a node is temporarily offline, the hash value will be recalculated. It is recommended to use the down state

    Example : Note that ip_hash and weight cannot be used at the same time

    ip_hash;
    server 192.168.10.3:80;
    server 192.168.10.4:80;
    Copy after login

    Consistency hash

    In order to avoid the above problems, consistent hash was born, using the modulo method, but it is not correct The number of server nodes is modulo, but modulo 2 raised to the 32nd power. The hash function value is 0~2^32-1. (Forming a virtual ring, user requests will be sent to clockwise adjacent nodes)
    There is a problem: if there are fewer back-end nodes, it may cause data skew, so consistent hash introduces a virtual node mechanism, that is, for Each server computes multiple hashes and places a virtual node at each computed result location.
    What should we do if we want to use ip_hash, but the calculation formula uses consistent hash?

    hash $remote_addr consistent;
    server 192.168.10.3:80;
    server 192.168.10.4:80;
    Copy after login

    url_hash

    Carry out hash modulo based on the user's url, and assign the request to a specific back-end server based on the operation value.

    1. The user requests nginx load balancing, and the request is scheduled to cache1 through the url algorithm
    2. If cache1 has no data, it will obtain the data from the backend, return the data, and cache the data
    3. When When other users access the same URL, the scheduler will still schedule to the cache1 node
    4.cache1 will directly return the data to

    hash $request_uri consistent;
    server 192.168.10.3:80;
    server 192.168.10.4:80;
    Copy after login

    least_conn

    which server If the number of connections is the least, the request will be dispatched to this server

    least_conn;
    server 192.168.10.3:80;
    server 192.168.10.4:80;
    Copy after login

    Load balancing backend node status

    down

    Mark the server node as unavailable status, generally used for shutdown maintenance.

    server 192.168.10.3:80 down;
    server 192.168.10.4:80;
    Copy after login

    backup

    Standby node, this node will not be scheduled under normal circumstances; when all normal working nodes are unavailable, this node will be enabled; when the node recovers, this node The node will continue to return to standby status.

    server 192.168.10.3:80;
    server 192.168.10.4:80;
    server 192.168.10.5:80 backup;
    Copy after login

    max_conns

    is used to limit the maximum number of TCP connections received by each backend node. If the limit is exceeded, an error will be thrown.

    server 192.168.10.3:80 max_conns=10;
    server 192.168.10.4:80 max_conns=10;
    Copy after login

    One can connect 10. Two can connect 20. If it exceeds 20, an error will occur.

    keepalived

    Activate caching with the back-end server, that is, long links, to improve website throughput.
    This function is not enabled by default. When there is a request, a connection will be established, maintained, and closed, so there will be network consumption; but if all connections are cached, other system resources will be occupied when the connection is idle, so The keepalived parameter can be used.

    server 192.168.10.3:80;
    server 192.168.10.4:80;
    
    keepalived 32;   # 最大空闲连接数的个数
    keepalived_timeout 100s; # 空闲连接的超时时间
    
    # 需要配合以下两个参数使用
    
    proxy_http_version 1.1;
    proxy_set_header connection "";
    Copy after login

    max_fails and fail_timeout

    max_fails=2:服务器通信失败两次,认为服务器不可用
    fail_timeout=5s:服务器通信失败后,每5秒探测一次服务器是否恢复正常。
    在fail_timeout设定时间内,与服务器连接失败次数达到max_fails数量,则认为服务器不可用。
    如果不设置的话默认是探测一次,间隔10s。

    server 192.168.10.3:80 max_fails=2 fail_timeout=5s;
    server 192.168.10.4:80 max_fails=2 fail_timeout=5s;
    Copy after login

    The above is the detailed content of How Nginx uses ngx_http_upstream_module to implement load balancing function. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    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

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

    The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

    How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

    How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

    How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

    How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

    How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

    You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

    How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

    How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

    How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

    Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

    How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

    Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

    How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

    Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

    See all articles