1. Concept of load balancing

1.1 What is Load Balancing

Load Balancing is a computer technique used to distribute Load among multiple computers (clusters of computers), network connections, cpus, disk drives, or other resources in order to maximize resource usage, maximize throughput, minimize response times, and avoid overload.

In simple terms, Load balancing refers to balancing the Load (work tasks, access requests) and allocating it to multiple operation units (servers, components) for pointing. It is the ultimate solution for high performance, single point of failure (high availability) and scalability (horizontal scaling).

1.2 Why Is Load Balancing Required

In our life, we often have to go to some crowded places, such as railway stations, cinemas, banks and so on. Whether buying tickets or queuing for admission, these places will generally set up multiple service Windows or entrances. Without guidance, most of the time the nearest window or entrance will be crowded with people, while those farther away will be less crowded.

In view of the above life situation, it is actually a waste of resources, because how to properly disperse the queuing people to each window or entrance will greatly shorten the queuing time. So, for the construction of the website or system is the same, in order to improve the service capacity of the website, many websites have adopted cluster deployment. Just like a movie theater with multiple entrances, a coordinator is needed to evenly distribute requests from users to different servers.

1.3 Load Balancing Classification

Let’s review the OSI seven-layer model: OSI is an open reference model for interconnection of communication systems, and it is a very well defined protocol specification. The OSI model has seven layers, each of which can have several sub-layers. The seven layers of OSI from top to bottom are:

  • 7 application layer
  • 6 the presentation layer
  • 5 the session layer
  • 4 the transport layer
  • 3 network layer
  • 2 Data link layer
  • 1 the physical

In each of the seven layers, the high level is dependent on the low level. The higher the level, the easier it is to use. The top layers (layers 7, 6, 5, and 4) define the functionality of the application, while the bottom three layers (layers 3, 2, and 1) focus on end-to-end data flow over the network.

Concepts related to computer networks:

  • TELNET, HTTP, FTP, NFS, SMTP, and DNS belong to layer 7 application layer concepts.
  • TCP, UDP, and SPX are concepts of layer 4 transport layer.
  • IP and IPX are layer 3 network concepts.
  • Concepts such as ATM and FDDI belong to layer 2 data link layer.

After understanding the seven-layer model of network protocols, let’s look at load balancing. One thing we can make clear is that load balancing is about transport over the network. And in the process of network transmission, so these seven layers are bound to open.

Therefore, load balancing can be classified according to the implementation of load balancing technology at different levels of the OSI seven-layer model.

Common implementation methods, mainly in the application layer, transmission layer, network layer and data transmission layer. So, load balancing at the application layer is usually called layer 7 load balancing, and load balancing at the transport layer is called layer 4 load balancing.

It can be roughly divided into the following mechanisms, of which the most commonly used are layer 4 and layer 7 load balancing:

Layer 2 Load Balancing

Generally, virtual MAC addresses are used. The load balancing server provides a VIP (virtual IP address). The servers that do not pass through the cluster use the same IP address, but the MAC addresses of the servers are different. After receiving the request, the load balancing server rewrites the destination MAC address of the packet and forwards the request to the destination host for load balancing.

Layer 3 load balancing

Generally, a virtual IP address is used. Similar to Layer-2 load balancing, a load balancing server still provides a VIP (virtual IP address), but different machines in the cluster use different IP addresses. After receiving the request, the load balancing server forwards the request to different real servers using IP addresses based on different load balancing algorithms.

Layer 4 load balancing

Use the virtual IP address + port mode. Layer 4 load balancing works at the transport layer of the OSI layer 7 model. At the transport layer, only TCP/UDP protocols are available. These two protocols contain source and destination IP addresses as well as source and destination port numbers. After receiving the request from the client, the Layer-4 load balancing server forwards the traffic to the application server by modifying the IP address and port number of the packet.

Layer 7 load balancing

