Nginx load balancing-Overall architecture

Source: Internet
Author: User
Tags nginx load balancing

Nginx load balancing-Overall architecture

Nginx Version: 1.9.1

My blog: http://blog.csdn.net/zhangskd

The Load Balancer module currently available in Nginx:

Ngx_http_upstream_round_robin, weighted polling, an equalization request, is the default HTTP load balancing algorithm, integrated in the framework.

Ngx_http_upstream_ip_hash_module,ip Hash, which preserves the session.

Ngx_http_upstream_least_conn_module, the minimum number of connections, can be evenly connected.

Ngx_http_upstream_hash_module, consistent hashing, reduces the invalidation of cached data.

We know that the polling algorithm forwards the requests on average to each backend, so that their load is roughly the same. The premise is that each request takes up a similar back-end time, and if some requests take a long time, it can result in a higher back-end load. In this scenario, the request is forwarded to the backend with a few connections, which can achieve better load balancing effect, which is the least_conn algorithm.

The least_conn algorithm is simple, it is preferred to traverse the backend cluster, compare the conns/weight of each back end, and select the backend with the lowest value. If there are multiple back-end conns/weight values with the same minimum, then the weighted polling algorithm is used for them.



What is load balancing

We know that the performance of a single server is capped, when the traffic is very large, you need to use multiple servers to provide services together, this is called the cluster.

A load balancer server is used to distribute traffic through it, in a way, to each server in the cluster. This will not only bear

Greater traffic, reduced service latency, and a single point of failure can prevent service unavailability. General reverse proxy Server, all have the function of load balancing.

The load balancing function can be provided by hardware, such as the previous F5 device. Can also be provided by software, LVS can provide four-layer load balancing (using IP and port),

Haproxy and Nginx can provide seven-layer load balancing (leveraging application-level information).

Look at one of the simplest nginx load balancer configurations.

[HTML]View Plain Copy
  1. HTTP {
  2. Upstream Cluster {
  3. Server SRV1;
  4. Server srv2;
  5. Server srv3;
  6. }
  7. server {
  8. Listen 80;
  9. Location/{
  10. Proxy_pass Http://cluster;
  11. }
  12. }
  13. }

With the above configuration, Nginx will be used as an HTTP reverse proxy, the HTTP request to access the native computer, to the back-end cluster of 3 servers.

The HTTP reverse proxy module used at this time is ngx_http_proxy_module.

Generally in the upstream configuration block to indicate the use of load balancing algorithm, such as hash, Ip_hash, Least_conn.

This is not specified, so the default HTTP load balancing algorithm-weighted polling is used.

Load Balancer Flowchart

Before describing the specific implementation of the Load Balancer module, take a look at its approximate process:

Load Balancer Module

The Load Balancer module currently available in Nginx:

Ngx_http_upstream_round_robin, weighted polling, an equalization request, is the default HTTP load balancing algorithm, integrated in the framework.

Ngx_http_upstream_ip_hash_module,ip Hash, which preserves the session.

Ngx_http_upstream_least_conn_module, the minimum number of connections, can be evenly connected.

Ngx_http_upstream_hash_module, consistent hashing, reduces the invalidation of cached data.

The above load balancing module implementation, basically follow a set of similar processes.

1. Analytic functions of instructions

such as Least_conn, ip_hash, hash instruction analytic function.

These functions are called when parsing a configuration file and are used primarily for:

Check the legality of directive parameters

Specifies the value of the peer.init_upstream function pointer, which is used to initialize the upstream block.

2. Initialize the upstream block

After executing the parse function of the instruction, the init main conf function of all HTTP modules is immediately called.

When the init main conf function of Ngx_http_upstream_module is executed, all the upstream blocks of the initialization function are called,

The peer.init_upstream specified in the first step is used primarily for:

Create and initialize back-end clusters, saving data for the upstream block

Specifies Peer.init, which is used to initialize the requested load balancing data

Look at the Ngx_http_upstream_module.

[Java]View Plain Copy
    1. ngx_http_module_t Ngx_http_upstream_module_ctx = {
    2. ...
    3. ngx_http_upstream_init_main_conf,/ * init main configuration * /
    4. ...
    5. };
