Nginx与LibreSSL集成实践
本文将分享在 Nginx 环境中搭配 LibreSSL 使用的实际操作经验。 文中涉及的软件版本信息如下:
- nginx 1.6.0
- libressl 2.0.0
安装步骤
直接对 LibreSSL 进行源码编译,其构建过程输出简洁明了,且源码包内包含测试用例并支持并行构建(具体细节见附录)。
# 用于构建及安装 libressl 的选项
$ ./configure --prefix=/usr LDFLAGS=-lrt && make check && sudo make install
新安装的 LibreSSL 可以像 openssl 一样运行,但需注意:正如 sabotage-linux 的 spencerjohn 和 Gentoo 的 Hanno Böck 所指出的,用 libressl 完全替换操作系统中的 openssl 可能会带来麻烦。[3,4]
LibreSSL 会报告其版本号为 LibreSSL 2.0,openssl 命令的使用方式与原版一致: 复制代码 代码如下:$ which openssl /usr/bin/openssl $ openssl version LibreSSL 2.0 $ openssl s_client -host www.openssl.org -port 443 CONNECTED(00000003) depth=2 C = BE, O = GlobalSign nv-sa, OU = Root CA, CN = GlobalSign Root CA verify error:num=19:self signed certificate in certificate chain verify return:0 --- Certificate chain 0 s:/C=GB/OU=Domain Control Validated/CN=*.openssl.org i:/C=BE/O=GlobalSign nv-sa/CN=GlobalSign Domain Validation CA - G2 1 s:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA i:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA 2 s:/C=BE/O=GlobalSign nv-sa/CN=GlobalSign Domain Validation CA - G2 i:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA --- Server certificate -----BEGIN CERTIFICATE----- ... skip -----END CERTIFICATE----- subject=/C=GB/OU=Domain Control Validated/CN=*.openssl.org issuer=/C=BE/O=GlobalSign nv-sa/CN=GlobalSign Domain Validation CA - G2 --- No client certificate CA names sent --- SSL handshake has read 4136 bytes and written 707 bytes --- New, TLSv1/SSLv3, Cipher is DHE-RSA-AES256-GCM-SHA384 Server public key is 2048 bit Secure Renegotiation IS supported Compression: NONE Expansion: NONE SSL-Session: Protocol : TLSv1.2 Cipher : DHE-RSA-AES256-GCM-SHA384 TLS session ticket lifetime hint: 300 (seconds) TLS session ticket:
确认 libressl 可用后,我开始尝试让 nginx 调用 libressl。在旧系统中,若仍使用 openssl 0.9.x,我通常会将 nginx 与 openssl 静态构建在一起以获得最新的 TLS 支持。初次尝试仅使用 ./configure --with-openssl=/path/to/libressl 参数时失败了,因为 nginx 的构建流程已与 openssl 深度耦合:
- 可以使用名为./config的脚本来替代./configure(此问题较易解决)
- openssl 会将 .openssl/lib 目录下的对象文件(.obj)及其他文件收集起来链接进二进制文件和库文件中,而 libressl 则将这些文件分别存放在 crypto/.libs 和 ssl/.libs 中。
尝试通过手动建立目录层次(.openssl/lib)并根据 libressl 构建后的错误提示来复制文件以解决问题;在编译 libressl 时,看到一个类似可通过使用 LDFLAGS=-lrt 选项解决的错误提示,但在尝试编译 nginx 并链接到已静态编译过的 libressl 库时该问题仍未修复(但我继续尝试): 复制代码 代码如下: ... objs/addon/nginx-upstream-fair/ngx_http_upstream_fair_module.o \ objs/addon/src/ngx_http_headers_more_filter_module.o \ objs/addon/src/ngx_http_headers_more_headers_out.o \ objs/addon/src/ngx_http_headers_more_headers_in.o \ objs/addon/src/ngx_http_headers_more_util.o \ objs/addon/src/ngx_http_encrypted_session_module.o \ objs/addon/src/ngx_http_encrypted_session_cipher.o \ objs/ngx_modules.o \ -Wl,-E -lpthread -lcrypt -L/usr/lib -lm -llua5.1 -lpcre /data/builds/froggstack/nginx_modules/openssl/.openssl/lib/libssl.a /data/builds/froggstack/nginx_modules/openssl/.openssl/lib/libcrypto.a -ldl -lz /data/builds/froggstack/nginx_modules/openssl/.openssl/lib/libcrypto.a(libcompat_la-getentropy_linux.o): In function `getentropy_fallback': /data/builds/froggstack/nginx_modules/openssl/crypto/compat/getentropy_linux.c:324: undefined reference to `clock_gettime' /data/builds/froggstack/nginx_modules/openssl/crypto/compat/getentropy_linux.c:395: undefined reference to `clock_gettime' collect2: error: ld returned 1 exit status make[1]: *** [objs/nginx] Error 1 make[1]: Leaving directory `/data/builds/froggstack/src_nginx/nginx-1.6.0' make: *** [build] Error 2
下一次尝试是在安装 libressl 的前提下,通过链接到 libressl 的动态库来构建 nginx,最终成功(完整的 nginx ./configure 选项参数见附录)。运行 nginx-libressl -t 测试通过,并将 /usr/bin/nginx 替换为新的二进制可执行文件,随后运行 /etc/init.d/nginx restart,更新后的 nginx + libressl 正式上线。任何配置文件和 nginx 的 ssl 配置均无需修改,非常便捷!
测试环节
感兴趣的朋友可访问:www.mare-system.de,该网站自2014-07-12起运行在 libressl 之上。如发现任何不兼容问题,请通过 atsecurity@mare-system.de 留言给我。
在各种 Linux 和 Android 浏览器上测试均未发现问题;甚至在一台已被遗忘、装有2007年10月发布且已过时的附带 OpenSSL 0.9.8g 19 的 debian 5 系统上,使用 w3m 等控制台浏览器浏览也未出现问题。
在 ssllabs.com 上的测试得分为 A+,成绩与之前配置持平;在使用了 libressl 后,唯一给出的提示是加密算法 ChaCha20-Poly1305 仍处于实验阶段。

进行了一项小型性能测试,结果显示无重大问题;LibreSSL 相比平均水平慢了 4%。原因可能是 openssl 是静态链接到 nginx 的,而 libressl 则是动态链接到 nginx 的,因此产生了更多的资源开销。

纯数字测试结果:复制代码 代码如下: | Parallel Requests | OpenSSL-RPS | LibreSSL-RPS | 10 | 2341.75 | 2260.5 | 20 | 2459.75 | 2418.25 | 30 | 2472 | 2397 | 40 | 2485 | 2384.5 | 50 | 2445 | 2382.25 | 60 | 2453.25 | 2390.75 | 70 | 2426.25 | 2347.25 | 80 | 2346.5 | 2227.5 | 90 | 2325.5 | 2211 | 100 | 2297.75 | 2318.25
关于性能测试方式的更多说明可能在附录中找到。 结论部分
该方法可行。
虽然不建议在此阶段使用 LibreSSL 完全代替 OpenSSL,但我主要旨在测试其可行性。结果证明这是可行的。从我的测试来看,没有任何功能或性能上的问题,而且只要找到正确方法,构建 nginx + libressl 就变得容易了。依我看,长期使用 LibreSSL 的好处包括:
- 代码更整洁
- 漏洞更少
- 社区参与度更高
在我撰写本文时,收到了新版 LibreSSL 发布的消息,新版本解决了一些新问题。因此,再回头使用 OpenSSL 显得不太理智:
做得好,LibreSSL 团队,再次感谢