introduce

Load balancing across multiple application instances is a common technique for optimizing resource utilization, maximizing throughput, reducing latency, and ensuring fault-tolerant configurations. You can use Nginx as a very effective HTTP load balancer to distribute traffic to multiple application servers, and use Nginx to improve the performance, scalability, and reliability of Web applications.

Load balancing

What is load balancing? Literally speaking, N servers share the load evenly, so that no server is idle due to the high load breakdown of a server. The premise of load balancing is to have multiple servers to achieve the high performance and high availability of the whole system.

Nginx load balancing introduction

Strictly speaking, Nginx is only used as an Nginx Proxy reverse Proxy, because the effect of this reverse Proxy function is load balancing cluster effect, so we call it Nginx load balancing.

Nginx load balancing component

There are two main components to implement Nginx load balancing:

  • ngx_http_upstream_module

The load balancing module can realize the load balancing function of the website and the health check of nodes

  • ngx_http_proxy_module

Proxy proxy module used to forward requests to server nodes or upstream server pools

Http_upstream module

Upstream module introduction

The upstream module allows Nginx to define one or more groups of node server groups, using the proxy_pass agent to route web site requests to the name of the predefined upstream group as follows:

proxy_pass http://server_pool_name
Copy the code

Server_pool_name is the name of an upstream node server group.

Upstream standard with
Upstream server_pool_name {server 192.168.0.2; Server 192.168.0.3:80 weight=1 max_fails=1 fail_timeout=10s; # This line is equivalent to the previous line. The extra part of this line is the default configuration. Server 192.168.0.4:80 weight=1 max_fails=2 fail_timeout=20s backup; Add a number of parameters to the end of the server.Copy the code
Upstream module parameters
  • server

The RS configuration behind the load can be IP or domain name.

  • weight

The weight of the request server. The default value is 1. A larger value means a larger percentage of requests are accepted.

  • max_fails

The number of failed nginx attempts to connect to the back-end host. This value is used in conjunction with proxy_net_upstream, fastcgi_next_upstream, and memcached_next_upstream. When nginx receives a status code from the backend server defined by these three parameters, it forwards the request to a working backend server, such as 404,502,503

  • fail_timeout

After the number of failures defined by max_fails, the interval between the next check is 10s by default

  • backup

Hot backup configuration, which indicates that this server acts as a backup server and forwards requests to it if the primary servers all go down

  • down

Indicates that the server is never available and can be used with ip_hash

  • For example,
Upstream {server 192.168.0.3:80 weight=5; Server 192.168.0.4:80 max_fail = 5 fail_timeout = 10 s. // If five consecutive checks fail, check again 10 seconds later. 80 backup server 192.168.0.5:; // Specify a backup server. Action: Forwards requests to the server when all of the above servers are inaccessible. }Copy the code

Http_proxy module

Proxy_pass directive introduction

The proxy_pass directive belongs to the ngx_HTTP_proxy_module module, which can forward requests to another server. In the actual reverse proxy work, the location function matches the specified URI, and then the received request matching the URI is thrown to the defined upstream node pool via proxy_pass. The specific configuration and use of the location directive will be introduced in the subsequent article. Please look forward to it.

        location / {
            proxy_pass http://upstream_name;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
Copy the code
HTTP Proxy module parameters
  • proxy_set_header

Set the HTTP request header to the back-end server node. For example, it is possible to have the server node on the proxy back end get the real IP address of the user accessing the client

  • client_body_buffer_size

Used to specify the client request topic buffer size

  • proxy_connect_timeout

Indicates the timeout time for the connection between the reverse proxy and the back-end node server, that is, the timeout time for initiating a handshake and waiting for a response

  • proxy_send_timeout

Indicates the data return time of the proxy backend server, that is, within the specified time, the backend server must send all data, otherwise, nginx will disconnect the connection

  • proxy_read_timeout

Set the time it takes for nginx to get information from the proxy’s backend server. This is how long it takes nginx to wait for the backend server to respond to a successful connection

  • proxy_buffer_size

Sets the buffer size, which by default is equal to the size of the proxy_buffers directive

  • proxy_buffers

Set the number and size of buffers into which nginx will place response information received from the proxy’s back-end server

  • proxy_busy_buffers_size

This parameter sets the size of proxy_buffers that can be used when the system is very busy. The recommended size is proxy_bufer*2

  • proxy_temp_file_write_size

Specifies the size of the proxy cache temporary file

Nginx load balancing scheduling algorithm

Nginx load balancing is implemented through the upstream module, which implements three built-in load policies and is relatively simple to configure. Official website load balancing configuration description: nginx.org/en/docs/htt…

Round – robin (polling)

The default scheduling algorithm assigns servers to different back-end servers one by one based on client requests. The faulty servers are automatically removed from the node server pool.

upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }
Copy the code

Note: For clusters with different server performance, this algorithm may cause problems such as unreasonable resource allocation.

Least-connected

The connection minimum allows for more equitable control of the load on an application instance in cases where some requests take longer to complete. Using load balancing with the least number of connections, Nginx will try not to overload busy application servers with too many requests and instead allocate new requests to less busy servers. You can use Nginx as a very effective HTTP load balancer to distribute traffic to multiple application servers, and use nginx to improve the performance, scalability, and reliability of Web applications

upstream myapp1 {
        least_conn;
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }
Copy the code

ip-hash

Each request is allocated according to the hash result of the access IP address. Each visitor regularly accesses one back-end server, which can solve the problem that sessions are not shared.

upstream myapp1 {
    ip_hash;
    server srv1.example.com;
    server srv2.example.com;
    server srv3.example.com;
}
Copy the code

reference

  • Nginx.org/en/docs/htt…
  • Juejin. Cn/post / 684490…

conclusion

Welcome to wechat public account “Code zonE”, focusing on sharing Java, cloud computing related content, including SpringBoot, SpringCloud, microservices, Docker, Kubernetes, Python and other related technology dry goods, looking forward to meeting you!