Using Nginx to realize load balancing in Linux system

Posted by dotMoe on Mon, 07 Feb 2022 09:34:09 +0100

1. Types of load balancing

1. Solve through hardware.

Common hardware includes commercial load balancers such as NetScaler, F5, Radware and Array, which are expensive and are generally used in hundreds of thousands or even millions of Internet applications.

2. Solve through software.

Common software include LVS, Nginx, apache, etc. they are based on Linux system and open source load balancing strategy, and the processing concurrency is generally 10000 to 20000.

The characteristic of Nginx is that it occupies less memory and has strong concurrency. The concurrency of Nginx is the best in the same type of web server, and many large companies are using it.

 

2. Distribution mode of nginx load balancing

1. Polling

Each request is allocated to different back-end servers one by one in chronological order. If one of the back-end servers hangs, it will be automatically rejected, and the polling access method is the default.

2. Weight

Specify the polling probability. The weight is directly proportional to the access ratio. It is generally used in the case of uneven performance of back-end servers.

3. Hash algorithm

Each request is allocated according to the hash algorithm result of the access IP. In this way, each visitor can access a back-end server regularly, which can solve the session problem of multiple servers.

 

3. Configure Nginx to achieve load balancing

1. Preparation

(1). Prepare two servers;

Suppose there are two virtual machine servers, one 192.168.1.128 and the other 192.168.1.129.

(2). Turn off ESLinux on these two servers;

vi etc/selinux/config

Execute the above command to open the configuration file of SELinux, press the # key to enter the editing mode, change SELINUX=enforcing # to # SELINUX=disabled, then press esc to exit the editing mode, enter: wq # save and exit, and execute the following command to make the configuration effective.

soure etc/selinux/config

If the configuration does not take effect, it is recommended to restart Linux.

init 6

(3). Open the port numbers on the two servers;

Query the open port number.

firewall-cmd --zone=public --list-ports

Open port 80. If the service application accesses other port numbers, it needs to be opened together.

firewall-cmd --zone=public --add-port=80/tcp --permanent

2. Configure load balancing of a single website

At www.aaa.com Com website as an example, configure the load balancing file and put it into the 192.168.1.128 server. The code is as follows:

# www_aaa_com and the following proxy_ Corresponding in pass
upstream www_aaa_com {

    # Access is allocated by polling by default
    # server 127.0.0.1:3001; 
    # server 127.0.0.1:3002;
    # server 192.168.1.129:3001;

    # If the weight value is added after the address, it means polling by ratio
    # server 127.0.0.1:3001 weight=1; 
    # server 127.0.0.1:3002 weight=1;
    # server 192.168.1.129:3001 weight=3;


    # Add ip_hash means that the IP address is allocated after hashing algorithm
    ip_hash;
    server 127.0.0.1:3001 weight=1; 
    server 127.0.0.1:3002 weight=1;
    server 192.168.1.129:3001 weight=3; 

}

server {
    listen       80;
    server_name  www.aaa.com;

    # charset koi8-r;
    # access_log  /var/log/nginx/host.access.log  main;

    location / {

        # Set the host header and the real address of the client so that the server can obtain the real IP of the client
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # disable cache 
        proxy_buffering off;
        # Reverse proxy address
        proxy_pass http://www_aaa_com;
    }

    # error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html

    error_page   500 502 503 504  /50x.html;

    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
}

Then put two sets of www.aaa in the server of 192.168.1.128 Com website with the same code and port numbers of 3001 and 3002 respectively. Www.aaa is also placed in the server of 192.168.1.129 The code of COM website is 3001, which realizes the load balancing of the server. When a server hangs up, the server user will automatically switch to another server to respond.

3. Configure forwarding of multiple websites

Take the following network topology as an example.

First configure www.aaa The nginx configuration file of COM is put into the server of 192.168.1.128. The code is as follows:

# www_aaa_com and the following proxy_ Corresponding in pass
upstream www_aaa_com {

    # Access is allocated by polling by default
    # server 127.0.0.1:3001; 
    # server 127.0.0.1:3002;
    # server 192.168.1.129:3001;

    # If the weight value is added after the address, it means polling by ratio
    # server 127.0.0.1:3001 weight=1; 
    # server 127.0.0.1:3002 weight=1;
    # server 192.168.1.129:3001 weight=3;


    # Add ip_hash means that the IP address is allocated after hashing algorithm
    ip_hash;
    server 127.0.0.1:3001; 

}

server {
    listen       80;
    server_name  www.aaa.com;

    # charset koi8-r;
    # access_log  /var/log/nginx/host.access.log  main;

    location / {

        # Set the host header and the real address of the client so that the server can obtain the real IP of the client
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # disable cache 
        proxy_buffering off;
        # Reverse proxy address
        proxy_pass http://www_aaa_com;
    }

    # error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html

    error_page   500 502 503 504  /50x.html;

    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
}

Configure www.bbb.com The nginx configuration file of COM is put into the server of 192.168.1.128. The code is as follows:

# www_bbb_com and the following proxy_ Corresponding in pass
upstream www_bbb_com {

    # Access is allocated by polling by default
    # server 127.0.0.1:3001; 
    # server 127.0.0.1:3002;
    # server 192.168.1.129:3001;

    # If the weight value is added after the address, it means polling by ratio
    # server 127.0.0.1:3001 weight=1; 
    # server 127.0.0.1:3002 weight=1;
    # server 192.168.1.129:3001 weight=3;


    # Add ip_hash means that the IP address is allocated after hashing algorithm
    ip_hash;
    server 127.0.0.1:3002 ; 
}
server {
    listen       80;
    server_name  www.bbb.com;

    # charset koi8-r;
    # access_log  /var/log/nginx/host.access.log  main;

    location / {
        # Set the host header and the real address of the client so that the server can obtain the real IP of the client
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # disable cache 
        proxy_buffering off;
        # Reverse proxy address  
        proxy_pass http://www_bbb_com;
    }

    # error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
}

Configure www.ccc.com The nginx configuration file of COM is put into the server of 192.168.1.128. The code is as follows:

# www_ccc_com and the following proxy_ Corresponding in pass
upstream www_ccc_com {	

    # Access is allocated by polling by default
    # server 127.0.0.1:3001; 
    # server 127.0.0.1:3002;
    # server 192.168.1.129:3001;

    # If the weight value is added after the address, it means polling by ratio
    # server 127.0.0.1:3001 weight=1; 
    # server 127.0.0.1:3002 weight=1;
    # server 192.168.1.129:3001 weight=3;


    # Add ip_hash means that the IP address is allocated after hashing algorithm
    ip_hash;
    server 192.168.1.129:3001; 
}

server {
    listen       80;
    server_name  www.ccc.com;

    # charset koi8-r;
    # access_log  /var/log/nginx/host.access.log  main;

    location / {
        # Set the host header and the real address of the client so that the server can obtain the real IP of the client
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # disable cache 
        proxy_buffering off;
        # Reverse proxy address
        proxy_pass http://www_ccc_com;
    }

    # error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
}

Then put www.aaa in the server of 192.168.1.128 COM, port number 3001; Put www.bbb in the server of 192.168.1.128 COM, port number 3002; Put www.ccc in the server of 192.168.1.129 Com code, the port number is 3001, so it is convenient to use nginx server to realize the forwarding of multiple websites.

 

Topics: Linux