What are the nginx load balancing strategies?
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.
Nginx load balancing strategy
Load balancing is used from the list of backend servers defined from the "upstream" module Select a server to accept the user's request. A most basic upstream module is like this. The server in the module is the server list:
#动态服务器组 upstream dynamic_zuoyu { server localhost:8080; #tomcat 7.0 server localhost:8081; #tomcat 8.0 server localhost:8082; #tomcat 8.5 server localhost:8083; #tomcat 9.0 }
After the upstream module configuration is completed, you want to reverse proxy the specified access to the server list:
#其他页面反向代理到tomcat容器 location ~ .*$ { index index.jsp index.html; proxy_pass http://dynamic_zuoyu; }
This is the most basic load balancing instance, but it is not enough to meet actual needs;
Currently, the upstream module of the Nginx server supports 6 ways of distribution:
Round The query method is the default load balancing strategy of the upstream module. Each request is distributed to a different backend server one by one in chronological order.
Weight method, specify the probability of polling based on the polling strategy.
ip_hash, specifies the load balancer allocation method based on the client IP. This method ensures that requests from the same client are always sent to the same server to ensure session sessions. In this way, each visitor has fixed access to a back-end server, which can solve the problem that the session cannot cross servers.
least_conn, forward the request to the backend server with fewer connections. The polling algorithm forwards requests to each backend evenly so that their loads are roughly the same; however, some requests take a long time, which will cause the backend where they are located to have a higher load. In this case, least_conn can achieve better load balancing effect.
The implementation of third-party load balancing strategies requires the installation of third-party plug-ins.
fair, allocate requests according to the response time of the server, and those with short response times will be allocated first.
url_hash, allocates requests according to the hash result of the accessed URL, so that each URL is directed to the same back-end server, and must be used in conjunction with cache hits. Multiple requests for the same resource may arrive at different servers, resulting in unnecessary multiple downloads, low cache hit rates, and a waste of resource time. Using url_hash, the same URL (that is, the same resource request) can reach the same server. Once the resource is cached, it can be read from the cache when a request is received.
For more Nginx related technical articles, please visit the Nginx Usage Tutorial column to learn!
The above is the detailed content of What are the nginx load balancing strategies?. 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 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.

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.

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

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.

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.

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]

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