Use a virtual URL or host name. Layer-7 load balancers work at the application layer of the OSI 7-layer model and have many protocols, such as HTTP, RADIUS, and DNS. Layer 7 loads can be loaded based on these protocols. There is a lot of interest in these application layer protocols. For example, the load balancing of the same Web server can be determined by the URL, browser category and language of the seven layers in addition to the IP + port number.

This is because load balancers are often referred to as layer 4 switches or layer 7 switches. Here for the difference between the fourth layer and the seventh layer to say more in-depth:

Technical principle difference

  • Layer 4 load balancing determines the internal server based on the destination IP address and port in the packet and the server selection mode set by the load balancing device.

Taking TCP as an example, when the load balancing device receives the first SYN request from the client, it selects an optimal server in the preceding manner, changes the destination IP address of the packet to the IP address of the back-end server, and directly forwards the packet to the server. The TCP connection is established, that isThe three-way handshake is directly established between the client and server, and the load balancer only performs a forwarding action like a router. In some deployment scenarios, the original source IP address of the packet may be changed when the packet is forwarded to ensure that the packet can be correctly returned to the load balancer.

  • Layer 7 load balancing, also known as “content exchange”, mainly determines the internal server selected by the meaningful application layer content in the packets and the server selection mode set by the load balancing device.

Take TCP as an example. If the load balancing device needs to select a service server based on the actual application layer content, the load balancing device can only receive the actual application layer content packets sent by the client after the connection is established between the final server and the client (three-way handshake). Together with the server selection method set by the load balancing device, the final internal server is selected. Load balancing in this case is more like a proxy server. TCP connections are established between load balancers and front-end clients and back-end servers respectively. Therefore, the layer-7 load balancing has higher requirements on load balancing devices, and the layer-7 load balancing capability is lower than that of the layer-4 deployment mode.

Differences in Application Scenarios

Layer 7 makes the application more intelligent and secure, at the cost of more complex design and configuration, because it can modify and process user requests arbitrarily. So whether it’s necessary to use seven layers of load balancing is a tradeoff.

At present, the seven-layer load balancing mainly focuses on the application of HTTP protocol, so its application scope is mainly a large number of websites or internal information management platform based on B/S development system. Layer-4 load balancing corresponds to other TCP applications.

1.4 Load Balancing Tool

There are many open source load balancing tools or software on the market that are based on the aforementioned solutions, most of which work at layers 7 and 4. Nginx, LVS and HAProxy are the three most widely used load balancing software.

LVS: mainly used for four-layer load balancing

LVS (Linux Virtual Server), also known as Linux Virtual Server, is a free software project initiated by Dr. Wensong Zhang, using LVS technology to achieve the following objectives: LVS load balancing technology and Linux operating system to achieve a high-performance, highly available server cluster architecture, it has good reliability, scalability and operability. Thus achieving optimal service performance at low cost.

Nginx: mainly used for layer 7 load balancing

Nginx is a web server that can discover protocol links for proxy HTTP, HTTPS, SMTP, POP3, and IMAP, as well as a load balancer and an HTTP cache.

Nginx currently supports both ngx_STREAM_CORE_module and ngx_HTTP_upstream_module load balancing at layers 4 and 7, but because LVS is so well known for doing load balancing at layers 4, So there are not many people who use Nginx to do load balancing, and there are very few people who use Nginx to do load balancing.

HAProxy: used for layer 7 load balancing

HAPorxy is free and open source software written in C that provides high availability, load balancing, and APPLICATION proxies based on TCP and HTTP.

1.5 Load Balancing Algorithm

Load balancing algorithms are used by load balancing servers to determine which real servers to forward requests to. Load balancing is the core of a load balancing server.

Load balancing algorithm can be divided into two types: static load balancing algorithm and dynamic load balancing algorithm.

