Nginx implements load balancing

Table of Contents

1: Introduction to load balancing

2. Functions of load balancing

1. Improve server performance

2. Improve system availability

3. Improve system scalability

4. Achieve traffic balance

3. Example configuration, how to use nginx to achieve load balancing

4. Load balancing policy configuration

1. Poll-based load balancing (default)

2. Weight-based load balancing

3. Load balancing based on IP HASH

4. Implement active and backup services

5. Back-end cooperation

One: Introduction to load balancing

nginx is a high-performance web server and reverse proxy server that can be used to achieve load balancing. The load balancing function of nginx is implemented by configuring the upstream block. In the upstream block, multiple backend servers can be configured and one of them can be selected through various algorithms to handle the request.

2. Functions of load balancing

1. Improve server performance

Load balancing can distribute requests to multiple servers, thereby improving the server’s processing power and response speed.

2. Improve system availability

When a server fails, load balancing can automatically transfer requests to other normal servers to avoid system downtime.

3.Improve the scalability of the system

When the system load increases, the system’s processing capabilities can be expanded by adding more servers.

4.Achieve traffic balance

The load balancing server distributes traffic to different application servers through some kind of scheduling algorithm to ensure that each server gets a reasonable workload and avoids overload.

3. Sample configuration, How to use nginx to achieve load balancing

?
http {

upstream backend {

server 192.168.116.12:8081;

server 192.168.116.12:8082;

 }

server {

listen 80;

 location/{

 proxy_pass http://backend;

 }

 }

?

In the configuration above, we have defined anupstream block called backend, which lists three Backend servers: backend1.example.com, backend2.example.com, and backend3.example.com. Then, in the server block, we proxy the request to the backend so that nginx will choose one of the three backend servers to handle the request.

nginx provides a variety of load balancing algorithms, such as weight, round-robin, and ip-hash. Different algorithms can be implemented by using corresponding configuration directives in the upstream block.

4. Load balancing policy configuration

1.Poll-based load balancing (default)< /strong>

Each request is assigned to different back-end application server nodes one by one in chronological order. If the back-end service fails, nginx can automatically remove the node. The specific configuration method is as shown in the demo example configuration above;

2.Based on weight (weight ) Load balancing

The default value of weight (weight) is 1. The higher the weight, the more requests are allocated.

For example: node A weight=2, node B weight=1, then the request will be polled according to A:B=2:1.

The configuration reference is as follows:

3.Load balancing based onIP HASH< /strong>

Each request is assigned according to the hash result of the accessed IP. Since the hash value is a unique value that does not repeat, each request can access the same back-end server. This can maintain the session and solve the session synchronization problem.

The configuration reference is as follows:

?
http {

ip_hash;

upstream backend {

server 192.168.116.12:8080;

server 192.168.116.12:8081;

 }

server {

listen 80;

 location/{

 proxy_pass http://backend;

 }

 }

?

4. Implement active and backup services

Use Nginx to automatically switch to the 192.168.12:8081 server when the 192.168.116.12:8080 server hangs up. You can use Nginx’s proxy module and load balancing function.

?
http {

upstream backend {

server 192.168.116.12:8080 fail_timeout=10s;

server 192.168.116.12:8081 backup;

 }

server {

listen 80;

 location/{

 proxy_pass http://backend;
 proxy_set_header Host $host;

 }

 }

In the above example:

The upstream block defines the server group backend, where 192.168.116.12:8080 is designated as the main server, and fail_timeout=10s is set, indicating that when the server is unavailable, Nginx will retry the connection after 10 seconds.
192.168.116.12:8081 is designated as the backup server, marked with the backup parameter, indicating that it will only be used when the primary server is unavailable.
The proxy_pass directive in the location block forwards the request to the server group backend, while proxy_set_header Host $host; will pass the original request’s Host header information to the backend server.
When the 192.168.116.12:8080 server is unavailable, Nginx will automatically forward requests to the alternate server 192.168.116.12:8081 so that users can continue to access the available server without knowing it.

Please make sure to configure the correct configuration in the Nginx configuration file on each server and adjust it according to your actual situation.

  • 5. Backend Cooperation

For the backend, the cooperation is relatively simple, and you only need to start multiple backend services;

Note: The port number started by the backend must be consistent with the port configured for nginx above.

syntaxbug.com © 2021 All Rights Reserved.