Nginx understanding and summary

Version: 1.0.14
Reference Book: "In-depth Understanding of Nginx Module Development and Architecture Analysis 2nd Edition"

First question: what is nginx?
Nginx is a web server that supports high concurrency. It takes advantage of the waiting time to solve a large number of concurrency problems by implementing a few processes.


Which leads to the second question: what is the waiting time?
Ordinary servers open up threads/processes to process requests through a connection. The number of threads is the number of concurrency, so most of the waiting time after web requests is due to the processing and delivery of messages, while nginx only opens a small number of process workers to let them run. Go to the place where blocking may occur, register the event, and tell me after the background server has processed the result. At this time, I will rest. When a new request arrives, the worker will repeat the above operation.
The above is the vague concept of nginx, and then the following article leads to the understanding and summary of nginx through a series of questions.


Question 3: How does Nginx achieve high concurrency under
linux? The limitation of high concurrency has to talk about file descriptors. Under linux, all files are connected. The related operations on file descriptors can be regarded as the process of sending and receiving data from sockets. Then linux The default number of file descriptors that a process can open is only 1024, which means that the maximum concurrency will not exceed 1024. So how to achieve high concurrency? Only modify the kernel parameters. (After modifying the kernel parameters, the ceiling of concurrency is related to the hardware configuration)
/etc/sysctl.conf
and then execute sysctl -p to make the modification take effect



Question 4: What are the advantages of Nginx?
Advantages: (The advantages can also be regarded as one by one problem, and the understanding is completed)
1. Faster response: Under normal circumstances or high concurrency, a single request will get a faster response (related to code understanding)
2. High scalability: It is composed of multiple modules with different levels and different functions (low coupling) (related to the framework)
3. High reliability: The workers are relatively independent, and a new worker will be quickly pulled up to provide services after a worker fails ( Code framework combination)
4. Low memory consumption: 10,000 inactive httpKeep alive connections consume only 2.5MB of memory (Chapter 3, http connection objects)
5. Single machine supports 100,000+ concurrency: (Chapters 8~11)
ps .Process binding cpu operation
worker_cpu_affinity 1000 0100 0010 0001 (cpu 4 cores)


Question 5: How is the load balancing of nginx achieved?
accept_mutex is the load balancing lock of nginx. How
processes compete for this accept_mutex is controlled by a variable called ngx_accept_disabled,
ngx_accept_disabled = 1/8 * the total number of connections in a single process – the remaining number of idle connections
When ngx_accept_disabled is greater than zero, do not try to compete for locks, And subtract one, if it is less than zero, it will give up,
so when there are fewer remaining connections, the number will be larger, and then it will decrease to give more opportunities.


To be continued, save for now

Guess you like

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