Home  >  Article  >  Operation and Maintenance  >  How to implement load balancing polling configuration in nginx

How to implement load balancing polling configuration in nginx

PHPz
PHPzforward
2023-05-18 17:58:212233browse

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

How to implement load balancing polling configuration in nginx

How to implement load balancing polling configuration in nginx

## 1. Round-robin polling (default)

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;
    }
}

Access result: ABABABABA

2, Weighted polling

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;
    }
}

Access result: ABBABB ABBABB

Note: The access result affected by weight is calculated based on the minimum proportion, not the ideal Status: AABBBBB AABBBBB

3, ip_hash

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;
    }
}

Access result:

IP1:AAAAAA

IP2:BBBBBB

4, Hot standby

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;
    }
}

Access result:

Access 1: Both services are normal. AAAAAA

Access 2: Deactivated 10.0.0.110:8099. BBBBBB

Access 3: Restart 10.0.0.110:8099. AAAAAA

5. Add parameter optimization

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;
    }
}

Access results: Same as 4

backup has the highest priority. When this parameter is set, the corresponding service can only As a hot standby.

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete