找回密码
 新建账号

编译使用 nginx http acme module ngx_http_acme_module 实现自动签发、部署、续签 HTTPS 证书

[复制链接]
大郎 发表于 2025-12-27 16:22 | 显示全部楼层 |阅读模式
nginx http acme module ngx_http_acme_module 模块用于实现 nginx 自动签发、部署、续签 HTTPS 证书。TLS 证书最长有效期越来越短,从原来的八年缩短到五年,又缩短到十三个月,即将缩短到两百天,最终会缩短到四十七天,未来会进一步缩短;加密证书越来越短命,传统的手动签证部署方式将难以应对频繁续签和部署。

nginx 的 acme 原生支持起步比较晚,由于无法自动申请和续签证书,被 caddy 偷了桃子,抢占了不少的市场。后来 nginx 终于想通了,搞出了 ngx_http_acme_module 模块,但是,直到现在 nginx 仍然将 ngx_http_acme_module 作为附加模块在 Github 上发布,而不是和 nginx 标准代码或预编译程序一起打包发行。要使用 ngx_http_acme_module 模块就只能自己下载源码编译。要编译 ngx_http_acme_module 也不是一件容易的事,因为 ngx_http_acme_module 是用 Rust 所写,需要安装 Rust 才能编译它,很容易卡关。好在如果有多台运行配置一样的同类型操作系统的设备,可以只在一台设备上将 ngx_http_acme_module 编译成动态链接库 ngx_http_acme_module.so,将这个文件拷贝到其他设备上 nginx 的 modules 目录内,然后用 load_module 加载使用。

在 Ubuntu 编译 ngx_http_acme_module 可以分以下步骤。

第一步,安装必要的依赖程序或库。ngx_http_acme_module 依赖于 nginx 编译需要的所有程序和库,还额外需要 libclang-dev 和 Rust 1.81+。编译软件需要的程序或库需要根据实际情况安装。
安装基本库
  1. apt -y install libclang-dev libpcre2-dev libssl-dev zlib1g-dev
复制代码
安装 Rust。这是 Rust 官方推荐的安装方式。
  1. curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
复制代码
第二步,下载 nginx 源码和 ngx_http_acme_module 源码,将它们解包。
第三步,进入 nginx 源码解包后的目录,运行 nginx 源码中的 configure 脚本。可以根据实际需要调整参数。
  1. ./configure --prefix=/wuxiancheng.cn/nginx/1.29.4 --with-file-aio --with-http_addition_module --with-http_realip_module --with-http_gunzip_module --with-http_secure_link_module --with-http_ssl_module --with-http_sub_module --with-http_v2_module --with-http_v3_module --with-cc-opt="-I../boringssl/include" --with-ld-opt="-L../boringssl/build/ssl -L../boringssl/build/crypto" --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-stream_realip_module --with-threads --add-dynamic-module=/root/nginx-acme-0.3.1
复制代码
以下参数是关键,它会将 ngx_http_acme_module 编译为 ngx_http_acme_module.so 这个可以动态加载的模块文件。也可以使用 --add-module 参数,它会让模块编译进 nginx 主程序,导致模块无法跨设备拷贝使用。
  1. --add-dynamic-module=/root/nginx-acme-0.3.1
复制代码
第四步,编译和安装。参数可以自己调整。
  1. make
  2. make install
复制代码
第五步,如果选择了将 ngx_http_acme_module 编译为 ngx_http_acme_module.so,就在 nginx 配置文件中的最顶层也就是 main 层加入以下配置项。
  1. load_module modules/ngx_http_acme_module.so
复制代码
第六步,按另一篇文章《nginx http acme module ngx_http_acme_module 自动签发、部署、续签 HTTPS 证书配置示例》按官方文档中的 ngx_http_acme_module 章节配置。

需要注意的是,要申请证书的域名必须能被证书签发机构访问到,因此,在为服务器域名做 TLS 证书申请部署之前一定要先将域名正确解析,保证公网能够查询到域名记录:这是自动申请 HTTPS 证书和先申请 TLS 证书再部署的一个大的区别。

TLS 证书和 HTTPS 证书是对同一个概念的不同叫法。

手机版|轻松e站

GMT+8, 2026-1-27 02:58

快速回复 返回顶部 返回列表