nginx- study notes 8

Load balancing module

Load balancing means for the rear end of the list of hosts from the definition "upstream" instruction to select a host. nginx load balancing module to use to find a host, and then use the upstream modules interact with this host.

Configuration

To understand the load balancing module development approach, you first need to learn how to use load-balancing module.

In the configuration file, if we need to use load balancing algorithm ip hash of. We need to write a configuration similar to the following:

upstream test {
    ip_hash;

    server 192.168.0.1;
    server 192.168.0.2;
}

instruction

Configuration decisions instruction, now look ip_hash definition of instruction:

static ngx_command_t  ngx_http_upstream_ip_hash_commands[] = {

    { ngx_string("ip_hash"),
      NGX_HTTP_UPS_CONF|NGX_CONF_NOARGS,
      ngx_http_upstream_ip_hash,
      0,
      0,
      NULL },

    ngx_null_command
};

  

hook

Hook load balancing module of code is regular, here to analyze the law by ip_hash module.

static char *
ngx_http_upstream_ip_hash(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    ngx_http_upstream_srv_conf_t  *uscf;

    uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);

    uscf->peer.init_upstream = ngx_http_upstream_init_ip_hash;

    uscf->flags = NGX_HTTP_UPSTREAM_CREATE
                |NGX_HTTP_UPSTREAM_MAX_FAILS
                |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
                |NGX_HTTP_UPSTREAM_DOWN;

    return NGX_CONF_OK;
}

Set uscf-> flags

  1. NGX_HTTP_UPSTREAM_CREATE: create a logo, if you create a logo containing the words, nginx will check for duplicate is created, and the necessary parameters are filled in;
  2. NGX_HTTP_UPSTREAM_MAX_FAILS: max_fails properties may be used in the server;
  3. NGX_HTTP_UPSTREAM_FAIL_TIMEOUT: fail_timeout properties may be used in the server;
  4. NGX_HTTP_UPSTREAM_DOWN: down property may be used in the server;
  5. NGX_HTTP_UPSTREAM_WEIGHT: weight properties may be used in the server;
  6. NGX_HTTP_UPSTREAM_BACKUP: You can use backup attribute server in.

And modifications may be provided upstream {} in the "server" attribute instruction support load balancing module of the instruction handler.

Callback settings init_upstream

When nginx is initialized upstream, it calls the callback function initializes the load balancing module set in ngx_http_upstream_init_main_conf function. Here is a very good understanding of the specific location uscf. By the following diagram, illustrating the layout of the memory modules arranged upstream balanced load.

As can be seen from the figure, the configuration item MAIN_CONF ngx_upstream_module module has a pointer array upstreams, each element of the array that corresponds to the profile information of each of the upstream {}.

Initial Configuration

init_upstream callback functions execute the initial configuration load balancing module, also set a new hook, the hook function as an initialization function is called when nginx handle each request

ngx_http_upstream_init_round_robin(cf, us);
us->peer.init = ngx_http_upstream_init_ip_hash_peer;

This code is very simple: IP hash module first call another Round Robin load balancing module initialization function, and then set your own request processing phase initialization hook. In fact several load balancing module can be composed of a linked list, each time to start the process from the first module of the chain. If you decide not to deal with the module, the right treatment can be handed over to the next module in the list. Here, IP hash module Round Robin designated as its successor module load balancing module, so that in their initial configuration function module Round Robin also initialized.

Initialization request

nginx after a request is received, upstream access if found necessary, it will perform the corresponding function peer.init. This is the callback function set at initial configuration. The most important role of this function is to construct a table, you can use the current request the upstream server are successively added to this table. The need for this table, the most important reason is that if an exception occurs upstream server, when unable to provide service, you can get this table from another server retry the operation. Further, this table can also be used for balancing the computational load. The reason why this form of behavior constructed on here rather than in front of the initial configuration stage, because of the need to provide independent upstream isolation environment for each request.

In order to discuss the core peer.init, we look to achieve IP hash module:

r->upstream->peer.data = &iphp->rrp;

ngx_http_upstream_init_round_robin_peer(r, us);

r->upstream->peer.get = ngx_http_upstream_get_ip_hash_peer;

The first row is provided a data pointer that is pointing to that table mentioned above;

The second line module to the callback function is the Round Robin module initialization request. Has been mentioned before, a load-balancing module can call other load balancing module to provide additional functionality.

The third line is to set a new callback get. This function is responsible for removing a server from the table. In addition to get a callback function, there is another r-> upstream-> peer.free callback function. This function is invoked when the request is complete upstream, we are responsible to do some remedial work. For example, we need to maintain an upstream server to access the counter, you can add it 1, in free in its minus 1 in the get function. If SSL is the case, nginx also provides two callback functions peer.set_session and peer.save_session. Generally speaking, there are two entry points to achieve load balancing algorithm, one is here, and the second is a callback function to get in.

peer.get and peer.free callback function

These two functions are a function of the lowest load balancing module, responsible for the actual connection and obtaining a recovery connection a preliminary operation. The reason why is ready to operate, because these two functions, it is not actually establish a connection or connection release action, but only get the address of the connection to perform operations or maintenance of the connection status. It should be understood clearly, to obtain the address information in connection peer.get function, does not mean that the connection must not be established at this time, on the contrary, through the get function's return value, nginx can see if there is an available connection, whether the connection has been set up. These return values ​​are summarized as follows:

return value Explanation subsequent action nginx
NGX_DONE Get the connection address information, and the connection has been established. Directly connected, the transmission data.
NGX_OK Address information has been connected, but the connection is not established. Establish a connection, such as a connection can not be established immediately, set the event, to suspend the implementation of this request, the implementation of other requests.
NGX_BUSY All connections are available.

502 error is returned to the client.

There are several issues here:

Q: When the connection is established?
A: Use rear keepalive connection when the connection is not closed after the completion of use, but is stored in a queue, a new request is just removed from the queue connections that are already prepared.
Q: What is that all connections are available?
A: Initialization process the request, the establishment of a table, get from this function is responsible for each table does not duplicate out a connection, when you can not get a new connection from the table that all connections are available.
Q: For a request, peer.get function may be called multiple times it?
A: 正式如此。当某次peer.get函数得到的连接地址连接不上,或者请求对应的服务器得到异常响应,nginx会执行ngx_http_upstream_next,然后可能再次调用peer.get函数尝试别的连接。upstream整体流程如下:

 

Guess you like

Origin www.cnblogs.com/Anderson-Chaow/p/12364710.html
Recommended