Reverse proxy function and load balancing of nginx

Use nginx to implement reverse proxy

Nginx only forwards requests. There are multiple http servers in the background to provide services. The function of nginx is to forward requests to the following servers and decide who to forward the request to.

1 Install tomcat

Create two tomcat instances on a virtual machine to simulate multiple servers.

2 Requirements

Access tomcat running on different ports by accessing different domain names , suitable for distributed deployment

8080.tomcat.com access tomcat running on port 8080

8081.tomcat.com access tomcat running on port 8081

3 The domain name needs to be configured with the host file:

# vim /etc/hosts

4Nginx configuration

 1 upstream tomcat1 {
 2     server 127.0.0.1:8080;
 3     }
 4     upstream tomcat2 {
 5     server 127.0.0.1:8081;
 6     }
 7    server {
 8         listen       80;
 9         server_name  8080.itheima.com;
10 
11         #charset koi8-r;
12 
13         #access_log  logs/host.access.log  main;
14 
15         location / {
16             proxy_pass   http://tomcat1;
17             index  index.html index.htm;
18         }
19 
20         
21     }
22     server {
23         listen       80;
24         server_name  8081.itheima.com;
25 
26         #charset koi8-r;
27 
28         #access_log  logs/host.access.log  main;
29 
30         location / {
31             proxy_pass   http://tomcat2;
32             index  index.html index.htm;
33         }
34 
35         
36     }
37 


If multiple servers provide services under the same domain name, nginx load balancing is required at this time.

Just add a server under an upstream

Set the weight of load balancing

Add at the back: weight=x, the larger the x, the greater the probability of being assigned to the request, as shown in the figure

-----------------------------------------------------------------------------------------------------------------------------------------

 

Description of other options:

1  Node description:
 2  Add to the http node:
 3  
4 #Define  the IP and device status of the load balancing device 
 5  upstream myServer {   
 6  
7      server 127.0 . 0.1 : 9090 down; 
 8      server 127.0 . 0.1 : 8080 weight= 2 ; 
 9      server 127.0 . 0.1 : 6060 ; 
 10      server 127.0 . 0.1 : 7070 backup; 
 11  }
 12  
13 Add
 14  
15 proxy_pass http: // myServer; 
16  
17  upstream The status of each device:
 18  
19  down means that the server before the single does not participate in the load temporarily 
 20  weight The default is 1. The larger the weight, the more load the greater the weight. 
21  max_fails: The default number of allowable request failures is 1. When the maximum number of times is exceeded, the error defined by the proxy_next_upstream module will be returned. 
 22  fail_timeout: The time to pause after max_fails failures. 
23 backup: When all other non-backup machines are down or busy, request the backup machine. So this machine will be the least stressful.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325073878&siteId=291194637