Nginx Load Balancing Interpretation

Table of contents

What is load balancing

Load balancing scheduling algorithm

Method 1: Polling

Method 2: Weight

Method 3: ip_hash

Method 4: fair (third-party plug-in)

Method 5: url_hash (third-party plug-in)

Method 6: Minimal connections

Server configuration items in upstream


What is load balancing

In order to avoid server crashes, everyone will use load balancing to share server pressure. The platform servers are formed into a cluster. When a user accesses, they first access a forwarding server, and then the forwarding server distributes the access to servers with less pressure.

Load Balancing is a technology that distributes incoming request traffic to multiple back-end servers to eliminate single points of failure, improve availability, and increase scalability.

Load balancing scheduling algorithm

Method 1: Polling

RR (default polling) each request is assigned to different back-end servers one by one in chronological order. If the back-end server goes down, it can be automatically eliminated.

upstream test {
  server weiyigeek.top:8080;
  server weiyigeek.top:8081;
}

server {
  listen  81;
  server_name  weiyigeek.top;
  client_max_body_size 1024M;
  location / {
    proxy_pass http://test;
    proxy_set_header Host $host:$server_port;
  }
} 

Method 2: Weight

The weight specifies the polling probability. The weight is proportional to the access ratio and is used when the performance of the back-end server is uneven.

upstream test {
  server weiyigeek.top:8081 weight=1;
  server weiyigeek.top:8080 weight=9;  #那么10次一般只会有1次会访问到8081,而有9次会访问到8080
} 

Method 3: ip_hash

ip_hash session adhesion, the above two methods have a problem, that is, when the next request comes, the request may be distributed to another server. When our program is not stateless (session is used to save data), then There is a big problem. For example, if the login information is saved in the session, then you need to log in again when jumping to another server. So many times we need a customer to only access one server, then we need to use iphash, each request of iphash 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 test {
  ip_hash;
  server weiyigeek.top:8080;
  server weiyigeek.top:8081;
} 

 But sometimes the IP will be switched and the session problem will still exist.

Method 4: fair (third-party plug-in)

fair (third party) allocates requests based on the response time of the backend server, with priority given to those with shorter response times.

upstream backend {
  fair;
  server weiyigeek.top:8080;
  server weiyigeek.top:8081;
} 

Method 5: url_hash (third-party plug-in)

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.

Add a hash statement to the upstream. Other parameters such as weight cannot be written in the server statement. Hash_method is the hash algorithm used.

upstream backend {
  hash $request_uri;
  hash_method crc32;
  server weiyigeek.top:8080;
  server weiyigeek.top:8081;
} 

Method 6: Minimal connections

Description: Distribute requests to the service with the fewest connections.

upstream  dalaoyang-server {
  least_conn;
  server    weiyigeek.top:10001;
  server    weiyigeek.top:10002;
} 

Server configuration items in upstream

The upstream module is mainly responsible for load balancing configuration and distributes requests to back-end servers through the default polling scheduling method. 

The basic syntax of upstream is as follows. An upstream needs to set a name. This name can be used as a proxy host in the server. 

  • weight: weight
  • down: The current server does not participate in load balancing for the time being.
  • backup: reserved backup server; when all other non-backup machines are down or busy, backup machines are requested.
  • max_fails: max_fails is the maximum number of errors. You can set a max_fails for each server. If an error occurs in the request server, max_fails will be increased by one. If the number of errors in the request server reaches max_fails, Nginx will mark the server as a fault state, and then Won't ask for it again.
  • fail_timeout: service suspension time after max_fails
  • max_conns: Limit the maximum number of connections

Guess you like

Origin blog.csdn.net/m0_62436868/article/details/132796628