Several ways for Nginx to achieve load balancing
What is load balancing
When the number of visits per unit time of a server is greater, the pressure on the server will be greater. When it exceeds its own capacity, the server will will crash. In order to avoid server crashes and provide users with a better experience, we use load balancing to share server pressure.
We can build many servers to form a server cluster. When a user accesses the website, he first visits an intermediate server, and then lets the intermediate server choose a server with less pressure in the server cluster, and then transfers the server to the server cluster. Access requests are directed to the server. In this way, every visit by a user will ensure that the pressure of each server in the server cluster tends to be balanced, sharing the server pressure and avoiding server crash.
Load balancing is implemented using the principle of reverse proxy.
Several common methods of load balancing
1. Polling (default)
Each request is assigned to different servers one by one in chronological order The end server can be automatically eliminated if the backend server goes down.
upstream backserver { server 192.168.0.14; server 192.168.0.15; }
2. Weight
Specifies the polling probability, weight is proportional to the access ratio, and is used for
situations where back-end server performance is uneven.
upstream backserver { server 192.168.0.14 weight=3; server 192.168.0.15 weight=7; }
The higher the weight, the greater the probability of being accessed. As in the above example, they are 30% and 70% respectively.
3. There is a problem with the above method, that is, in the load balancing system, if the user logs in to a certain server, then when the user makes a second request, because we are a load balancing system, every time Every request will be redirected to one of the server clusters. If a user who has logged in to a server is redirected to another server, his login information will be lost. This is obviously inappropriate.
We can use the ip_hash instruction to solve this problem. If the customer has already visited a certain server, when the user visits again, the request will be automatically located to the server through the hash algorithm.
Each request is allocated according to the hash result of the access IP, so that each visitor has fixed access to a back-end server, which can solve the session problem.
upstream backserver { ip_hash; server 192.168.0.14:88; server 192.168.0.15:80; }
4. fair (third party)
Requests are allocated according to the response time of the backend server, and those with short response times are allocated first.
upstream backserver { server server1; server server2; fair; }
5. url_hash (third party)
Distribute requests according to the hash result of the accessed URL, so that each URL is directed to the same back-end server. It is more effective when the back-end server is cached.
upstream backserver { server squid1:3128; server squid2:3128; hash $request_uri; hash_method crc32; }
The status of each device is set to:
1.down means that the previous server will not participate in the load temporarily
2.weight The default is 1. The greater the weight, the weight of the load The bigger it gets.
3.max_fails
: The number of allowed request failures is 1 by default. When the maximum number is exceeded, the error defined by the proxy_next_upstream
module is returned
4.fail_timeout: The time to pause after max_fails
failures.
5.backup: When all other non-backup machines are down or busy, request the backup machine. So this machine will have the least pressure.
Configuration example:
#user nobody;worker_processes 4; events { # 最大并发数 worker_connections 1024; } http{ # 待选服务器列表 upstream myproject{ # ip_hash指令,将同一用户引入同一服务器。 ip_hash; server 125.219.42.4 fail_timeout=60s; server 172.31.2.183; } server{ # 监听端口 listen 80; # 根目录下 location / { # 选择哪个服务器列表 proxy_pass http://myproject; } } }
Related recommendations:
Nginx reverse proxy and load balancing practice
##nginx four Layer load balancing configuration
Detailed explanation of load balancing Nginx
The above is the detailed content of Several ways for Nginx to achieve load balancing. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".