Static load balancing algorithms include polling, ratio, and priority

  • Round Robin: The sequential loop requests a sequential loop to connect to each server once, and when a server fails at layer 2 through layer 7, big-IP removes it from the sequential loop queue and does not participate in the next Round Robin until it recovers.
  • Ratio: Each server is assigned a weighted Ratio based on which user requests are assigned to each server. When a layer 2 to 7 failure occurs on one of these servers, big-IP takes it out of the server queue and does not participate in the next user request allocation until it recovers.
  • Priority: Big-ip assigns requests to the highest priority server group (within the same group, using a polling or ratio algorithm to allocate requests from users). Big-ip sends requests to the lower priority server group only when all servers in the highest priority fail. In this way, users are actually provided with a hot backup mode.

Dynamic load balancing algorithm includes: minimum number of connections, fastest response speed, observation method, prediction method, dynamic performance allocation, dynamic server replenishment, quality of service, service type, rule mode

  • Least Connection: Pass new connections to the server that does the Least Connection processing. When a layer 2 to 7 failure occurs on one of these servers, big-IP removes it from the server queue and does not participate in the allocation of the next user request until it recovers.
  • Fastest mode: Transfers connections to the Fastest servers. When a layer 2 to 7 failure occurs on one of these servers, big-IP takes it out of the server queue and does not participate in the next user request allocation until it recovers.
  • The server is selected for the new request based on the optimal balance of connection number and response time. When a layer 2 to 7 failure occurs on one of these servers, big-IP takes it out of the server queue and does not participate in the next user request allocation until it recovers.
  • Predictive mode (Predictive mode) : Big-IP analyzes the collected server performance data and selects the server with the best performance in the next time slice to respond to user requests. (Detected by BIG-IP).
  • Dynamic Ratio-APM: Big-IP collects performance parameters of applications and application servers to dynamically adjust traffic allocation.
  • Dynamic Server Act. : When the number of primary Server farms is reduced due to a failure, backup servers are dynamically added to the primary Server farms.
  • Quality of service (QoS) : Data flows are allocated according to different priorities.
  • Service Type (ToS) : Allocates data flows based on load balancing of different service types (identified in Type of Field).
  • Rule mode: Users can set guidance rules for different data flows.

The above is the current mainstream algorithm for load balancing. Different load balancing servers choose different algorithms.

1.6 Comparison between software LB and Hardware LB

The load balancing service provided by software and the load balancing service provided by hardware are both widely used.

Software load balancing services generally run on standard x86 servers, which are usually cheaper than hardware load balancing devices and can better adapt to popular cloud services.

Hardware load balancing services require special equipment support and may be better than software load balancing services in terms of performance and security.

I’ve seen software load balancing, but I’m going to expand a little bit on hardware load balancing.

What is hardware load balancing?

The hardware LB solution is to install a load balancer directly between the server and the external network. The load balancer is usually called a load balancer. The dedicated device performs the task of forwarding network requests, is independent of the operating system, and has high overall performance, diversified load balancing policies, and intelligent traffic management.

What are the advantages and disadvantages of hardware load balancing?

Advantages: Directly connected to the switch, strong ability to process network requests, independent of the system, can be strong load. Can be applied to a large number of facilities, adapt to large volume, simple to use. Disadvantages: High cost and redundant configuration. Even though network requests are distributed to server clusters, load balancing facilities are single-point configurations; Unable to master server and usage status.

What are the precautions and application scenarios?

Note that hardware load balancing only focuses on network judgment and does not consider service systems and applications. Sometimes the system processing capacity reaches the bottleneck, but the network is not abnormal. The hardware load balancer does not notice the application server exception, and the traffic continues to flow to the application server. Usage scenarios, general applications and PV hundreds of thousands or even millions of Internet applications, general software load balancer such as Nignx processing concurrency generally in 1-2W, is not enough to support such a large network request, so before it usually prevent hardware load balancer like F5 to help control network requests. If the general Internet enterprise network request number is about 1W, you can consider using Nignx (software load balancer) is enough to meet the current business.

What does a hardware load balancer do?

At present, there are NetScaler, F5, Radware, Array and other products on the market, the basic implementation principle is roughly the same, we here use F5 as an example to give you a simple explanation, can be seen.

Multi-link load balancing

Multiple ISPs (network service providers) access links must be arranged and configured for key services to ensure the quality of network services. If one ISP goes out of service or the service is abnormal, then another ISP can be used to replace the service, improving the availability of the network. Different ISPs have different AUTONOMOUS domains. Therefore, you need to consider INBOUND and OUTBOUND.

  • INBOUND, the request information from the network. F5 binds the public addresses of two ISPs to resolve DNS resolution requests from the two ISPs. F5 can send DNS packets based on the server status and response, or establish DNS connections over multiple links.
  • OUTBOUND, the response information returned to the requester. F5 allocates traffic to different network interfaces and performs source ADDRESS NAT (NETWORK address translation), that is, translates IP addresses into source request addresses. The interface address can also be automatically mapped to ensure that the packet is correctly received by the source when it is returned.

Firewall load balancing

In the presence of a large number of network requests a single firewall’s ability is limited, and required data with firewall itself into with out, in order to solve the problem of multiple firewalls load balancing, firewall F5 proposed load balance of firewall “sandwich” firewall might be to monitor the user’s session of bidirectional data flow, to determine the legitimacy of the data. If multiple firewalls are used for load balancing, bidirectional data of a user session may be processed on multiple firewalls. However, if no user session information is displayed on a single firewall, the data is considered invalid and therefore discarded. Therefore, layer 4 switches should be set up at both ends of each firewall to distribute traffic and maintain the integrity of user sessions, so that the same user session is processed by one firewall. F5 coordinates the configuration and implementation of the above solution, sandwiched “switch”, “firewall” and “switch” together like a sandwich.

Server load balancing

  • For application servers, F5 can be configured to implement load balancing. F5 can check the health status of the servers and remove them from the load balancing group if faults are found.
  • F5 has a real IP address for the Internet and a virtual IP address for each server on the Intranet for load balancing and management. Therefore, it can provide SLB service for a large number of TCP/ IP-based network applications.
  • Define different server groups based on service types.
  • Traffic is directed to the corresponding server based on different service ports. VIP user requests can even be processed in a special way, and such requests can be imported to high-performance servers so that VIP customers can get the best service response.
  • Traffic is directed to a specific server based on what the user accesses.

availability

  • High availability and millisecond switching when working in dual redundancy mode.
  • Device redundancy Power module Optional.
  • Each device monitors the electrical frequency of other devices through heartbeat cables, and automatically switches when faults are found.
  • Link redundancy: The system detects link faults in real time. Once a fault is found, traffic is switched over automatically and the process is transparent.
  • Server redundancy: The heartbeat detection is performed on the server. Once a fault is detected, the server is removed from the server list immediately. After the server recovers, it is added to the server list again.

security

  • Site security Protection
  • Remove idle connections to prevent denial of service attacks
  • Can perform source route tracing to prevent IP spoofing
  • Reject SYN without ACK buffer acknowledgment to prevent SYN attacks
  • [Fixed] Reject Teartop and Land attacks Protect yourself and your server from ICMP attacks

System management

  • Provides browser-level management software and Web graphical user interface.

Conclusion: For Internet applications with high concurrency and high traffic volume, hardware load balancer can be considered as the access layer to assist the software load balancer at the agent layer to carry out load balancing. The hardware load balancer is independent of the operating system, processes a large volume of traffic, and costs a lot. From the function to support multi – link, multi – server, multi – firewall load balancing, in availability and security also has a good performance.

2. Nginx load balancing

2.1 Introduction to Nginx Load Balancing

Strictly speaking, Nginx is only used as an Nginx Proxy reverse Proxy, because the reverse Proxy function performs load balancing. Nginx Forward proxy and Reverse Proxy

Nginx load balancing supports both ngx_STREAM_CORE_module (ngX_HTTP_upstream_module) and ngx_HTTP_upstream_module (ngX_HTTP_upstream_module), but LVS is widely used in load balancing at layer 4. So Nginx’s four-tier load balancing is not used very much.

  • The function of LVS is only to forward (or rewrite) and transfer the request packet. The obvious feature of DR mode is that the request received from the node server under the load balancer is still from the real user accessing the client of the load balancer.

  • And reverse proxy, the reverse proxy to receive access to the user’s request, will re-launch the proxy user requests to a node under the proxy server, finally returned to the client users, the data in the node server, node server client user access is the reverse proxy server, rather than the real web site visitors.

Nginx uses the same upstream configuration for both layer 4 and layer 7, except that it uses a different upstream. The difference between layer 4 and layer 7 does not affect our configuration. This section uses layer 7 load balancing as an example.

However, it is important to note that when using Nginx as a layer 7 load balancer, if Nginx has proxy_cache, Nginx will not forward the request to the node if the resource has been accessed and has been cached. Instead, Nginx will directly return the cache resource. That is, it is possible for Nginx to have configuration caching that does not trigger scheduling.

2.2 Nginx load balancing component module

There are two main components to implement Nginx load balancing:

  • ngx_http_upstream_module 

The load balancing module can realize the load balancing function of the website and the health check of nodes

  • ngx_http_proxy_module 

Proxy proxy module used to forward requests to server nodes or upstream server pools

The upstream module

(1) Upstream module introduction

The upstream module allows Nginx to define one or more groups of node server groups, using the proxy_pass agent to route web site requests to the name of the predefined upstream group:

proxy_pass http://server_pools
Copy the code

Where server_pools is an upstream node server group name.

(2) Upstream configuration case

Example 1: Basic upstream configuration example:

upstream server_pools {
  Upstream: server_pools: upstream: server_pools: upstream: server_pools: upstream: server_pools: upstream: server_pools
  server 192.168.1.251:80 weight=5;
  server 192.168.1.252:80 weight=10;
  server 192.168.1.253:80 weight=15;
  The # server keyword is fixed and can be followed by a domain name or IP address. If no port is specified, the default port is 80. Weight stands for weight, and the higher the value, the more requests will be assigned.
}
Copy the code

Example 2: A more complete upstream configuration:

upstream blog_pools {
  server 192.168.0.223;   # This line tag is equivalent to downlink
  server 192.168.0.224:80 weight=1 max_fails=1 fail_timeout=10s;       # This line is equivalent to the previous line. The extra part of this line is the default configuration.
  server 192.168.0.225:80 weight=1 max_fails=2 fail_timeout=20s backup;
  You can add a number of parameters to the end of the server
}
Copy the code

(3) Upstream module parameters

The serial number Configuration items instructions
1 server The RS configuration behind the load can be IP or domain name
2 weight The weight of the request server. The default value is 1. The larger the weight, the larger the proportion of requests accepted
3 max_fails The number of failed nginx attempts to connect to the back-end host. The default value is 1. This value must be used with proxy_next_upstream, fastcgi_next_upstream, and memcached_next_upstream. When nginx receives a status code from the backend server, it forwards the request to a working backend server, such as 404, 502, 503
4 fail_timeout After the number of failures defined by max_fails, the interval between the next check is 10s by default
5 backup Hot backup configuration, which indicates that this server acts as a backup server and forwards requests to it if the primary servers all go down. Backup can be used to do the sorry server effect of sending a “sorry, website temporarily unavailable” message when the backend server goes down. The advantage of this is that the user experience is better than if a page with a specific HTTP status code is configured to have a similar effect, instead of a long timeout wait, an error message is returned immediately.
6 down Indicates that the current server does not participate in load balancing temporarily, that is, the server is marked as unavailable and can be used with ip_hash to achieve the effect of grayscale publishing.
7 max_conns This parameter is added after version 1.11.5. It refers to the maximum number of concurrent active connections to a back-end server.
upstream web_pools {
  server linux.example.com weight=5; 
  server 127.0.0.1:8080 max_fail=5 fail_timeout=10s;
  # If five consecutive checks fail, re-check 10 seconds later.
  server linux.example.com:8080 backup; 
  # Specify a backup server. Action: Forwards requests to the server when all of the above servers are inaccessible.
}
Copy the code

Http_proxy_module module

(1) Introduction to proxy_pass instruction

The proxy_pass directive belongs to the ngx_HTTP_proxy_module module, which can forward requests to another server. In actual reverse proxy work, the location function matches the specified URL, and requests that match the URL are thrown to the defined upstream node pool via proxy_pass.

(2) Use cases of proxy_pass

location /web/ {
	proxy_pass http://127.0.0.1/abc/;
}
Copy the code

Toss a request matching the URL web to http://127.0.0.1/abc/

(3) Http_proxy module parameters

The serial number Configuration items instructions
1 proxy_set_header Set the HTTP request header to the back-end server node. For example, it is possible to have the server node on the proxy back end get the real IP address of the user accessing the client
2 client_body_buffer_size Used to specify the client request topic buffer size
3 proxy_connect_timeout Indicates the timeout time for the connection between the reverse proxy and the back-end server, that is, the time for initiating a handshake and waiting for a response
4 proxy_send_timeout Indicates the data return time of the server after the proxy, that is, within the specified time, the backend server must complete the data transfer, otherwise, nginx will disconnect the connection
5 proxy_read_timeout Set the time it takes for nginx to get information from the proxy’s backend server. This is how long it takes nginx to wait for the backend server to respond to a successful connection
6 proxy_buffer_size Sets the buffer size, which by default is equal to the size of the proxy_buffers directive
7 proxy_buffers By setting the number and size of the buffers, nginx will place the response information from the proxy’s back-end server in the cache
8 proxy_busy_buffers_size This parameter is used to set the size of proxy_buffers that can be used when the system is busy. The recommended value is proxy_buffers * 2
9 proxy_tem_file_write_size Specifies the size of the proxy cache temporary file

2.3 Nginx load balancing scheduling algorithm

Rr Polling (default mode)

The default scheduling algorithm assigns servers to different back-end servers one by one based on client requests. The faulty servers are automatically removed from the node server pool.

upstream server_pools {
    server 192.168.1.251;
    server 192.168.1.252; }...location / {
    proxy_pass http://server_pools;
}
Copy the code

Note: For clusters with different server performance, this algorithm may cause problems such as unreasonable resource allocation.

WRR Weighted polling (weight)

The weight is added to the RR polling algorithm. The weight is proportional to user access. The larger the weight value is, the more requests will be forwarded.

upstream server_pools {
    server 192.168.1.251 weight=5;
    server 192.168.1.252 weight=10;
}
Copy the code

Weighted polling is used in clusters with unequal server performance to rationalize resource allocation.

Ip_hash (sticky session)

Each request is allocated according to the hash result of the access IP address. Each visitor regularly accesses one back-end server, which can solve the problem that sessions are not shared. However, due to the non-consistent Hash algorithm, once the number of nodes changes, all the allocation mappings will change, and the expected effect cannot be achieved in the case of unstable node configuration.

upstream server_pools {
    ip_hash;
    server 192.168.1.251;
    server 192.168.1.252;
}
Copy the code

In this case, if the user has logged in and the requests sent by the user are allocated to server A, server A breaks down suddenly and the requests are forwarded to server B. However, because the Session is not shared, server B cannot read the login information of the user directly to perform other operations.

Url_hash (Web cache node)

Requests are allocated based on the hash results of urls accessed, directing each URL to the same backend server. Therefore, it is very suitable for use when the node server is a cache server, and can provide a large cache hit ratio. However, due to the non-consistent Hash algorithm, once the number of nodes changes, all the allocation mappings will change, and the expected effect cannot be achieved in the case of unstable node configuration.

upstream server_pools {
    hash $request_uri;
    hash_method crc32;
    server 192.168.1.251;
    server 192.168.1.252;
}
Copy the code

Nginx does not support hash algorithms prior to version 1.7.2. If you want to use this algorithm in older versions, you need to install a third-party hash module. Add two lines of hash $request_URI and hash_method crc32 to the upstream configuration.

Fair (Dynamic Scheduling Algorithm)

