search
HomeOperation and MaintenanceNginxHow Nginx uses ngx_http_upstream_module to implement load balancing function

    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;
        }
    }

    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;
        }
    }

    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;

    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;

    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;

    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;

    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;

    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;

    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;

    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;

    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 "";

    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;

    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
    This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
    The Advantages of NGINX: Speed, Efficiency, and ControlThe Advantages of NGINX: Speed, Efficiency, and ControlMay 12, 2025 am 12:13 AM

    The reason why NGINX is popular is its advantages in speed, efficiency and control. 1) Speed: Adopt asynchronous and non-blocking processing, supports high concurrent connections, and has strong static file service capabilities. 2) Efficiency: Low memory usage and powerful load balancing function. 3) Control: Through flexible configuration file management behavior, modular design facilitates expansion.

    NGINX vs. Apache: Community, Support, and ResourcesNGINX vs. Apache: Community, Support, and ResourcesMay 11, 2025 am 12:19 AM

    The differences between NGINX and Apache in terms of community, support and resources are as follows: 1. Although the NGINX community is small, it is active and professional, and official support provides advanced features and professional services through NGINXPlus. 2.Apache has a huge and active community, and official support is mainly provided through rich documentation and community resources.

    NGINX Unit: An Introduction to the Application ServerNGINX Unit: An Introduction to the Application ServerMay 10, 2025 am 12:17 AM

    NGINXUnit is an open source application server that supports a variety of programming languages ​​and frameworks, such as Python, PHP, Java, Go, etc. 1. It supports dynamic configuration and can adjust application configuration without restarting the server. 2.NGINXUnit supports multi-language applications, simplifying the management of multi-language environments. 3. With configuration files, you can easily deploy and manage applications, such as running Python and PHP applications. 4. It also supports advanced configurations such as routing and load balancing to help manage and scale applications.

    Using NGINX: Optimizing Website Performance and ReliabilityUsing NGINX: Optimizing Website Performance and ReliabilityMay 09, 2025 am 12:19 AM

    NGINX can improve website performance and reliability by: 1. Process static content as a web server; 2. forward requests as a reverse proxy server; 3. allocate requests as a load balancer; 4. Reduce backend pressure as a cache server. NGINX can significantly improve website performance through configuration optimizations such as enabling Gzip compression and adjusting connection pooling.

    NGINX's Purpose: Serving Web Content and MoreNGINX's Purpose: Serving Web Content and MoreMay 08, 2025 am 12:07 AM

    NGINXserveswebcontentandactsasareverseproxy,loadbalancer,andmore.1)ItefficientlyservesstaticcontentlikeHTMLandimages.2)Itfunctionsasareverseproxyandloadbalancer,distributingtrafficacrossservers.3)NGINXenhancesperformancethroughcaching.4)Itofferssecur

    NGINX Unit: Streamlining Application DeploymentNGINX Unit: Streamlining Application DeploymentMay 07, 2025 am 12:08 AM

    NGINXUnit simplifies application deployment with dynamic configuration and multilingual support. 1) Dynamic configuration can be modified without restarting the server. 2) Supports multiple programming languages, such as Python, PHP, and Java. 3) Adopt asynchronous non-blocking I/O model to improve high concurrency processing performance.

    NGINX's Impact: Web Servers and BeyondNGINX's Impact: Web Servers and BeyondMay 06, 2025 am 12:05 AM

    NGINX initially solved the C10K problem and has now developed into an all-rounder who handles load balancing, reverse proxying and API gateways. 1) It is well-known for event-driven and non-blocking architectures and is suitable for high concurrency. 2) NGINX can be used as an HTTP and reverse proxy server, supporting IMAP/POP3. 3) Its working principle is based on event-driven and asynchronous I/O models, improving performance. 4) Basic usage includes configuring virtual hosts and load balancing, and advanced usage involves complex load balancing and caching strategies. 5) Common errors include configuration syntax errors and permission issues, and debugging skills include using nginx-t command and stub_status module. 6) Performance optimization suggestions include adjusting worker parameters, using gzip compression and

    Nginx Troubleshooting: Diagnosing and Resolving Common ErrorsNginx Troubleshooting: Diagnosing and Resolving Common ErrorsMay 05, 2025 am 12:09 AM

    Diagnosis and solutions for common errors of Nginx include: 1. View log files, 2. Adjust configuration files, 3. Optimize performance. By analyzing logs, adjusting timeout settings and optimizing cache and load balancing, errors such as 404, 502, 504 can be effectively resolved to improve website stability and performance.

    See all articles

    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 Article

    Hot Tools

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software