nginx load balancing strategy (six kinds)

I, on the Nginx load balancing

  In a server cluster, Nginx play the role of a proxy server (i.e., reverse proxy), in order to avoid excessive pressure on a single server, forwarding the request from the user to a different server.

 

Two, Nginx load balancing strategy

  Load balancing a list of servers from a rear end defined "upstream" module select one server accepts the user's request. A basic upstream module like this, server module is in the server list:

begging. A basic upstream module like this, server module is in the server list:

 

    Dynamic Server Group # 
    upstream dynamic_zuoyu { 
        Server localhost: 8080; #tomcat 7.0 
        Server localhost: 8081; #tomcat 8.0 
        Server localhost: 8082; #tomcat 8.5 
        Server localhost: 8083; #tomcat 9.0 
    }

 

  After the upstream module configuration is complete, to make the specified access to the reverse proxy server list:

 

        # Other pages reverse proxy to tomcat container 
        LOCATION ~ * $ {. 
            Index index.jsp index.html; 
            proxy_pass HTTP: // dynamic_zuoyu; 
        }
        

  This is the most basic load balancing instance, but not enough to meet the actual needs; currently Nginx server upstream module supports six ways of distribution:

Load balancing strategy
polling Default
weight Weight way
ip_hash Ip based distribution
least_conn Minimum Connection
fair (third party) Response time way
url_hash (third party) URL based distribution

  Here, the only detail that comes with Nginx load balancing strategy, a third party not much description.

1, polling

  The most basic configuration, the above example is polling way, it is the default load balancing upstream module default policy. Each request will be individually assigned to different backend servers chronologically.

  It has the following parameters:

fail_timeout Used in conjunction with max_fails.
max_fails

Sets the maximum number of failures within the time fail_timeout parameter set, if within this time, all requests for that server fail, it is considered that the server is considered to be shut down,

fail_time The length of time the server will be considered downtime, the default is 10s.
backup Mark the server is on standby. When the primary server is stopped, the request will be sent to it here.
down Permanent marker server shut down.

  note:

  • In the poll, if the server is down, the server will be automatically rejected.
  • The default configuration is polling policies.
  • This policy is quite suitable for server configuration, stateless and fast track service usage.

2、weight

  Weight manner, on the basis of the specified polling on polling strategies chance. Examples are as follows:

    #动态服务器组
    upstream dynamic_zuoyu {
        server localhost:8080   weight=2;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082   backup;  #tomcat 8.5
        server localhost:8083   max_fails=3 fail_timeout=20s;  #tomcat 9.0
    }

  In this example, the probability weight parameter specifies the polling, the default weight value of 1,; weight ratio proportional to the value of the access, such as Tomcat 7.0 is twice the probability of being accessed other servers.

  note:

  • The higher the weight assigned to the request requires more processing.
  • This policy can be combined with least_conn and ip_hash.
  • Hardware configuration differences were larger cases, this strategy is more suitable server.

3、ip_hash

  Load balancer according to the specified allocation method based on the client IP, this method ensures that the same client request has been sent to the same server, in order to ensure session session. Visitors are fixed so that each access a back-end server, can not solve the problem session across servers.

Dynamic Server Group # 
    upstream dynamic_zuoyu { 
        ip_hash; # ensure that each guest access to a fixed back-end server 
        Server localhost: 8080 weight = 2; #tomcat 7.0 
        Server localhost: 8081; #tomcat 8.0 
        Server localhost: 8082; #tomcat 8.5 
        Server localhost: = =. 3 fail_timeout max_fails 8083 20S; #tomcat 9.0 
    }

  note:

  • Before nginx version 1.3.1, can not be used in ip_hash weight (weight).
  • ip_hash not be used with backup.
  • This policy suitable for state services, such as session.
  • When the server needs to have removed, you must manually goes down.

4、least_conn

  Forwards the request to the back-end server fewer connections. Polling algorithm is the average of the request is forwarded to the back-end, so that their load is substantially the same; however, some request takes a long time, it will result in higher in the back-end load. In this case, least_conn this way we can achieve better load balancing effect.

    Dynamic Server Group # 
    upstream dynamic_zuoyu { 
        least_conn; # forwards the request to the back-end server fewer connections 
        Server localhost: 8080 weight = 2; #tomcat 7.0 
        Server localhost: 8081; #tomcat 8.0 
        Server localhost: 8082 Backup; #tomcat 8.5 
        localhost Server: fail_timeout. 3 = 8083 = max_fails 20S; #tomcat 9.0 
    }

  note:

  • This load balancing policy for request processing of varying duration cause the server overload.

5, third-party policy

  Load balancing policies of third-party plug-ins need to install a third-party.

①fair

  According to the response time of the server allocation request, allocating a short response time priority.

    Dynamic Server Group # 
    upstream dynamic_zuoyu { 
        Server localhost: 8080; #tomcat 7.0 
        Server localhost: 8081; #tomcat 8.0 
        Server localhost: 8082; #tomcat 8.5 
        Server localhost: 8083; #tomcat 9.0 
        Fair; # achieve short response time priority assigned 
    }

②url_hash

  Press hash to url to access the results of the allocation request, so that each url directed to the same back-end server, to be used with the cache hit. A resource with multiple requests may arrive on different servers, leading to unnecessary multiple downloads, the cache hit rate is not high, and some waste of resources of time. The use url_hash, you can make the same url (that is, with a request for resources) will reach the same server, once lived cache resources, then this request is received, it can be read from the cache. 

    Dynamic Server Group # 
    upstream dynamic_zuoyu { 
        the hash $ REQUEST_URI; url # implement each directed to the same back-end server 
        Server localhost: 8080; #tomcat 7.0 
        Server localhost: 8081; #tomcat 8.0 
        Server localhost: 8082; #tomcat 8.5 
        Server localhost: 8083; #tomcat 9.0 
    }

Third, the summary

  The above is six kinds of load balancing strategy implementation, which in addition to polling and polling weights, the Nginx are different according to the algorithm. In practice, we require the use of selective according to different scenarios, mostly in combination with a variety of strategies to meet the actual demand.

Guess you like

Origin blog.csdn.net/qq_40585396/article/details/93377924