Preface:

Modern high-traffic web sites must handle hundreds of thousands (if not millions) of concurrent requests from users or clients in a fast and reliable manner and return the correct text, images, video, or application data. To scale economically and efficiently to accommodate these high volumes, modern computing best practices often require adding more servers.

A load balancer acts as a “traffic cop” sitting in front of your server, routing client requests across all servers that can perform to maximize speed and capacity utilization, ensuring that no one server is overworked in a way that could degrade the performance of these requests. If a single server fails, the load balancer redirects traffic to the remaining online servers. When a new server is added to the server group, the load balancer automatically starts sending requests to it.

What is load balancing?

Load balancing is the efficient distribution of incoming network traffic among a group of back-end servers (also known as server farms or server pools).

What is Nginx load balancing

From the simple concepts introduced above, you probably have a rudimentary idea of load balancing as a scheduling strategy. So what is Nginx? How does Nginx implement load balancing? This starts with forward and reverse proxies.

Forward agent (forward proxy) : is a is located in the client and server (proxy server), between the target server to get content from the target server, the client sends a request to the proxy server and specify the target, then the proxy server to the target server transfer request and will get back to the content of the client.

For example, to visit Google in China, direct access is not possible. We can use a forward proxy server that can access Google to send requests to the proxy server, and the proxy will go to Google to obtain data and return it. In this way, the demand for accessing Google can be realized in a disguised way.

In a word: forward proxy, the proxy server proxies the client to interact with the target server.

Forward proxy uses:

  • The IP address access restriction is exceeded
  • Caching speeds up access to resources
  • Hide the real IP address of the client
  • Client access authorization

Reverse Proxy: In Reverse Proxy, the Proxy server represents the server. The proxy server receives the request from the client, forwards the request to the server on the Intranet, and returns the result to the client. In this case, the proxy server acts as a reverse proxy server.

The reverse proxy server is located between the user and the target server. However, for users, the reverse proxy server is the target server. Users can access the reverse proxy server directly to obtain the resources of the target server. At the same time, users do not need to know the address of the target server, nor do they need to make any Settings on the client side.

In a word: reverse proxy, a proxy server proxies the target server to interact with the client.

Reverse proxy uses:

  • Hide the real SERVER IP address
  • Load balancing
  • Caching speeds up access to resources
  • Provide security

conclusion

Forward proxy, where the proxy server proxies the client to interact with the target server. Real-life scenario: scalpers buy tickets.

Reverse proxy, where the proxy server proxies the target server to interact with the client. A real-life scenario: a real estate agent.

Nginx load balancing components

Nginx HTTP function module The module specification
ngx_http_proxy_module Proxy Proxy module for passing requests to server nodes or upstream server pools.
ngx_http_upstream_module The load balancing module can realize the load balancing function of the website and the health check of nodes.

The load balancing function of Nginx relies on ngx_HTTP_upstream_module and supports proxy_pass, fastcgi_pass, memcached_pass, etc.

Upstream www_server_pool {server 10.0.0.7 weight=5; Server 10.0.0.16 weight = 10; }Copy the code

Parameter description:

Upstream is a mandatory keyword. The www_server_pool is the name of an upstream group.

2. Server is a fixed keyword, which can be followed by the domain name or IP address. If no port is specified, the default value is 80. There’s a semicolon at the end.

3. Weight stands for weight, and the higher the value, the more requests are allocated.

Nginx common load balancing algorithm

polling

The principle is that each request is sent to different application servers one by one in chronological order. If the application server that receives the request fails and the maximum number of failed requests exceeds max_fails (one time), the request will not be forwarded to this node within fail_timeout (10 seconds)

Upstream defaultReverseProxyServer {server 192.168.0.1:8080; Server 192.168.0.2:8080; }Copy the code

The weight

The principle is that each request is distributed to different application servers according to the weight. Similarly, if the application server receiving the request fails and the maximum number of requests fails max_fails (1 by default or N times can be set), fail_TIMEOUT (10 seconds by default, N seconds can be set), Requests are no longer forwarded to this node ~

Upstream weightReverseProxyServer{server 192.168.0.1:8080 weight=10 max_fails=2 fail_timeout=5s; Server 192.168.0.2:8080 weight=5 max_fails=2 fail_timeout=5s; }Copy the code

ip_hash

The principle is that each request is allocated according to the hash result of the user’s access IP address. If the request comes from the same user IP address, the IP address is fixed to access an application server. This algorithm can effectively solve the session sharing problem in dynamic web pages.

upstream ipHashReverseProxyServer{
    ip_hash;
    server 192.168.0.1:8080;
    server 192.168.0.2:8080;
}
Copy the code

Commonly used algorithm is based on weight, because many cases are now cluster deployment, and most of the resources for each server cluster is uneven, resources, distribution of high weight, resources, distribution of low weight lower, this is using the load balancing algorithm based on weight, can be more efficient use of resources and improve the ability of handling concurrency ~