[learning notes] overview of Nginx core

Posted by Daniel0 on Tue, 08 Feb 2022 09:10:41 +0100

Nginx

introduce

Nginx is a high-performance HTTP and reverse proxy web server. It has a very powerful ability to handle high concurrency and can withstand the test of high load. Some reports show that it can support up to 50000 concurrent connections.

Its characteristics are less memory and concurrent capability. In fact, nginx's concurrent capability is better in the same type of web server. Chinese mainland users use Baidu website: Baidu, Jingdong, Sina, NetEase, Tencent, Taobao, etc. There are 12.18% of the active websites in the world, about 22.2 million websites.

Nginx is a service with very simple installation, very concise configuration files (perl syntax can also be supported) and very few bugs. Nginx is particularly easy to start, and can almost run 7 * 24 without interruption. It doesn't need to be restarted even if it runs for several months. You can also upgrade the software version without interruption of service.

Nginx code is completely written from scratch in C language. Official data tests show that it can support responses with up to 50000 concurrent connections.

Reverse proxy

Speaking of reverse proxy, let's first talk about forward proxy. If the Internet outside the LAN is imagined as a huge resource pool, the clients in the LAN need to access the Internet through the proxy server. This proxy service is called forward proxy.

Straight point: for example, we usually access things on the Internet through VPN, game accelerator and other operations or tools, which is called forward proxy. The client is operation aware.

Reverse proxy: the client is not aware (no configuration operation is required). We send the request to the reverse proxy server through the client, and then the reverse proxy server selects the request target server (which involves load balancing and server cluster) to obtain the data, and then return it to the client. For outsiders, the reverse proxy server and the target server are a whole, which is to expose the reverse proxy server address and hide the real target server address.

When used, it is mainly proxy_pass to specify the forwarding address

location{
	proxy_pass  host
}

load balancing

When the client requests gradually increase, we need to consider the pressure of the server. If too many requests exceed the maximum bearing capacity of the server, it may lead to server downtime. Therefore, we need to disperse these requests, and we also need the cluster backup of the server to enhance the robustness of the whole system.

There are four load balancing algorithms.

Suppose we set up four clusters of background servers (here I'm just an example, write the address casually)

upstream mybackserver{
	server 192.168.12.14:8001;
	server 192.168.12.15:7001;
	server 192.168.12.16:6001;
	server 192.168.12.17:7801;
}

Polling (default)

As the literal meaning is, polling is to query in turn, so we only need to point mybackserver to proxy when configuring_ Pass now

server  {
	listen       8888;
	server_name  localhost;
	root /usr/local/nginx/fileserver/;
	location / {
		proxy_pass  http://mybackserver;
		index  index.html index.htm;
	}
}

weight

This method mainly realizes the weight distribution through the weight keyword. Add weight=X after the request address of the background server cluster configured earlier.

upstream mybackserver{
	server 192.168.12.14:8001;
	server 192.168.12.15:7001;
	server 192.168.12.16:6001;
	server 192.168.12.17:7801;
}

The higher the weight, the more requests will be distributed. Here, it can be seen that if there are 10 requests, server1 forwards once, server2 forwards twice, server3 forwards three times and server4 forwards four times. If you do not write weight, the default value of weight is 1.

ip_hash and url_hash

ip_hash: calculate the hash according to the IP address requested by the client, and then match the calculated hash to a specific back-end server to ensure session sharing.

The rule is to add IP to the cluster configuration_ Hash is enough.

upstream mybackserver{
	ip_hash;
	server 192.168.12.14:8001;
	server 192.168.12.15:7001;
	server 192.168.12.16:6001;
	server 192.168.12.17:7801;
}

url_hash: calculate the hash according to the URL address requested by the client, and then match the calculated hash to a specific back-end server.

URL to use_ Hash is used in conjunction with cache hit.

upstream mybackserver{
	hash $request_uri;
	server 192.168.12.14:8001;
	server 192.168.12.15:7001;
	server 192.168.12.16:6001;
	server 192.168.12.17:7801;
}

fair

Requests are allocated according to the response time of the back-end server, and those with short response time are allocated first.

upstream mybackserver{
	fair;
	server 192.168.12.14:8001;
	server 192.168.12.15:7001;
	server 192.168.12.16:6001;
	server 192.168.12.17:7801;
}

Dynamic and static separation

Dynamic and static separation here is the independent division of dynamic request and static request.

Static request: refers to the request for some pictures, CSS and text resources.

Dynamic request: it can be understood as background business request, which is forwarded by reverse proxy.

How to use it?

  • Create a folder to store static resources according to the self determined path (assuming that an img folder and an html folder are created under the data file in the changed directory)

  • Configure the location path under the server that forwards the request

    server  {
    	listen       8888;
    	server_name  localhost;
    	root /usr/local/nginx/fileserver/;
    	location / {
    		proxy_pass  http://mybackserver;
    		index  index.html index.htm;
    	}
    	
    	# Picture resources
    	location /img/ {
    		root /data/;
    	}
    	
    	# html resources
    	location /html/ {
    		root /data/;
    	}
    }
    

summary

The above three points are the core of nginx. If you expand later, there are nginx's high availability clusters and so on. I won't study them for the time being. Here's a thought for me. Nginx does reverse proxy forwarding and request load balancing, which is similar to that of the Gateway of microservice? What is the relationship between the two... I'll update it when I figure it out.

Topics: Nginx