Nginx load balancing algorithm and failover analysis

Nginx load balancing algorithm and failover analysis

Overview

Nginx load balancing provides upstream servers (servers accessed by real business logic), load balancing, failover, failure retry, fault tolerance, health checks, etc.
When an upstream server (the server accessed by the real business logic) fails, it can be transferred to other upstream servers (the servers accessed by the real business logic).

Configuration

upstream backServer{
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
  }
  
  server {
    listen 80;
    server_name www.itmayiedu.com;
    location / {
      ### Specify the upstream server load balancing server proxy_pass http://backServer;
      index index.html index.htm;
    }
  }

Load Balancing Algorithm

Each request is assigned to a different backend service in chronological order. If a backend server crashes, the faulty system will be automatically removed so that user access is not affected.

weight (polling weight)

The larger the weight value is, the higher the probability of access is. It is mainly used when the performance of each backend server is uneven. Or just set different weights in the master-slave situation to achieve reasonable and effective use of host resources.

Usually in proportion

upstream backServer{
 server 127.0.0.1:8080 weight=1;
 server 127.0.0.1:8081 weight=2;
 }

ip_hash

Each request is assigned according to the hash result of the access IP, so that visitors from the same IP address can access a fixed backend server and effectively solve the session sharing problem of dynamic web pages. Commonly known as IP binding.

upstream backServer{
   server 127.0.0.1:8080 ;
   server 127.0.0.1:8081 ;
   ip_hash; 
 }

fair (third party)

The fair algorithm is a smarter load balancing algorithm than weight and ip_hash. It can intelligently balance the load according to the page size and loading time. That is, it allocates requests according to the response time of the backend server, giving priority to requests with shorter response time. Nginx itself does not support fair. If this scheduling algorithm is required, the upstream_fair module must be installed.

url_hash (third party)

Distributing requests according to the hash results of the accessed URLs so that each URL is directed to a backend server can further improve the efficiency of the backend cache server. Nginx itself does not support url_hash. If this scheduling algorithm is required, you must install the Nginx hash package.

Nginx configuration failover

  • If the upstream server (real access server) fails or fails to respond in time, the next server should be directly rotated to ensure high availability of the server.
  • The timeout between nginx and the upstream server (the server actually accessed) The timeout for the backend server connection_the timeout for initiating a handshake and waiting for a response proxy_connect_timeout 1s;
  • The timeout period that nginx sends to the upstream server (the server actually accessed) is proxy_send_timeout 1s;
  • nginx accepts the upstream server (the server actually accessed) timeout proxy_read_timeout 1s;

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Example of how to enable Brotli compression algorithm for Nginx
  • Example of enabling Brotli algorithm compression in Nginx
  • Nginx uses the Gzip algorithm to compress messages
  • Detailed explanation of the underlying implementation method of Nginx polling algorithm
  • A brief understanding of several scheduling algorithms for Nginx seven-layer load balancing
  • C# implements Nginx smooth weighted polling algorithm
  • In-depth analysis of nginx's four scheduling algorithms and advanced
  • Detailed explanation of the implementation process of Nginx enabling Brotli compression algorithm

<<:  MySQL multi-table join query example explanation

>>:  Detailed explanation of two methods for setting global variables and session variables in MySQL

Recommend

Detailed explanation of Vue mixin

Table of contents Local Mixin Global Mixins Summa...

How to install MySQL using yum on Centos7 and achieve remote connection

Centos7 uses yum to install MySQL and how to achi...

Example of implementing login effect with vue ElementUI's from form

Table of contents 1. Build basic styles through E...

Three ways to share component logic in React

Without further ado, these three methods are: ren...

Sample code for implementing Google third-party login in Vue

Table of contents 1. Developer Platform Configura...

First experience of creating text with javascript Three.js

Table of contents Effect Start creating text Firs...

Writing daily automatic backup of MySQL database using mysqldump in Centos7

1. Requirements: Database backup is particularly ...

Detailed explanation of querying JSON format fields in MySQL

During the work development process, a requiremen...

CSS3 achieves cool sliced ​​image carousel effect

Today we will learn how to use CSS to create a co...

When is it appropriate to use dl, dt, and dd?

dl:Definition list Definition List dt:Definition t...

5 VueUse libraries that can speed up development (summary)

Table of contents What utilities does VueUse have...

Implementation of MySQL's MVCC multi-version concurrency control

1 What is MVCC The full name of MVCC is: Multiver...

How to use vue3+TypeScript+vue-router

Table of contents Easy to use Create a project vu...

Button is stretched on both sides in IE

When you write buttons (input, button), you will f...