系统与基础设施aosabook.org编辑精选
开源应用架构:Nginx
The Architecture of Open Source Applications: Nginx
2012 — 2021,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这是《开源应用架构》一书中关于 Nginx 的章节,系统讲解了 Nginx 的事件驱动架构、worker 进程模型、请求处理流程及其设计取舍。文章解释了 Nginx 为何能在低资源消耗下实现高并发,并对比了它与 Apache 的架构差异。
为什么经久不衰:Nginx 至今仍是互联网基础设施的核心组件,这篇由其开发者视角撰写的架构剖析是理解事件驱动服务器设计的经典教材。
HN 评论精选
OpenResty/Lua 让 Nginx 能扩展为应用服务器,是 CloudFlare 等系统的核心
Nginx 的 Lua 支持非常出色,尤其配合 LuaJIT,基本能把 Nginx 变成一个应用服务器,运行几乎任意的代码。要特别提一下 agentzh 和他的团队发布的 OpenResty,它让用 Lua 扩展 Nginx 变得容易,这也是 CloudFlare 架构的基石,以及 Kong 等微服务管理项目的核心技术。
sinzone查看英文原文
Lua support in nginx is phenomenal, especially when combined with LuaJIT. It basically allows to transform nginx into an application server and run arbitrary code that can pretty much do anything. I would like to mention agentzh and his team that did an amazing job in releasing OpenResty[1] which makes it easy to extend nginx with custom Lua functionality, which also happens to be the backbone of CloudFlare architecture, and the core technology being used by projects like Kong[2] when it comes to microservices management. [1] http://openresty.org/ [2] https://github.com/Mashape/kong
Apache 2.4 事件模型已足够好,Nginx 的'快'被夸大,且配置匹配规则不直观
我很喜欢 Nginx 的灵活配置,但一直不明白 Apache 为什么背上了'慢'的名声。我用 Apache 2.4 的 mpm_event + mod_proxy_fcgid 运行得很好,99% 的时间和开销都在 FastCGI 应用里。Nginx 的问题在于复杂配置里很难搞清哪条规则会匹配;而 .htaccess 对多用户共享服务器来说简单好用。大多数人其实并不真的需要 Nginx,迁移到 Apache 2.4 + mpm_event 就足够了。
nisa查看英文原文
I really enjoy nginx, as it's more flexible to configure but I've never understood why Apache got this slow label... FTA: In February 2012, the Apache 2.4.x branch was released to the public. Although this latest release of Apache has added new multi-processing core modules and new proxy modules aimed at enhancing scalability and performance, it's too soon to tell if its performance, concurrency and resource utilization are now on par with, or better than, pure event-driven web servers. It would be very nice to see Apache application servers scale better with the new version, though, as it could potentially alleviate bottlenecks on the backend side which still often remain unsolved in typical nginx-plus-Apache web configurations. I'm using Apache 2.4 with mpm_event + mod_proxy_fcgid and it's doing fine - 99% of the work and time spend is done in the FastCGI application anyway and for static content mpm_event is good enough. I wouldn't run a dedicated static CDN box on Apache but for everything that can run on a single server Apache can also do the job... even HTTP/2 with mod_h2 works fine as of 2.4.17 A problem with nginx is to figure out what matches in a complex config... it's not straightforward. .htaccess is nice and simple for a shared server with lot's of users. I really like nginx but I guess most people just don't really need it. Migrating to 2.4 and mpm_event should be good enough.
标准 Nginx 嵌入脚本支持有限,推荐 lua-nginx-module
现有 worker 模型的另一个问题是对嵌入式脚本支持有限——标准 Nginx 发行版只支持嵌入 Perl 脚本。想要功能完整、健壮的嵌入式脚本支持,强烈推荐章亦春的 lua-nginx-module。
leeoniya查看英文原文
My only quibble with Nginx so far is the magical order in which it evaluates location directives. http://wiki.nginx.org/HttpCoreModule#location On more than one occasion, you have to unintuitively try to figure out whether a rule at the bottom is getting matched before a rule at the top. The matching algo is quite intricate and easy to to trip up on. It tries to help novices by matching literal strings first for them rather than simply making a note of the performance benefits of putting literal locations at the top, but ends up failing to reveal the true matching order at a glance - you need to check the verbose logs, yuck.
生态和普及度也是选型因素,Apache 的模块生态有时比 Nginx 的性能更重要
我用这台服务器的经历很糟糕。我们外包的 Web 团队用它,但不断遇到本可以借助 Apache 生态轻松解决的问题——比如返回特定请求头或状态码,在 Nginx 下都变成了反复开会的煎熬。就像 Java 有时因为生态庞大而成为最佳选择一样,有时选 Apache 就是因为几乎人人都会用,事情会简单得多。
lnanek2查看英文原文
My one experience with this server was quite bad. The web group we contracted used it, but would constantly run into issues us developers could have helped them with had they used Apache. Things like needing to return certain headers in the request, or return certain status codes, or do certain things handled easily by Apache's large ecosystem of modules became very grueling trials full of meetings with the contractors running the thing. Just like Java is sometimes the best language to use because it has huge mind share despite the warts, sometimes Apache is the right server to use just because that's what almost everyone uses and it makes things so much easier than using something more rare.
历次讨论
同类还值得读
TTY 揭秘(2008)
The TTY demystified (2008)
经典文章深入剖析了 Unix/Linux 中 TTY 子系统的工作原理,包括线路规程、终端驱动、信号与控制字符等机制。
系统与基础设施上首页 11 次 · 2009–2025linusakesson.net编辑精选为什么 GNU grep 这么快
Why GNU grep is Fast
FreeBSD 邮件列表上一篇广为流传的帖子,解释 GNU grep 为什么快:核心在于 Boyer-Moore 字符串搜索算法配合精心优化的内部循环和 I/O,让程序在搜索时“…
系统与基础设施上首页 9 次 · 2010–2022lists.freebsd.orgXplain:为普通人讲解 X11
Xplain – Explaining X11 for the rest of us
《Xplain》是作者(HN 用户 Jasper_)写的一系列 X11 长文,用他在浏览器中重建的 X11 绘图模型做交互演示,讲解 X11 的绘图模型与窗口系统设计。
系统与基础设施上首页 6 次 · 2013–2020magcius.github.io编辑精选旋风教程:为 Linux 创建极小 ELF 可执行文件(1999)
A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux (1999)
breadbox 写于 1999 年的经典教程,主题是如何为 Linux 手工构造并裁剪出体积极小的 ELF 可执行文件。
系统与基础设施上首页 6 次 · 2007–2025muppetlabs.comPostgreSQL 内部原理
The internals of PostgreSQL
《The Internals of PostgreSQL》是一本免费在线书籍,系统讲解 PostgreSQL 的内部实现,包括查询处理、并发控制、WAL、缓冲区管理等核心机制。
系统与基础设施上首页 5 次 · 2017–2025interdb.jp编辑精选