Notes-"Summarize the use of load balancing and nginx and write a demo

Recently I am doing load balancing related things. Encountered some problems, do some summary as follows.

1. Why do you need load balancing?

Let me first talk about why load balancing is needed, because traditional web sites are all clients (countless) + servers (one). If the server is down, the client cannot receive the request, and the problem is serious. So we add a few more servers at this time, and deploy the same business code on each server. If server A is deployed or down, then the client can also request server B and server C. Then there is a problem at this time. The request link URL we assign to the client is unique. How can the client request server B and server C? The answer lies in this load balancing, which can detect that server A is down, and then forward the request to server B or C.

2. What is load balancing?

You can understand load balancing as a hardware product, just use it as a springboard, or directly understand it as a load balancer (you can Baidu), through this springboard to distribute client requests to different servers. Each server provides a single service . Load balancing is divided into 4 layers of load balancing and 7 layers of load balancing. Layer 4 load balancing refers to the fourth layer of the OSI protocol (that is, the transport layer, which can be understood as the level of information transmission), and the 7th layer refers to the first layer of the OSI protocol. Layer 7 (ie application layer, which can be understood as a layer involving business logic), Layer 7 load balancing is built on the basis of Layer 4 load. Generally, we can use Layer 4 load balancing to achieve more reliable request distribution. Up. The realization of 4-layer load balancing is achieved through nginx reverse proxy.

There is an interesting picture that illustrates what a reverse proxy is,

Seriously,

3,demo

1. Download nginx, for this demo is running on windows, if it is running on linux, you can download it yourself http://nginx.org/en/download.html

Link: https://pan.baidu.com/s/1_6iHwFOY4JDrWa2j7yXKCw Extraction code: aqm2 

2. After downloading, unzip the compressed package and place it in a folder with a full English path. Modify the nginx.conf file.

    

2. Modify this configuration file, the meaning of each parameter has been annotated.

worker_processes  1;#工作进程的个数,一般与计算机的cpu核数一致
events {
    worker_connections  1024;#单个进程最大连接数(最大连接数=连接数*进程数) 
}
http {
	upstream  mysite{ 
		server 127.0.0.1:8082 weight=2;#权重,eg:8082 weight=2,8092 weight=1;访问3次,会先访问两次8082端口,再访问一次8092端口
		server 127.0.0.1:8092 weight=1;  
    }
    #当前的Nginx的配置 
    server {
	    #监听80端口,可以改成其他端口。这个端口号是一个虚拟的临时端口号,可自定义未被占用的端口号,
		#来自外部的请求会先通过这个虚拟的8083端口再转发到配置的真正端口上去
        listen       8083;
		#server_name当前服务的域名,如果只有一个server域的话,此参数是不起作用的,可以去掉或者随便定义。只有当配置了多个server的时候,这个参数才会发挥作用
        server_name  localhost;
        location / {
		    #这个参数的值自定义,只需要与upstream后面的名字一致即可,不用顾虑其他
            proxy_pass http://mysite; 
			#不进行重定向
			proxy_redirect off;
			#不隐藏端口号
            proxy_set_header Host $host:$server_port;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header REMOTE-HOST $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			#请求超时时间
			proxy_connect_timeout 1; 
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }       
    }	
}

 Let's explain more redundantly below. I started two springboot applications locally with different tomcat port numbers to simulate two servers. The two applications use the same code to connect to the same database. The two port numbers are 8082 and 8092, but I don't want to expose these two port numbers, so I defined an analog port number 8083. When we are accessing this proxy interface, if it is accessed from the intranet ip address, we can access it through 127.0.0.1:8083. If the content has been mapped by the external network and accessed through the external network domain name, we set the original 8082 port number to 8083 in the external network penetration tool, and it can be accessed through the domain name. There are multiple servers in the back-end, but we simulate multiple servers into one through a simulated port. This is the embodiment of the concept of clustering. This hides the internal network structure, prevents attacks on the real port of the server, makes the real server more secure, and improves the stability of the server.

3. Visit. You need to start nginx before access to achieve the effect of load balancing. Every time you modify the configuration file, you need to reload it. Common cmd commands are as follows, linux commands are Baidu by themselves

Check whether the nginx configuration file format is correct: nginx.exe -t
closes all nginx port occupancy background: taskkill /IM nginx.exe /F
opens nginx load balancing: start nginx
reloads nginx changes (used after changing the configuration file): nginx.exe -s reload
Quit nginx load balancing nginx.exe -s quit

Guess you like

Origin blog.csdn.net/nienianzhi1744/article/details/102552361