[Java]View Plain Copy
  1. Static char *ngx_http_upstream_init_main_conf (ngx_conf_t *cf, void *conf)
  2. {
  3. ...
  4. / * The element type of the array is ngx_http_upstream_srv_conf_t * /
  5. For (i = 0; i < umcf->upstreams.nelts; i++) {
  6. / * If you do not specify an initialization function for the upstream block, the default is to use round robin's * /
  7. init = Uscfp[i]->peer.init_upstream? Uscfp[i]->peer.init_upstream:
  8. Ngx_http_upstream_init_round_robin;
  9. if (init (cf, uscfp[i]! = NGX_OK) {
  10. return ngx_conf_error;
  11. }
  12. }
  13. ...
  14. }

3. Load-balanced data blocks for initializing requests

When a request is received, the general use of the reverse proxy module (upstream module) is Ngx_http_proxy_module,

The processing function of its ngx_http_content_phase stage is Ngx_http_proxy_handler, in the initialization of the upstream mechanism

function Ngx_http_upstream_init_request, call the Peer.init specified in the second step, mainly for:

To create and initialize a load-balanced block of data for the request

Specify r->upstream->peer.getfor selecting a back-end server from the cluster (this is our most concern)

Specify r->upstream->peer.freeto update the data (whether successful or unsuccessful) when the backend is not used

In a load-balanced block of requests, a member typically points to the data of the corresponding upstream block, and in addition to its own unique members.

"The peer initialization function is called once per request.
It sets up a data structure that the module would use as it tries to find a appropriate
Backend server to service, that request; This structure is persistent across backend re-tries,
So it's a convenient place to keep track of the number of connection failures, or a computed
Hash value. By convention, the this struct is called ngx_http_upstream_<module_name>_peer_data_t. "

4. Select a back-end server

The general upstream block will have more than one backend, then for this request, to select which back-end?

The function that R->upstream->peer.get points to in the third step comes in handy:

Use a specific algorithm, such as a weighted poll or a consistent hash, to select a backend from the cluster to process the request.

The address of the selected backend is saved in pc->sockaddr,pc for active connection.

The return value of the function:

Ngx_done: A backend is selected, and the connection to that backend has been established. The request is then sent directly.

NGX_OK: A backend is selected, and the connection to the back end is not yet established. The connection is then established with the backend.

Ngx_busy: All backend (including backup clusters) are not available. The client is then sent a 502 (bad Gateway).

5. Release a back-end server

When you no longer use a back end, you need to do the finishing touches, such as the number of statistics failures.

The function that R->upstream->peer.free points to in the third step is called.

Value of the function parameter state:

0, request is processed successfully

Ngx_peer_failed, Connection failed

Ngx_peer_next, the connection failed, or the connection succeeded but the backend failed to process the request successfully

A request allows the number of backend attempts to be pc->tries, as specified in the third step. When state is the latter two values:

If Pc->tries is not 0, you need to re-select a backend, continue with the attempt, and then repeat the call to R->upstream->peer.get.

If the pc->tries is 0, it will no longer attempt to return a 502 error code (bad Gateway) to the client.

Callbacks in the upstream module

The function of the Load Balancer module is to select a back-end server from the backend cluster, and the specific reverse proxy function is implemented by the upstream module.

For example, to establish a connection with the back-end server, send requests to the back-end server, and handle the backend server response.

Let's take a look at some of the hook functions provided by the Load Balancer module, where the upstream module is callback.

The HTTP reverse proxy module of Nginx is Ngx_http_proxy_module, and its ngx_http_content_phase stage processing function is

Ngx_http_proxy_handler, the upstream mechanism for each request starts here.

[Java]View Plain Copy
  1. Ngx_http_proxy_handler
  2. Ngx_http_upstream_create/ * Create a upstream instance of the request * /
  3. Ngx_http_upstream_init/ * start upstream mechanism * /
  4. Ngx_htp_upstream_init_request/ * Access to load Balancer module * /
  5. Uscf->peer.init (R, USCF)/ * Step three, initialize the load Balancer data block for the request * /
  6. ...
  7. Ngx_http_upstream_connect/ * may be called repeatedly by Ngx_http_upstream_next * /
  8. Ngx_event_connect_peer (&u->peer); / * Connect back end * /
  9. Pc->get (PC, Pc->data); / * Fourth step, select a backend from the cluster * /
  10. ...
  11. /* And back end built after success */
  12. c = u->peer.connection;
  13. C->data = R;
  14. C->write->handler = Ngx_http_upstream_handler; / * Read event handler for registered connection * /
  15. C->read->handler = Ngx_http_upstream_handler; / * Registered Connection's Write event handler * /
  16. U->write_event_handler = Ngx_http_upstream_send_request_handler; / * Real processing function for write events * /
  17. U->read_event_handler = Ngx_http_upstream_process_header; / * Real processing function for read events * /

After the backend is selected, if an error occurs during communication with the backend, Ngx_http_upstream_next is called to continue to try the other backend.

[Java]View Plain Copy
  1. Ngx_http_upstream_next
  2. if (u->peer.sockaddr) {
  3. if (Ft_type = = ngx_http_upstream_ft_http_403 | |
  4. Ft_type = = ngx_http_upstream_ft_http_404)
  5. state = Ngx_peer_next;
  6. Else
  7. state = ngx_peer_failed;
  8. / * Fifth step, release the backend server * /
  9. U->peer.free (&u->peer, U->peer.data, state);
  10. U->PEER.SOCKADDR = NULL;
  11. }

Nginx load balancing-Overall architecture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.