What are the implementation methods of nginx load balancing?
In the server cluster, Nginx plays the role of a proxy server (ie, reverse proxy). In order to avoid excessive pressure on a single server, it forwards requests from users to different servers. Load balancing is used to select a server from the backend server list defined by the "upstream" module to accept user requests.
Several common methods of load balancing
1. Polling (default)
Each request They are assigned to different backend servers one by one in chronological order. If the backend server goes down, it can be automatically eliminated.
upstream backserver { server 192.168.0.14; server 192.168.0.15; }
2, weight
Specify the polling probability, weight is proportional to the access ratio, and is used when the 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. ip_hash
One problem with the above method is that in the load balancing system, if the user logs in on a certain server, then when the user makes a second request, because We are a load balancing system. Each request will be redirected to a certain server cluster. 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 server in front of the single is temporarily not participating in the load
2), the weight defaults to 1. The weight exceeds The larger it is, the greater the weight of the load.
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: After max_fails failures, the time.
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; } } }
The above is the detailed content of What are the implementation methods of nginx 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



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.

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

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.

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).

To get Nginx to run Apache, you need to: 1. Install Nginx and Apache; 2. Configure the Nginx agent; 3. Start Nginx and Apache; 4. Test the configuration to ensure that you can see Apache content after accessing the domain name. In addition, you need to pay attention to other matters such as port number matching, virtual host configuration, and SSL/TLS settings.

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.

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

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".
