Summary of Nginx load balancing methods

Summary of Nginx load balancing methods

To understand load balancing, you must first understand forward proxy and reverse proxy.

Note:

  • Forward proxy, the agent is the user.
  • Reverse proxy, the proxy is the server

What is Load Balancing

The more visits a server receives per unit time, the greater the pressure on the server. When the pressure exceeds its ability to bear, the server will crash. In order to avoid server crashes and provide users with a better experience, we share server pressure through load balancing.

We can build many servers to form a server cluster. When a user visits a website, he first accesses an intermediate server, and then lets this intermediate server select a server with less pressure in the server cluster, and then directs the access request to that server. In this way, every time a user visits, the pressure on each server in the server cluster will be balanced, sharing the server pressure and avoiding server crashes.

Load balancing is achieved using the principle of reverse proxy.

Several common methods of load balancing

1. Polling (default)

Each request is assigned to a different backend server one by one in chronological order. If the backend server is down, it can be automatically removed.

upstream backserver {
 server 192.168.0.14;
 server 192.168.0.15;
}

2. Weight

Specifies the polling probability. The weight is proportional to the access ratio and is used when the backend 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 visited. In the above example, they are 30% and 70% respectively.

3. There is a problem with the above method. In the load balancing system, if a user logs in on a server, when the user makes a second request, because we are a load balancing system, each request will be relocated to one of the servers in the cluster. If a user who has logged in to a server is relocated to another server, his login information will be lost, which is obviously inappropriate.

We can use the ip_hash instruction to solve this problem. If the client has already visited a server, when the user visits it again, the request will be automatically located to the server through the hash algorithm.

Each request is assigned according to the hash result of the access IP, so that each visitor accesses a fixed backend 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 distributed based on the response time of the backend server, with requests with shorter response times given priority.

upstream backserver {
  server server1;
  server server2;
  fair;
}

5. url_hash (third party)

Requests are distributed according to the hash result of the accessed URL, so that each URL is directed to the same backend server. This is more effective when the backend server is cached.

upstream backserver {
  server squid1:3128;
  server squid2:3128;
  hash $request_uri;
  hash_method crc32;
}123456

The status of each device is set to:

  • down means that the server in front of the order is temporarily not involved in the load
  • weight defaults to 1. The larger the weight, the greater the weight of the load.
  • max_fails: The default number of request failures allowed is 1. When the maximum number is exceeded, an 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. So this machine will have the lightest pressure.

Configuration example:

#user nobody;
worker_processes 4;
events {
  # Maximum number of concurrent worker_connections 1024;
}
http{
  # List of servers to be selected upstream myproject{
    # The ip_hash directive directs the same user to the same server.
    ip_hash;
    server 125.219.42.4 fail_timeout=60s;
    server 172.31.2.183;
    }

  server{
        # Listening port listen 80;
        # Root directory location / {
          # Select which server list proxy_pass http://myproject;
        }

      }
}

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Example of using nginx as a reverse proxy to achieve load balancing
  • 4 configuration examples of Nginx load balancing
  • Illustration of the steps for installing nginx on Windows (reverse proxy and load balancing)
  • Nginx+Windows load balancing configuration method
  • Windows uses nginx to implement website load balancing test example
  • Nginx installation notes (including PHP support, virtual hosts, reverse proxy load balancing)
  • Detailed explanation of Nginx server load balancing strategy (6 types)
  • How to configure load balancing for TCP in Nginx server
  • Nginx geoip module to achieve regional load balancing
  • Nginx load balancing multi-site shared session

<<:  Detailed explanation of Angular routing sub-routes

>>:  Detailed explanation of the implementation of shared modules in Angular projects

Recommend

Vue ElementUI implements asynchronous loading tree

This article example shares the specific code of ...

A detailed introduction to Linux system configuration (service control)

Table of contents Preface 1. System Service Contr...

Summary of some practical little magic in Vue practice

How can you forget lazy loading of routes that al...

Detailed explanation of grep and egrep commands in Linux

rep / egrep Syntax: grep [-cinvABC] 'word'...

A brief discussion on the principle of js QR code scanning login

Table of contents The essence of QR code login Un...

HTML Tutorial: Collection of commonly used HTML tags (4)

Related articles: Beginners learn some HTML tags ...

How to use Xtrabackup to back up and restore MySQL

Table of contents 1. Backup 1.1 Fully prepared 1....

CSS sets the list style and creates the navigation menu implementation code

1. Set the list symbol list-style-type: attribute...

Use Angular CDK to implement a Service pop-up Toast component function

Table of contents 1. Environmental Installation 2...

10 Website Usability Tips Everyone Should Know

Let’s not waste any more time and get straight to...

An audio-visual Linux distribution that appeals to audiophiles

I recently stumbled upon the Audiovisual Linux Pr...

HTML basics summary recommendation (paragraph)

HTML Paragraph Paragraphs are defined by the <...

Detailed explanation of MySQL phantom reads and how to eliminate them

Table of contents Transaction Isolation Level Wha...