Nginx builds load environment

Nginx builds load environment

 

         Nginx 's load balancing supports 4 algorithms, round-robin , least-connected , ip-hash , and weightd .

 

round-robin

         round-robin means round robin. The simplest load balancing configuration for Nginx is as follows:

http {

    upstream app1 {

        server 10.10.10.1;

        server 10.10.10.2;

    }

 

    server {

        listen 80;

 

        location / {

            proxy_pass http://app1;

        }

    }

}

 

         upstream app1 is used to specify a server group, the name of the group is app1 , including two servers. When specifying the servers included in the server group, it is specified in the form of " server ip/domain : port ", where port 80 can be ignored. Then when a request is received, the corresponding request is forwarded to the group app1 through " proxy_pass http://app1 " . The default load balancing algorithm of Nginx is circular polling . In the above configuration, we use circular polling, which will circularly distribute received requests to its included (currently available) servers. When using the above configuration, Nginx will send the first request to 10.10.10.1 , the second request to 10.10.10.2 , the third request to 10.10.10.1 , and so on.

 

least-connected

         The Chinese translation of the least-connected algorithm is the least connection, that is, the server with the least number of connections is found to forward the request every time. For example , there are two servers in the Nginx load, A and B. When Nginx receives a request, the number of requests being processed by A is 10 , and the number of requests being processed by B is 20 , then Nginx will hand over the current request to A for processing . To enable the least connection load algorithm just add " least_conn " when defining the server group , like:

    upstream app1 {

                   least_conn;

        server 10.10.10.1;

        server 10.10.10.2;

    }

 

ip-hash

         The ip-hash algorithm determines who the current request should be given to based on the requested client IP address. When using the ip-hash algorithm, Nginx will ensure that requests from the same client are distributed to the same server. To use the ip-hash algorithm, you only need to add the " ip-hash   " directive when defining the server group , such as:

    upstream app1 {

                   ip_hash;

        server 10.10.10.1;

        server 10.10.10.2;

    }

 

weighted

         The weighted algorithm, also known as the weighting algorithm, will distribute requests according to the weight of each service. Requests with a large weight will be distributed a little more, and requests with a small weight will be distributed a little less. This is typically used when the performance of multiple servers is inconsistent. When you need to use the weight algorithm, you only need to specify the parameter weight after the server when defining the server group , such as:

    upstream app1 {

        server 10.10.10.1 weight=3;

        server 10.10.10.2;

    }

 

         When configured as above , about every 4 requests received by Nginx , 3 requests will be handed over to the 10.10.10.1 server for processing, and 1 request will be handed over to 10.10.10.2 for processing.

 

         In addition to these directives, Nginx payload can also specify other directives, such as backup , down , etc. For details, please refer to http://nginx.org/en/docs/http/ngx_http_upstream_module.html .

         The directives that Nginx can specify can be found at http://nginx.org/en/docs/http/ngx_http_proxy_module.html .

 

References

http://nginx.org/en/docs/http/load_balancing.html

http://nginx.org/en/docs/http/ngx_http_upstream_module.html

http://nginx.org/en/docs/http/ngx_http_proxy_module.html

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326944424&siteId=291194637