What are the modes for nginx to implement load balancing?

王林
Release: 2020-06-24 17:28:38
forward
7865 people have browsed it

This article comes from the nginx tutorial. It introduces several modes of nginx to achieve load balancing. It has certain reference value and I hope it can help everyone. nginx implements load balancing mode: 1. Polling; 2. ip_hash; 3. url_hash; 4. fair.

What are the modes for nginx to implement load balancing?

(Recommended tutorial:nginx tutorial)

nginx has several modes to implement load balancing:

1. Polling

Each request is assigned to different back-end servers one by one in chronological order, which is also the default mode of nginx. The configuration of polling mode is very simple, just add the server list to the upstream module.

The following configuration means: there are three servers in the load. When a request arrives, nginx allocates the request to the three servers in chronological order.

upstream serverList { server 1.2.3.4; server 1.2.3.5; server 1.2.3.6; }
Copy after login

2. ip_hash

Each request is assigned according to the hash result of the accessed IP. The same IP client always accesses a back-end server. It can ensure that requests from the same IP are sent to a fixed machine, which can solve the session problem.

The following configuration refers to: There are three servers in the load. When a request arrives, nginx gives priority to allocation according to the result of ip_hash, that is, the request for the same IP is fixed on a certain server, and the others are allocated according to the result of ip_hash. Time sequence distributes requests to three servers for processing.

upstream serverList { ip_hash server 1.2.3.4; server 1.2.3.5; server 1.2.3.6; }
Copy after login

3. url_hash

Requests are allocated according to the hash result of the accessed URL. The same URL is always forwarded to the same back-end server for processing.

upstream serverList { server 1.2.3.4; server 1.2.3.5; server 1.2.3.6; hash $request_uri; hash_method crc32; }
Copy after login

4, fair

Requests are allocated according to the response time of the back-end server, and those with short response times are allocated first.

upstream serverList { server 1.2.3.4; server 1.2.3.5; server 1.2.3.6; fair; }
Copy after login

The above is the detailed content of What are the modes for nginx to implement load balancing?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!