Nginx (Engine X) is a high-performance HTTP and reverse proxy server.
What is load balancing
Based on the existing network structure, load balancing provides a cheap, effective and transparent method to expand the bandwidth of network devices and servers, increase the throughput, strengthen the network data processing capacity, and improve the flexibility and availability of the network. This means that execution is spread over multiple units of operation.
In a nutshell:
When a server has more traffic per unit of time, the greater the pressure on the server will be. When the pressure exceeds its capacity, the server will crash. In order to avoid server crashes and provide users with better experience, load balancing is used to share server load.
We have many servers and form a server cluster. When users access a request, they visit an intermediate server first. The intermediate server can distribute the request to the server with the least pressure, so that the pressure of each server is balanced.
Install nginx
Nginx official download
After the installation is complete, start nginx and type 127.0.0.1:8080
Nginx common commands
Start nginx(Windows start nginx);
Nginx -t Checks whether the directory of the nginx configuration file is correctly configured
Nginx -s reload nginx(when configuration information is changed)
Nginx -s quit stops ngix
Start the Node project and listen on ports 3000 and 3001 respectively
For those who don’t understand, check out the Express Hello World example
The following
Configure nginx
1. Open the nginx.conf file in the conf folder of the nginx installation directory
worker_processes 1; // The nginx process is set to the same number of CPU cores. Upstream {//upstream stands for load server pool and defines a server pool named firsttest#ip_hash; Automatically locate the server for the first time
# weight weightServer 127.0.0.1:3000; Server 127.0.0.1:3001; } server { listen 8080; //# monitor port
location / {//# root directoryproxy_pass http://firsttest; // Use that load server pool}}}Copy the code
2. Access 127.0.0.1:8080
Nginx will reverse proxy requests to port 8080 to port 3000 or port 3001. This is a very simple load balancing, refreshing, access to port 3000 or port 3001 ratio is about 1:1. This is the default polling.
3. We can also specify the polling probability and set the weight.
Weight is proportional to the access ratio and is used when back-end server performance is uneven. The higher the weight, the greater the probability of being accessed, as follows, the request has a 2/3 chance of landing at 3000 and a 1/3 chance of landing at 3001.
upstream firsttest{
#ip_hash; Automatically locate the server for the first time
# weight weightServer 127.0.0.1:3000 weight = 2; Server 127.0.0.1:3001; }Copy the code
4. The ip_hash directive
One problem with this approach is that accessing the same page from the same IP address will be allocated to different servers. If the session is not synchronized, there will be a lot of problems, such as the most common login state, which is obviously not possible.
We can solve this problem by using the IP_hash directive. If the client has already accessed a server, when the user accesses it again, the request will be automatically located to the server through the hash algorithm.
upstream firsttest{
#ip_hash; Automatically locate the server for the first time
#weight Weight ip_hash; Server 127.0.0.1:3000 weight = 2; Server 127.0.0.1:3001; }Copy the code
This is a simple nginx load balancing, of course, if you really want to achieve a real load balancing far more than this, still need to configure a lot of commands, also need time to learn, thank you for watching.