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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
David Zhang
September 09, 2016
Programming
580
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
33
ReactNative 探索之路
crispgm
1
400
Other Decks in Programming
See All in Programming
JRuby: Past, Present, and Future
headius
0
220
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
140
SalesForceを内製化!? ~ HR事業を支える顧客管理基盤のインフラを大公開 ~
oku053
0
150
dotNET_vulnclicker_intro.pdf
deka_morita
0
110
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.4k
wkhtmltopdfの次どうするか問題2026
willnet
2
1.9k
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
920
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
120
最近”作らせてる” ローカルAIアプリの話
akit37
0
200
技術的負債の返済は、AI時代の複利で効く投資 — 経営としての意思決定とその遂行
curekoshimizu
1
2.2k
カツオ、ご期待ください
suneo3476
0
150
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
630
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
Faster Mobile Websites
deanohume
310
32k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Tell your own story through comics
letsgokoyo
1
1.1k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
310
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
320
SEO for Brand Visibility & Recognition
aleyda
0
4.8k
So, you think you're a good person
axbom
PRO
2
2.2k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
790
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Docker and Python
trallard
47
4.2k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
2
320
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 !