Configuration Guide for nginx four layer load balancing

Time:2021-12-5

1、 Introduction to four layer load balancing

What is four layer load balancing

The so-called four layer load balancing is to determine the final internal server through the target address and port in the message and the server selection mode set by the load balancing equipment.

Taking the common TCP as an example, when receiving the first syn request from the client, the load balancing device selects the best server and modifies the target IP address in the message (changed to the back-end server IP) and directly forwarded to the server. TCP connection establishment, that is, the three-time handshake is established directly between the client and the server. The load balancing device only plays a forwarding action similar to that of the router. In some deployment cases, in order to ensure that the server’s packet can be returned to the load balancing device correctly, the original packet may be updated while forwarding the message To modify the source address.

Application scenario

1. Four layers + seven layers are used for load balancing. Four layers can ensure the high availability of load balancing of seven layers;

2. Load balancing can be used for port forwarding

3. Database read / write separation

Four layer load balancing features

1. Layer 4 load balancing can only forward TCP / IP protocol and UDP protocol. It is usually used to forward ports, such as TCP / 22 and UDP / 53;

2. Layer 4 load balancing can be used to solve the problem of port restriction of layer 7 load balancing; (layer 7 load balancing uses 65535 port numbers at most)

3. Layer 4 load balancing can solve the problem of high availability of layer 7 load balancing; (multiple backend seven layer load balancing can be used at the same time)

4. The forwarding efficiency of layer 4 is much higher than that of layer 7, but only supports TCP / IP protocol, not HTTP and HTTPS protocols;

5. In general, for large concurrency scenarios, four layers of load balancing are added in front of the seven layers of load.

2、 Four layer load balancing environment construction

Environmental preparation

host IP identity
lb4 172.16.1.6,10.0.0.6 Four layer load balancing
lb01 172.16.1.4,10.0.0.4 Seven layer load balancing
lb02 172.16.1.5,10.0.0.5 Seven layer load balancing

Lb4 and lb02 build nginx

#Configure Yum source
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

#Installing nginx
[[email protected] ~]# yum install nginx -y
[[email protected] ~]# yum install nginx -y

#Create user
[[email protected] ~]# groupadd www -g 666 && useradd www -u 666 -g 666 -s /sbin/nologin -M
[[email protected] ~]# groupadd www -g 666 && useradd www -u 666 -g 666 -s /sbin/nologin -M

#Configure nginx
[[email protected] ~]# vim /etc/nginx/nginx.conf 
user  www;
[[email protected] ~]# vim /etc/nginx/nginx.conf 
user  www;

#Start nginx
[[email protected] ~]# systemctl start nginx && systemctl enable nginx && systemctl status nginx
[[email protected] ~]# systemctl start nginx && systemctl enable nginx && systemctl status nginx

Synchronize LB01 configuration to lb02


[[email protected] ~]# scp /etc/nginx/conf.d/* 172.16.1.5:/etc/nginx/conf.d/
[[email protected] ~]# scp /etc/nginx/proxy_params 172.16.1.5:/etc/nginx/

Test lb02 load balancing

[[email protected] ~]# nginx -t && systemctl restart nginx

#Configure hosts test
10.0.0.5 linux.wp.com

3、 Configure four layer load balancing

Four layer load balancing syntax

Syntax:	stream { ... }
Default:	—
Context:	main

#Example: the four layer load balancing stream module is at the same level as the HTTP module and cannot be configured in http
stream {
    upstream backend {
        server backend1.example.com:12345 weight=5;
        server 127.0.0.1:12345            max_fails=3 fail_timeout=30s;
    }

    server {
        listen 12345;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass backend;
    }
}

Configure nginx master profile

[[email protected] ~]# vim /etc/nginx/nginx.conf
#Comment all content of HTTP layer
user  www;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
#Add an include file
include /etc/nginx/conf.c/*.conf;
#http {
#    include       /etc/nginx/mime.types;
#    default_type  application/octet-stream;
#    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                      '$status $body_bytes_sent "$http_referer" '
#                      '"$http_user_agent" "$http_x_forwarded_for"';
#    access_log  /var/log/nginx/access.log  main;
#    sendfile        on;
#    #tcp_nopush     on;
#    keepalive_timeout  65;
#    #gzip  on;
#    include /etc/nginx/conf.d/*.conf;
#}

Configure four layer load balancing

#Create directory
[[email protected] ~]# mkdir /etc/nginx/conf.c

#Disposition
[[email protected] ~]# vim /etc/nginx/conf.c/linux.lb4.com.conf
stream {
    upstream lbserver {
        server 10.0.0.4:80;
        server 10.0.0.5:80;
    }

    server {
        listen 80;
        proxy_pass lbserver;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
    }
}

#Start nginx
[[email protected] ~]# nginx -t && systemctl start nginx

#Configure hosts access
10.0.0.6 linux.lb4.com

Layer 4 load balancing configuration log

#There is no access log for layer 4 load balancing, because in the configuration of nginx.conf, the log format of access is configured under HTTP, while the configuration of layer 4 load balancing is outside HTTP;

#If logs are required, they need to be configured under stream
[[email protected] ~]# vim /etc/nginx/conf.c/linux.lb4.com.conf
stream {
	log_format  proxy '$remote_addr $remote_port - [$time_local] $status $protocol '
                  '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"';
    access_log /var/log/nginx/proxy.log proxy;

    upstream lbserver {
        server 10.0.0.4:80;
        server 10.0.0.5:80;
    }

    server {
        listen 80;
        proxy_pass lbserver;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
    }
}

#View all web server logs
[[email protected] ~]# tail -f /var/log/nginx/access.log
[[email protected] ~]# tail -f /var/log/nginx/access.log

4、 Layer 4 load port forwarding

Request 5555 port for load balancing and jump to 22 port of web01

#Simple configuration
stream {
	server {
        listen 5555;
        proxy_pass 172.16.1.7:22;
	}
}

#General configuration
stream {
    upstream ssh_7 {
        server 10.0.0.7:22;
    }

    server {
        listen 5555;
        proxy_pass ssh_7;
    }
}

#Testing
[D:\~]$ ssh [email protected]:5555
Successful jump

6666 port requesting load balancing, jump to 172.16.1.51:3306


stream {
    upstream db_51 {
        server 172.16.1.51:3306;
    }
    
    server {
        listen 6666;
        proxy_pass db_51;
    }
}

Load balancing of database slave database


stream {
    upstream dbserver {
        server 172.16.1.51:3306;
        server 172.16.1.52:3306;
        server 172.16.1.53:3306;
        server 172.16.1.54:3306;
        server 172.16.1.55:3306;
        server 172.16.1.56:3306;
    }
    
    server {
        listen 5555;
        proxy_pass dbserver;
    }
}

summary

This is the end of this article on nginx four-tier load balancing configuration. For more information about nginx four-tier load balancing, please search previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

The real problem of Alibaba IOS algorithm can’t hang up this time

More and more IOS developers continue to enter the peak of job hopping in 2020 Three main trends of interview in 2020: IOS bottom layer, algorithm, data structure and audio and video development Occupied the main battlefield. Data structure and algorithm interview, especially figure, has become the main reason for the failure of most first-line […]