How to implement load balancing polling configuration in nginx
1. Overview
The allocation algorithm currently supported by Nginx's upstream:
1. Round-robin polling 1:1 to process requests in turn (default)
Each request is processed one by one in chronological order Assigned to different application servers, if the application server goes down, it will be automatically removed, and the remaining ones will continue to be polled.
2. Weight weight (weighted polling)
By configuring the weight, the polling probability is specified. The weight is proportional to the access ratio and is used for uneven performance of the application server.
3. ip_hash hash algorithm
Each request is allocated according to the hash result of the accessed IP, so that each visitor has a fixed access to an application server, which can solve the problem of session sharing. If the application server fails, it needs to be shut down manually.
Parameter meaning:
down: Indicates that the previous server will not participate in the load temporarily
weight: The default is 1. The greater the weight, the greater the weight of the load.
max_fails: The number of allowed request failures defaults to 1. When the maximum number is exceeded, the error defined by the proxy_next_upstream module is returned.
fail_timeout: The pause time after max_fails failures.
backup: When all other non-backup machines are down or busy, request the backup machine.
2. Configuration
upstream tg-t4 {
server 10.0.0.110:8099;
server 10.0.0.110:8098;
}
server {
listen 8096;
server_name www.tg-t4.com;
location / {
proxy_pass http://tg-t4;
}
}
Copy after login
Access result: ABABABABA2, Weighted pollingupstream tg-t4 { server 10.0.0.110:8099; server 10.0.0.110:8098; } server { listen 8096; server_name www.tg-t4.com; location / { proxy_pass http://tg-t4; } }
upstream tg-t4 {
server 10.0.0.110:8099 weight=2;
server 10.0.0.110:8098 weight=5;
}
server {
listen 8096;
server_name www.tg-t4.com;
location / {
proxy_pass http://tg-t4;
}
}
Copy after login
Access result: ABBABB ABBABB Note: The access result affected by weight is calculated based on the minimum proportion, not the ideal Status: AABBBBB AABBBBB3, ip_hashupstream tg-t4 { server 10.0.0.110:8099 weight=2; server 10.0.0.110:8098 weight=5; } server { listen 8096; server_name www.tg-t4.com; location / { proxy_pass http://tg-t4; } }
upstream tg-t4 {
server 10.0.0.110:8099;
server 10.0.0.110:8098;
ip_hash;
}
server {
listen 8096;
server_name www.tg-t4.com;
location / {
proxy_pass http://tg-t4;
}
}
Copy after login
Access result: IP1:AAAAAAIP2:BBBBBB4, Hot standbyupstream tg-t4 { server 10.0.0.110:8099; server 10.0.0.110:8098; ip_hash; } server { listen 8096; server_name www.tg-t4.com; location / { proxy_pass http://tg-t4; } }
upstream tg-t4 {
server 10.0.0.110:8099;
server 10.0.0.110:8098 backup;
}
server {
listen 8096;
server_name www.tg-t4.com;
location / {
proxy_pass http://tg-t4;
}
}
Copy after login
Access result:Access 1: Both services are normal. AAAAAAAccess 2: Deactivated 10.0.0.110:8099. BBBBBBAccess 3: Restart 10.0.0.110:8099. AAAAAA5. Add parameter optimizationupstream tg-t4 { server 10.0.0.110:8099; server 10.0.0.110:8098 backup; } server { listen 8096; server_name www.tg-t4.com; location / { proxy_pass http://tg-t4; } }
upstream tg-t4 {
server 10.0.0.110:8099 weight=1 max_fails=2 fail_timeout=2;
server 10.0.0.110:8098 weight=3 max_fails=2 fail_timeout=2 backup;
}
server {
listen 8096;
server_name www.tg-t4.com;
location / {
proxy_pass http://tg-t4;
}
}
Copy after login
Access results: Same as 4backup has the highest priority. When this parameter is set, the corresponding service can only As a hot standby. upstream tg-t4 { server 10.0.0.110:8099 weight=1 max_fails=2 fail_timeout=2; server 10.0.0.110:8098 weight=3 max_fails=2 fail_timeout=2 backup; } server { listen 8096; server_name www.tg-t4.com; location / { proxy_pass http://tg-t4; } }
The above is the detailed content of How to implement load balancing polling configuration in nginx. 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 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.

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

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

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]

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

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.
