The load balancing module is used to select a host from the list of backend hosts defined by the "upstream" command. Nginx first finds a host using the Server Load balancer module, and then uses the upstream module to interact with this host. To facilitate the introduction of the Server Load balancer module, the following uses the built-in nginx iPhone ash module as an example for analysis.
Server Load balancer module (100%)
The load balancing module is used to select a host from the list of backend hosts defined by the "upstream" command. Nginx first finds a host using the Server Load balancer module, and then uses the upstream module to interact with this host. To facilitate the introduction of the Server Load balancer module, the following describes how to use the built-in ip hash module of nginx as an example.
Configuration
To understand the development method of the Server Load balancer module, you must first understand how to use the Server Load balancer module. Because the load balancing module differs greatly from the modules mentioned in the previous document, it is easier to understand the configuration.
In the configuration file, if we need to use the ip hash load balancing algorithm. We need to write a configuration similar to the following:
upstream test { ip_hash; server 192.168.0.1; server 192.168.0.2;}
From the configuration, we can see the application scenarios of the Server Load balancer module: 1. The core command "ip_hash" can only be used in upstream. This command is used to notify nginx to use the ip hash load balancing algorithm. If this command is not added, nginx uses the default round robin Load Balancing module. Do you have something in common with the handler module configuration? 2. the commands in upstream {} may appear before the "server" command, after the "server" command, or between the two "server" commands. Readers may have questions. Is there any difference? Please try the following Configuration:
upstream test { server 192.168.0.1 weight=5; ip_hash; server 192.168.0.2 weight=7;}
The magic thing is:
nginx: [emerg] invalid parameter "weight=7" in nginx.conf:103configuration file nginx.conf test failed
It can be seen that the ip_hash command does affect the resolution of the configuration.