必须在源码编译时显式添加--with-mail、--with-mail_ssl_module和--with-mail_proxy_module参数,且需安装gcc、make、pcre-devel、openssl-devel、zlib-devel等依赖,再通过nginx -V验证输出含对应参数方可确认启用成功。

要启用 Nginx 的 Mail 模块,必须在源码编译阶段显式加入相关参数,它默认不编译进二进制文件。
确认系统环境与依赖
编译前确保已安装基础开发工具和依赖库:
- gcc、make、perl、libpcre3-dev(或 pcre-devel)
- openssl-dev(或 openssl-devel),用于 SSL/TLS 支持
- zlib1g-dev(或 zlib-devel),部分构建流程需要
configure 阶段启用 mail 相关模块
进入解压后的 Nginx 源码目录,执行 configure 命令时至少需添加以下三个参数:
-
--with-mail:启用核心邮件模块
ngx_mail_core_module - --with-mail_ssl_module:支持 SMTP/POP3/IMAP 的 TLS 加密连接
- --with-mail_proxy_module:开启邮件协议代理转发能力(如透传到后端真实邮件服务器)
示例完整命令:
--with-mail \
--with-mail_ssl_module \
--with-mail_proxy_module \
--with-http_ssl_module \
--with-pcre \
--with-zlib
编译并验证是否成功
运行 make && make install 完成安装后,用以下命令验证 mail 模块是否已编译进 nginx:
若输出中包含 --with-mail、--with-mail_ssl_module 和 --with-mail_proxy_module,说明启用成功。
注意关键限制
Mail 模块与 Stream 模块在配置层面互斥使用:
- mail{} 块只能存在于全局配置顶层,不能嵌套在 http{} 或 stream{} 内
- mail{} 中不支持 location、upstream 等 HTTP 模块指令
- 认证必须通过
auth_http指令对接外部 CGI 或 Web 接口,Nginx 自身不实现用户数据库


















