Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Nginx 事件驱动机制
Search
David Zhang
September 09, 2016
Programming
570
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Nginx 事件驱动机制
来自 Nevermore (@hzphust)
David Zhang
September 09, 2016
More Decks by David Zhang
See All by David Zhang
ngx_share: conf & module
crispgm
0
29
ReactNative 探索之路
crispgm
1
400
Other Decks in Programming
See All in Programming
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
120
iOSDC Japan 2026 - Swiftで作って学ぼう!データベース自作入門
kaseken
2
170
Family mrubyの進捗
kishima
1
120
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
150
Security issues being discussed on Web Platforms
petamoriken
0
960
WebRTC映像をAirPlayに対応させる挑戦.pdf
monolithic_adam
0
270
新卒PdEのリアル
ryu1013
1
500
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
340
App Storeの外へ──日本のiOSサイドローディング入門 for iOSDC Japan 2026
yuukiw00w
0
220
LL言語やWebフレームワークのPostgreSQL対応 〜DBの機能がユーザーに届くまで〜
kentaroutakeda
1
150
Are APIs Still Relevant in the AI Era?
soyuka
0
250
個人開発基盤をまるごとCloudflareに引っ越して爆速で総合的体験を向上させた話
tinykitten
0
190
Featured
See All Featured
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.6k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.1k
Embracing the Ebb and Flow
colly
88
5.2k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
5
670
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
850
Leo the Paperboy
mayatellez
10
2.3k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
57k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
30 Presentation Tips
portentint
PRO
1
400
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
430
Into the Great Unknown - MozCon
thekraken
41
2.7k
Transcript
NGINX事件驱动机制
大纲 ▸ Linuxᔮᕹ5ᐿIOཛྷୗ ▸ IOग़᪠॔አ ▸ nginxԪկཛྷࣘ ▸ nginxԪկḝۖຝ
⽂本 IO准备知识 ▸ DirectIO和BufferIO ▸ IO数据先拷贝到内核空间的缓冲区,然后从内核空间缓冲区复制到应用程序的地址 空间 ▸ 不经过内核缓冲区,直接访问应用程序地址空间 ▸
IO的两个阶段 ▸ 等待数据准备 ▸ 将数据从内核拷贝到用户进程中
LINUX系统5种IO模式 ▸ blocking IO ▸ nonblocking IO ▸ IO multiplexing
▸ signal driven IO ▸ asynchronous IO
⽂本
⽂本
⽂本 IO multiplexing ▸ I/O多路复用就是通过一种机制,一个进程可以监视多个描述符,一旦某个 描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写 操作 ▸ 本质是同步IO ▸
select/poll/epoll int select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); int poll (struct pollfd *fds, unsigned int nfds, int timeout);
⽂本 epoll ▸ 数据和接口 ▸ LT模式和ET模式 int epoll_create(int size); int
epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout); struct epoll_event { __uint32_t events; epoll_data_t data; } EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD EPOLLIN EPOLLOUT EPOLLHUP EPOLLERR
⽂本 nginx事件模块 ▸ 事件驱动架构 ▸ ᗑܜ/ᏺፏ/ਧԾኞԪկ ▸ ngxԪկཛྷࣘᨮᨱԪկጱතᵞ/ړݎ ▸ ٌ՜ཛྷࣘဳٙఽي᪁Ԫկ҅ၾᩇԪկ
▸ 与传统web服务器比较 ▸ 不再使用进程/线程作为事件消费者,只能是模块,只有事件收集/分发器才能占用 进程资源 ▸ 事件消费者不允许有任何阻塞操作,进程不会休眠
⽂本 EVENT ▸ IO和定时器 ▸ ngx_event_t ▸ void *data, ngx_event_handler_pt
handler ▸ nginx启动时预分配了所有读写事件,和连接相对应 ▸ ngx_handle_read_event/ngx_handle_write_event
⽂本 CONNECTION ▸ 主动连接/被动连接 ▸ ngx_connection_t ▸ fd, read_ev, write_ev,data,
sockaddr,recv,write ▸ 连接池 ▸ connections/free_connections ▸ ngx_get_connection/ngx_free_connection
⽂本 NGX_EVENT_MODULE ▸ 事件模块 ▸ 功能 ▸ 定义新的事件类型 ▸ 定义所有事件模块必须实现的ngx_event_module_t接口
▸ 管理并解析事件类模块的配置项 ▸ ngx_events_module ▸ ngx_events_commands ▸ ngx_events_module_ctx
⽂本 NGX_EVENT_CORE_MODULE ▸ 所有事件模块中顺序第一位 ▸ 功能 ▸ 创建连接池 ▸ 决定使用哪些事件驱动机制
▸ 初始化将要使用的事件模块 ▸ ngx_events_core_commands
⽂本 NGX_EPOLL_MODULE ▸ ngx_epoll_init ▸ epoll_create ▸ 创建event_list数组 ▸ ngx_epoll_add_event/ngx_epoll_del_event
▸ epoll_ctl ▸ ngx_epoll_process_events ▸ epoll_wait ▸ instance/NGX_POST_EVENTS 标志位
⽂本 ACCEPT_MUTEX ▸ “惊群”效应 ▸ 多个worker同时accept一个连接 ▸ accept_mutex ▸ ngx_trylock_accept_mutex
▸ ngx_enable_accept_event ▸ 什么时候释放? ▸ nginx post事件机制 ▸ NGX_POST_EVENTS flags ▸ ngx_posted_accept_events/ngx_posted_events ▸ worker子进程的负载均衡 ▸ ngx_accept_disabled = connection_n/8 - free_connection_n
⽂本 ▸ ngx_process_events_and_timer ▸ 处理网络事件 ▸ 调用所有事件模块的ngx_event_process ▸ 处理post事件队列 ▸
ngx_event_process_posted ▸ 处理定时器事件 ▸ ngx_event_expire_timer()
THANKS !