Nginx reverse proxy + load balancing

1. The concept of reverse agency

Reverse proxy means that the client does not know that there is no content on the proxy server. For the client, the proxy server is the original server, and the client does not need to make any special settings. The client sends a general request to the content in the namespace of the reverse proxy server, and then the reverse proxy server judges where to transfer the request, And return the obtained content to the client as if it were his own

2. Forward agency concept

The forward proxy is a server between the client and the original server. In order to obtain content from the original server, the client sends a request to the proxy and specifies the target, and then the proxy forwards the request to the original server and returns the obtained content to the client. The client must make some special settings to use the forward proxy

3. Configuration of reverse proxy server

NGINX proxy server 192.168.72.149

web1: 192.168.72.150

web2: 192.168.72.151

Restart nginx

Configuring web2 192.168.72.151

See that the ip address of the reverse proxy server is not the ip address of the client

[root@localhost conf]# tail -f /usr/local/nginx/logs/access.log 
192.168.72.1 - - [06/Jun/2021:04:44:54 -0400] "GET / HTTP/1.1" 502 537 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"
192.168.72.1 - - [06/Jun/2021:04:45:51 -0400] "GET / HTTP/1.1" 502 537 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"
192.168.72.1 - - [06/Jun/2021:04:45:52 -0400] "GET / HTTP/1.1" 200 12 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"
192.168.72.1 - - [06/Jun/2021:04:46:23 -0400] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"
192.168.72.1 - - [06/Jun/2021:04:46:25 -0400] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"
192.168.72.1 - - [06/Jun/2021:04:46:26 -0400] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"
192.168.72.1 - - [06/Jun/2021:04:46:26 -0400] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"
192.168.72.1 - - [06/Jun/2021:04:46:40 -0400] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"

Configure web2

[root@localhost ~]# yum install nginx 
[root@localhost ~]# vi /usr/share/nginx/html/index.html 

[root@localhost ~]# cat  /usr/share/nginx/html/index.html 
hello word1111
[root@localhost ~]# nginx 
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in us

4. Load balancing

1. Description of upstream load balancing module

Upstream is the http upstream module of nginx. This module realizes the load balancing from the client ip to the back-end server through a simple scheduling algorithm, and specifies a load balancing name through the upstream instruction. This name can be specified arbitrarily and can be called directly where needed later.

2.upstream supports load balancing algorithm

The load balancing module of nginx currently supports four scheduling algorithms 1 2 Ip_ hash 3. Fair 4. url_ hash

1. Polling: each request is allocated to different back-end servers in chronological order. If the back-end server goes down, the faulty server will be eliminated automatically, so that the user's access will not be affected. Weight specifies the weight value. The greater the value, the greater the probability of access. This depends on the configuration level of the back-end server

This problem is solved by allocating a valid hash for each visitor's request to access the same session from the backend server

3.fair: This is a more intelligent load balancing algorithm than the previous two. This algorithm can intelligently balance the load according to the length of page consignment and loading time, that is, allocate the load according to the response time of the back-end server

4.url_hash: this method allocates requests according to the hash result of the access URL, so that each URL is directed to the same back-end server, which can further improve the efficiency of the back-end cache server

5. Status parameters supported by upstream

1.max_ Failures: the number of times the request is allowed to fail. The default value is 1. When the maximum number of times is exceeded, it will be returned

2.proxy_ next_ Error in upstream module definition.

3.Fail_timeout: after experiencing max_ The time when the service is suspended after failures. Max_ Failures and failures_ Use with timeout

4.Weight: set the number of visits

6. Load balancing configuration

Then stop a web1 server and continue to access the reverse proxy server in the browser. It is found that it can only access the web2 server. Then start web1 and find that both web1 and web2 are good. This indicates that the health check configuration of nginx is successful

 

 

 

Added by Revos on Mon, 31 Jan 2022 10:03:50 +0200