The underlying principle of nginx: analyze why the concurrent number of nginx can reach 3W!

Linux server development 2020-11-12 15:36:14
underlying principle nginx analyze concurrent

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 .

reflection :

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 :

Modular architecture

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)

High performance

High concurrency

Less system resources


Blocking + Multi process / Multithreading

A more stable ,Bug Less

More modules

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 .

reflection :

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

Request header

Request body

HTTP Respond to :

Response line :http version、status code

Response head

Response body

IO Model

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 ).

本文为[Linux server development]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple