Nginx With its high performance , stability , Rich functions , It is famous for its simple configuration and low resource consumption . This article from the bottom principle analysis Nginx Why so soon? ！
Nginx Process model of
Nginx The server , During normal operation ：
Multi process ： One Master process 、 Multiple Worker process .
Master process ： management Worker process . External interface ： Receive external operations （ The signal ）; Forward... Internally ： Depending on the external operation , Through signal management Worker; monitor ： monitor Worker The running state of the process ,Worker After the process terminates abnormally , Automatic restart Worker process .
Worker process ： all Worker The process is all equal . The actual processing ： Network request , from Worker Process processing .Worker Number of processes ： stay nginx.conf Middle configuration , It is generally set as the core number , make the best of CPU resources , meanwhile , Avoid too many processes , Avoid process competition CPU resources , Increase the loss of context switching .
The request is to connect to Nginx,Master The process is responsible for processing and forwarding ？
How to choose which Worker The process processes the request ？ The processing result of the request , Is it necessary to go through Master process ？
HTTP Connection establishment and request processing
HTTP The process of connection establishment and request processing is as follows ：
Nginx Startup time ,Master process , Load profile .
Master process , Initialize listening Socket.
Master process ,Fork More than one Worker process .
Worker process , Competing for new connections , The winner shakes hands three times , establish Socket Connect , And process the request .
Nginx High performance 、 High concurrency
Nginx Why have high performance and can support high concurrency ？
Nginx Adopt multi process + Asynchronous non blocking mode （IO Multiplexing Epoll）.
Complete process of request ： Establishing a connection → Read request → Resolve request → Processing requests → Response request .
The whole process of the request corresponds to the underlying layer ： Reading and writing Socket event .
Nginx Event processing model of
Request：Nginx in HTTP request .
Basic HTTP Web Server Working mode ：
Receiving request ： Read the request line and request header line by line , After the judgment segment has a request body , Read request body .
Processing requests .
Return response ： According to the result , Generate corresponding HTTP request （ Response line 、 Response head 、 Response body ）.
Nginx It's the same routine , The overall process is consistent ：
more Linux The basic principles of server development Nginx,ZeroMQ,MySQL,Redis,MongoDB,ZK, Streaming media ,P2P, Thread pool ,Docker,TCP/IP, coroutines ,DPDK,Linux Kernel content .+602878196（VX Same number ） obtain
Nginx According to its function, the modules can be divided into the following types ：
①event module： The framework of event handling mechanism independent of operating system is built , And provide the handling of specific events . Include ngx_events_module,ngx_event_core_module and ngx_epoll_module etc. .
Nginx What kind of event processing module is used , It depends on the specific operating system and compilation options .
②phase handler： This type of module is also called directly handler modular . Mainly responsible for processing client requests and generating content to be responded , such as ngx_http_static_module modular , Responsible for client static page request processing and preparing the corresponding disk file for response content output .
③output filter： Also known as filter modular , It is mainly responsible for processing the output content , You can modify the output .
for example , Can achieve all of the output html Add predefined footbar One kind of work , Or for the output picture URL Do the work of replacing .
④upstream：upstream Module realizes the function of reverse proxy , Forward the real request to the back-end server , And read the response from the back-end server , Send back to client .
upstream The module is a special handler, But the response content is not really generated by itself , It's read from the back-end server .
⑤load-balancer： Load balancing module , Implement specific algorithms , In many back-end servers , Select a server to forward a request .
Analysis of common problems
Nginx vs Apache
IO Multiplexing ,Epoll（freebsd On is kqueue）
Less system resources
Blocking + Multi process / Multithreading
A more stable ,Bug Less
Nginx maximum connection
Based on the background ：
Nginx It's a multi process model ,Worker The process is used to process requests .
The number of connections for a single process （ File descriptor fd）, Upper limit （nofile）：ulimit -n.
Nginx Configure a single Worker The maximum number of connections for a process ：worker_connections Cap of nofile.
Nginx On the configuration Worker The number of processes ：worker_processes.
therefore ,Nginx Is the maximum number of connections ：
Nginx Is the maximum number of connections ：Worker Number of processes x Single Worker The maximum number of connections for a process .
It's on it Nginx As a universal server , Maximum number of connections .
Nginx When acting as a reverse proxy , The maximum number of connections that can be serviced ：（Worker Number of processes x Single Worker The maximum number of connections for a process ）/ 2.
Nginx When reverse proxy , Will establish Client The connection and back end of Web Server The connection of , Occupy 2 A connection .
Every time you open one Socket Take up one fd？
Why? , A process can open fd Limited quantity ？
HTTP Requests and responses
HTTP request ：
Request line ：method、uri、http version
HTTP Respond to ：
Response line ：http version、status code
When processing multiple requests , May adopt ：IO Multiplexing or blocking IO+ Multithreading ：
IO Multiplexing ： One thread , Track multiple Socket state , Which is ready , Read and write which .
Blocking IO+ Multithreading ： Every request , Create a new service thread .
IO Multiplexing and multithreading scenarios ？
IO Multiplexing ： There is no advantage in the speed of request processing for a single connection .
A lot of concurrency ： Use only one thread , Handle a lot of concurrent requests , Reduce context switching loss , There's no need to think about concurrency , More requests can be processed .
Consume less system resources （ No thread scheduling overhead ）.
For long connections （ Long connection in multi-threaded mode is easy to cause too many threads , Cause frequent scheduling ）.
Blocking IO + Multithreading ： Implement a simple , Can be independent of system calls .
Every thread , Both need time and space .
When the number of threads grows , The cost of thread scheduling increases exponentially .
select/poll and epoll The comparison is as follows ：
select/poll system call ：
// select system call
int select(int maxfdp,fd_set readfds,fd_set writefds,fd_set errorfds,struct timeval timeout);
// poll system call
int poll(struct pollfd fds, nfds_t nfds, int timeout);
Inquire about fd_set in , Is there anything ready fd, You can set a timeout , When there is fd (File descripter) Ready or timeout return .
fd_set It's a bit set , Size is a constant when compiling the kernel , The default size is 1024.
characteristic ： Connections limit ,fd_set Expressible fd The quantity is too small ; Linear scanning ： Judge fd Is it ready , Need to traverse the side fd_set; Data replication ： User space and kernel space , Copy connection ready status information .
Solved the connection limit ：poll Lieutenant general select Medium fd_set Replaced with a pollfd Array , solve fd Too small a number of problems .
Data replication ： User space and kernel space , Copy connection ready status information .
epoll,event Event driven ：
Event mechanism ： Avoid linear scanning , For each fd, Register a listening event ,fd When changed to ready , take fd Add to ready list .
fd Number ： unlimited （OS Level restrictions , How many can a single process open fd）.
I/O Multiplexing mechanism .
I/O Multiplexing is through a mechanism , Can monitor multiple descriptors , Once a descriptor is ready （ Generally read ready or write ready ）, Can inform the program to read and write accordingly ; Monitoring multiple file descriptors .
but select,poll,epoll It's all synchronous in nature I/O： The user process is responsible for reading and writing （ Copy from kernel space to user space ）, In the process of reading and writing , User process is blocked ; asynchronous IO, No user process is responsible for reading and writing , asynchronous IO, Will be responsible for copying from kernel space to user space .
Nginx The concurrent processing power of
About Nginx The concurrent processing power of ： Number of concurrent connections , After general optimization , The peak value can be kept at 1~3w about .（ Memory and CPU The number of cores is different , There will be room for further optimization ）.