One of the simplest nginx.conf configurations

http { include mime.types; Default_type application/ ocTEt-stream; # Default file type: sendfile on; Keepalive_timeout 65; Server {listen 8081; server {listen 8081; Server_name localhost; / {proxy_pass http://0.0.0.0:9000; Error_page 500 502 503 504/50x.html; # default location = /50x.html {root HTML; }}}Copy the code

Start with the simplest Node service:

const http = require('http'); const server = http.createServer(); Const host = '0.0.0.0' const port = 9000 let n = 0 server.on('request', function (req, res) {n += 1 console.log(' request' : ', n) res.write('Hello World!!! '); res.end(); }); Server.listen (port, host, function () {console.log(' server started, please visit http://${host}:${port} '); })Copy the code

Access http://localhost:8081/. Nginx is able to send requests to the node 9000 port service

Let’s move on to load balancing

What is load balancing

For example, a new load of bricks arrived at the construction site, and the boss asked them to be moved before dark. However, there is only one worker on the site. In this case, it is possible to carry 1000 jin per trip before dark. But if 1,000 jin of bricks were carried on his shoulder, the worker would be killed at once. At this time, the contractor hired another two people, three people to work together, each person carry 300 jin, that everyone is very easy to finish the work, no one will die.

The same goes for the server. Now let’s say there are 1000 requests per second, and a server can’t handle it, and it’s going to die every minute. We increased the number of servers to three, so that each could handle 300, each with ease.

So the question is, there is a difference in the physical strength of the three workers. When the contractor is arranging the work, is it absolutely fair to carry 300 pounds each? Or according to the actual physical condition of the real workers, more can be done?

The same is true for servers. Different servers have different processing power. What Nginx load balancing does is to balance the number of requests to each server based on the server’s capabilities. The balance here is not an absolute average, how to balance the specific request, we can set different balance strategy according to their own needs

Load balancing Policy

Polling policy (default)

According to the above, to play load balancing, first of all there are many machines, one can not play ah. However, the condition is limited, so I use the same node service on different ports to represent multiple machines:

As shown in the picture above, the three services are identical and used separately9000,9001,9002The port starts, then modifynginx.confFile,Nginx load balancingMainly through configurationupstreamTo achieve:

http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; "Upstream backserver {# configure server 0.0.0.0:9000; Server 0.0.0.0:9001; Server 0.0.0.0:9002; } server { listen 8081; server_name localhost; location / { proxy_pass http://backserver; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }}}Copy the code

As you can see from the above code, the change is relatively simple, adding an upsteam configuration. This is the simplest polling strategy. When a large number of requests come in, Nginx distributes them equally across the three servers. Next, let’s test whether our configuration works by sending 600 requests in batches through the tool

The results were as expected, with each service handling 200 requests.

Weighted polling strategy

This one is also very simple, as you can guess from the name. This one is polling according to the weights we specify. Nginx.conf is modified as follows:

http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; Upstream backserver {server 0.0.0.0:9000 weight=2; Server 0.0.0.0:9001 weight = 1; Server 0.0.0.0:9002 weight = 1; } server { listen 8081; server_name localhost; location / { proxy_pass http://backserver; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }}}Copy the code

We add the weight parameter to the end of each service address to represent the weight, which means that port 9000 handles 50% of requests, port 9001 handles 25%, and port 9002 handles 25%. Let’s test this again by batch sending 100 requests and see what happens:

Ip_hash strategy

This is also very simple, you can guess a little bit by the name, to assign requests by IP. Requests from fixed clients are allocated to a fixed server. Next, modify the nginx.conf configuration

http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream backserver { ip_hash; Server 0.0.0.0:9000; Server 0.0.0.0:9001; Server 0.0.0.0:9002; } server { listen 8081; server_name localhost; location / { proxy_pass http://backserver; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }}}Copy the code

Again, because the condition is not allowed, it is supposed to use multiple clients to access, and the console to print the IP to see the results, but I only have the local machine, so I can not do this experiment. If I send 100 requests to the same server, the other two will receive zero requests. Let’s test this:

As expected, only the service from port 9002 received 100 requests, while the other two services received zero requests

least_conn

Forward the request to the back-end server with fewer connections

upstream backserver {
    least_conn; 
    server 0.0.0.0:9000;
    server 0.0.0.0:9001;
    server 0.0.0.0:9002;
}
Copy the code

url_hash

Requests are allocated according to the hash result of the accessed URL. Each URL points to a fixed server in the back end, which can improve the cache efficiency when NGINx serves as a static server. The hash software package of nginx must be installed.

upstream backserver { hash $request_uri; # Implement that each URL is directed to the same backend server hash_method crc32; # hash_method is the hash algorithm used server 0.0.0.0:9000; Server 0.0.0.0:9001; Server 0.0.0.0:9002; }Copy the code

fair

Based on the response time of the back-end server, short response time is preferentially allocated, depending on the third-party plug-in nginx-upport-fair, which needs to be installed first.

upstream backserver { fair; # Implement short response time priority assignment server 0.0.0.0:9000; Server 0.0.0.0:9001; Server 0.0.0.0:9002; }Copy the code