The response time of the root backend node server is used to allocate requests, with priority given to those with short response times.

upstream server_pools {
    server 192.168.1.251;
    server 192.168.1.252;
    fair;
}
Copy the code

Nginx itself does not support the fair algorithm, so if you need to use it, you need to install the third-party upstream_fair module.

Least_conn (Dynamic Scheduling Algorithm)

Requests are allocated based on the number of connections to the back-end node server, with those with fewer connections being allocated first.

Least_conn can be used in scenarios with a large number of long connections (such as game servers) to help node servers evenly split connections and keep the load as equal as possible.

The Least_conn algorithm is simple. It first iterates through all the nodes, compares the number of connections, and selects the node with the smallest value. If there are multiple nodes with the smallest number of connections, then weighted polling algorithm is applied to them. Can be used in combination with the weight weight value.

upstream balabala {
     least_conn;
     server 192.168.1.251;
     server 192.168.1.252;
}
Copy the code

consistent_hash

Consistent_hash is a static scheduling algorithm generated based on the consistent Hash algorithm. It is an updated version of IP_hash and URl_hash.

Consistent_hash also assigns nodes to each request based on the client IP address and the Hash result (optional) of the requested URL, so that requests with the same Hash value can always be assigned to the same node. The unique feature of consistent_hash is that, The consistency Hash algorithm provides it with pre-failure and consistency preservation effects. In other words, even if a node breaks down during use, other Hash values and corresponding nodes will not be affected, and the original mapping relationship can be maintained. The Hash values of a failed node are mapped to the next node on the ring for failover.

The configuration is similar to url_hash. Add a line consistent_hash $request_URI to upstream. Can. Again, this parameter can be replaced with something else.

upstream balabala {
    consistent_hash $request_uri;
    server 192.168.1.251;
    server 192.168.1.252;
}
Copy the code

2.3 Nginx Load Balancing Configuration Example

Implementation effect

Enter the address [http://www.test.com](http://www.test.com) in the browser to achieve load balancing (two servers can be evenly accessed).

The preparatory work

(1) Prepare three Nginx servers as follows

The host name The IP address instructions
web01 192.168.1.251 Nginx Web01 server
web02 192.168.1.252 Nginx Web02 server
lb 192.168.1.253 Nginx load balancing server

(2) Nginx is installed on all three servers

Nginx installation is not mentioned here, there are many references on the web.

(3) Configure the Web server for the test

The configuration is performed on Web01 and Web02

[root@web01 nginx]# cat conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
	      access_loglogs/access_www.log main; }}Copy the code

Create test file data

[root@web01 ~]# cd /usr/local/nginx/html/
[root@web01 html]# mkdir www
[root@web01 www]# echo "`hostname` www" > index.html
Copy the code

View the contents of the created file:

[root@web01 www]# cat index.html
web01 www
Copy the code

Then start the nginx service.

(4) With Nginx load balancing server

[root@lb01 nginx]# cat conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream www_server_pools {         The Web server pool is defined here and contains 251,252 Web nodes
	  server 192.168.1.251:80 weight=1;
	  server 192.168.1.252:80 weight=1;
	}
    server {            # define proxy load balancing domain name virtual host here
        listen       80;
        server_name  www.test.com;
        location / {
						proxy_pass http://www_server_pools;     # visit www.test.com and send requests to nodes in www_server_pools}}}Copy the code

(5) Domain name resolution

Since this is not a real environment, the domain name www.test.com is used for testing, so the resolution of www.test.com can only be set in the hosts file.

MAC:

sudo vi  /etc/hosts
Copy the code

Windows:

C:\Windows\System32\drivers\etc\hosts
Copy the code

Add 192.168.1.253 www.test.com at the end

The validation test

Open a browser and visit www.test.com. You can find that all requests are allocated to Web01 (192.168.1.251) and web02 (192.168.1.252) by the load balancing server (192.168.1.253).

That’s how to configure Nginx as a load balancer. Please follow my wechat official account for more content.

Some pictures from the network, copyright to the original author, delete.