OpenBSD 作为标准的 UNIX 系统,提供 Web Server 能力,就如同空气与水一般自然。
早年,成熟的 Web Server 只有 Apache httpd,所有网站,后端基本都是它。之后,nginx 横空出世,逐步成为最流行的 Web Server。
而 OpenBSD 最开始,也是把 Apache httpd 作为 base system 组件之一。
Apache 1.3 => nginx => OpenBSD httpd
最早 Apache httpd 是满足要求的,但随着功能越来越多,系统也越来越复杂。作为 OpenBSD 上 Web Server 组件的维护者,Reyk Floeter (reky@openbsd.org) 将 Apache httpd 切换为 nginx,并从2011年开始,作为 OpenBSD 的主力 Web Server。
OpenBSD 的哲学是 Secure, Simple & Clean Code,让 nginx 符合此哲学,特别是 Secure 的要求,需要针对 nginx 打很多补丁。Reyk 为此花费了大量精力,他尝试将相关修改提交给 nginx 项目,但未被采纳。
为节约维护成本,Reyk 在 g2k14 hackathon 上,完成了 httpd 的原型,并快速迭代,在 OpenBSD 5.6 正式成为官方标配。
We constantly improve our code base for better security & quality. Aiming for perfection - "security craftsmanship"
OpenBSD 在诞生之初,就是为极致的安全而生。天生具有"一言不合,就自己搞一套更安全的"基因。
2014年 OpenSSL 爆发 heartbleed 安全漏洞,让大量使用 https 的网站处于危险之中。OpenBSD 也依赖 OpenSSL,此次事件之后,OpenBSD 团队对 OpenSSL 进行审计,发现大量陈旧、复杂、无人使用的代码。为此,OpenBSD 团队 fork 了 OpenSSL 项目,创建 LibreSSL,自己独立维护。
插曲:为了支持开源项目。2016年,罗永浩的锤子科技分别给 OpenSSL 和 OpenBSD 项目一次捐款150万。
Simplicity: Clean Code
只支持最核心的业务逻辑
如今(2026.06.12),OpenBSD 7.9 的 httpd 也只有1.6万行代码。
$ wc -l *
0 CVS
31 Makefile
977 config.c
319 control.c
34 css.h.in
257 http.h
100 httpd.8
1265 httpd.c
910 httpd.conf.5
817 httpd.h
19 js.h.in
199 log.c
46 log.h
320 logger.c
2589 parse.y
309 patterns.7
713 patterns.c
46 patterns.h
833 proc.c
1481 server.c
854 server_fcgi.c
826 server_file.c
2029 server_http.c
1054 tags
10 toheader.sed
16038 total
Simplicity: Core Features
Simplicity: Configuration
使用 OpenBSD 通用的配置格式,语法极简。
一份最简单的配置:监听所有网卡的80端口
$ cat /etc/httpd.conf
server "www.example.com" {
listen on * port 80
}
稍微复杂的配置:监听 http(80) 和 https(443) 端口、不写 log、为 php 文件启用 fastcgi
server "www.example.com" {
listen on * port 80
listen on * tls port 443
no log
location "*.php" {
fastcgi socket "/run/php-fpm.sock"
}
}
Security: Privilege Separation
httpd 默认 chroot,并通过 fork 不同职责的进程,达到权限的隔离。
默认启动时的进程结构
$ ps aux | grep httpd | grep www
www 31134 0.0 0.0 1436 2948 ?? Ipc 3:39PM 0:00.00 httpd: server (httpd)
www 44380 0.0 0.0 1432 2932 ?? Ipc 3:39PM 0:00.00 httpd: server (httpd)
www 80866 0.0 0.0 1424 2908 ?? Ipc 3:39PM 0:00.00 httpd: server (httpd)
www 71351 0.0 0.0 1556 2932 ?? Ipc 3:39PM 0:00.00 httpd: logger (httpd)
Security: TLS with LibreSSL