Analysis of Nginx Roundrobin, keepalive and Ip_hash modules

Source: Internet
Author: User
First, starting with the following structure, he is also included in the ngx_http_upstream_srv_conf_s structure.

typedefstruct {/*typedef ngx_int_t (*ngx_http_upstream_init_pt)(ngx_conf_t*cf,ngx_http_upstream_srv_conf_t *us);*/    ngx_http_upstream_init_pt        init_upstream;    ngx_http_upstream_init_peer_pt   init;    void                            *data;                     } ngx_http_upstream_peer_t

The following analysis focuses on the next two member variables Init_upstream and init

Init_upstream

Each upsteam configuration block is executed once;
Can be configured by configuring the KeepAlive or Ip_hash function, that is, encountering keepalive or Ip_hash configuration is called, if the default implementation is not configured as Ngx_http_upstream_init_round_robin function, When the configuration initialization function ngx_http_upstream_init_main_conf is called, the default setting for the main function is 2

    1. The us->servers array gets the data initialized in the ngx_http_upstream_rr_peers_t structure of the house us->peer.data for use by the INIT function;
    2. Set the value of the INIT function to enable INIT configuration;

Init

The

init function executes once for each request to Oringin;
is called in Ngx_http_upstream_init_request to configure the R->upstream->peer struct. For the keepalive and Ip_hash functions, different get and free functions will be configured, and the default implementation is Ngx_http_upstream_create_round_robin_peer

 struct ngx_peer_connection_s {ngx_connection_t  *  connection; struct sockaddr  *  sockaddr; Socklen_t Socklen; ngx_str_t  *  name; Ngx_uint_t tries; ngx_msec_t start_time; /* type typedef ngx_int_t (*NGX_EVENT_GET_PEER_PT) (ngx_peer_connection_t *pc, void *data); */ /* Above the INIT function is configured, if KeepAlive is configured and connection is found from the cache, the ngx_done,*/will be returned  /* and set the reuse of existing connections, if not configured KeepAlive or, no connection,*/found  /* But the next hop address is found and will return ngx_ok,*/ ngx_event_get_peer_pt get; /*, also set in Init, called when the connection is released */ ngx_event_free_peer_pt free; /* is set in init to hold us->peer.data data generated in Init_upstream, used in Get and free methods */  void   *   data ;  ...   ...   ...  };

The data initialized by the INIT function is used to create the connection for the RR, and the calling process is as follows:
Ngx_http_upstream_init_request->ngx_http_upstream_connect->ngx_event_connect_peer->
rc = Pc->get (PC, Pc->data);
Round_robin, ip_hash,keepalive the relationship between modules
Both the Round_robin and Ip_hash modules are used to find the scare Hop ip,keepalive feature used to find out if there is already an available connection through the IP address, so the get implementation of the two modules is different, IP_ Hash is now used Ip_hash to find the next, after the search failed to use the default Round_robin function,
While the KeepAlive feature is available after IP lookup, IP lookup can use the Ip_hash method, or you can use the method provided by Round_robin.
As follows

Static ngx_int_t Ngx_http_upstream_init_keepalive_peer (ngx_http_request_t*R, Ngx_htt p_upstream_srv_conf_t*us                                                                                                                                                                            { ngx_http_upstream_keepalive_peer_data_t*Kp ngx_http_upstream_keepalive_srv_conf_t*Kcf Ngx_log_debug0 (Ngx_log_debug_http, R -Connection -Log,0,"Init keepalive peer"); Kcf=Ngx_http_conf_upstream_srv_conf (US,                                                                                               Ngx_http_upstream_keepalive_module); Kp=Ngx_palloc (R -Pool, sizeof (ngx_http_upstream_keepalive_peer_data_t));if(KP==NULL) {returnNgx_error; }if(KCF -Original_init_peer (R, US)!=NGX_OK) {returnNgx_error;                                                                                                                                                                        } Kp -Conf=Kcf Kp -Upstream=R -Upstream/ * Save the default Original_get_peer method, will still be called in the new Get method,r->upstream->peer.get; can be round_robin, or it can be provided by the Iphash module */ Kp -Original_get_peer=R -Upstream -Peer.Get Kp -Original_free_peer=R -Upstream -Peer.Free....../ * Save the default data that is initialized in Original_init_peer, which you need to use in the Get and free methods */Kp -Data=R -Upstream -Peer.Data;/ * KeepAlive module can be found through r->upstream->peer.data * /R -Upstream -Peer.Data=Kp/* Set a new get and free hooks */R -Upstream -Peer.Get=Ngx_http_upstream_get_keepalive_peer; R -Upstream -Peer.Free=Ngx_http_upstream_free_keepalive_peer;returnNGX_OK; } Static Ngx_int_tngx_http_upstream_get_keepalive_peer (ngx_peer_connection_t*Pcvoid*Data) {ngx_http_upstream_keepalive_peer_data_t*Kp=Data; ngx_http_upstream_keepalive_cache_t*Item    ngx_int_t RC; ngx_queue_t*Q*Cache; Ngx_connection_t*C/ * Ask Balancer This source note should be inaccurate * //* For Round_robin or Ip_hash to find the next hop address */Rc=Kp -Original_get_peer (PC, KP -Data);if(RC!=NGX_OK) {returnRc }/ * Search cache for suitable connection * /Cache=&Kp -Conf -Cache;

You can see that keepalive is similar to the adorner model, which adds a keepalive function on the next hop IP, keepalive a function pointer that contains the origin implementation
Ip_hash and Round_robin can be similar to the policy model, which is to set different policies for request lookup IP in Init_upstream.
The overall relationship of these three modules, similar to the relationship between builders, must be implemented and sequentially called Init_upstream,init,get,free functions.

Postscript:
Round_robin provides a CREATE function for situations where resolved members are set in upstream, for example, by
Proxy_pass http://$vhost; the way to the address of origin is set. At this point, Init is replaced by create and cannot have keepalive and Ip_hash functions. Both KeepAlive and Iphash can be used only in upstream modules.

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the Nginx Roundrobin, keepalive, Ip_hash module analysis, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.