What is it like for nginx to configure http load balancing
The following brings you how nginx configures http load balancer, hoping to give you some help in practical application. Load balancing involves a lot of things, and there are not many theories. There are many books on the Internet. Today, we will use the accumulated experience in the industry to do an answer.
According to the official documentation, the example shown below is given first.
Http {
Upstream myapp1 {
Server srv1.example.com
Server srv2.example.com
Server srv3.example.com
}
Server {
Listen 80
Location / {
Proxy_pass http://myapp1;
}
}
}
This example is to configure a http load balancer cloud server group named myapp1.
That is, the basic configuration format and syntax of http load balancing
Nginx load balancing includes three mechanisms: rotational training (round-robin), least connection (least-connected) and IP hash (ip-hash).
Rotational training: http requests sent to the server are rotated to the server in the load balancer configuration
Minimum connections: http requests sent to the server side are assigned to the server with the least number of active links in the load balancing configuration
IP hash: uses a hash method based on the client's IP address to determine which server is assigned to the request
The configuration method is as follows:
Least connection load balancer
Upstream myapp1 {
Least_conn
Server srv1.example.com
Server srv2.example.com
Server srv3.example.com
}
Ip hash
Upstream myapp1 {
Ip_hash
Server srv1.example.com
Server srv2.example.com
Server srv3.example.com
}
Rotational training
Upstream myapp1 {
Server srv1.example.com weight=3
Server srv2.example.com
Server srv3.example.com
}
Explanation: weight is weighted and defaults to 1. If there are five requests, the first three requests will be assigned to srv1, the fourth request to srv2, and the fifth request to srv3, and so on.
Note:
1. Default is rotational training.
2, the minimum number of connections and rotation mechanism will assign client requests to different servers during the client-server session, while ip hashes can maintain the session and keep the requests on the same server all the time
After reading the above about how nginx configures http load balancer, if there is anything else you need to know, you can find out what you are interested in in the industry information or find our professional and technical engineers for answers. Technical engineers have more than ten years of experience in the industry.