必须在源码编译时显式添加--with-http_ssi_module参数,且需装齐构建依赖、正确执行configure命令,并通过nginx -V和nginx -t双重验证模块内建与配置有效,缺一不可。

要让 Nginx 在源码编译时启用 SSI(Server Side Includes)模块,必须显式添加 --with-http_ssi_module 参数,并确保依赖完整、配置正确、安装后验证生效。它不是默认内置模块,漏掉任一环节都会导致 ssi on 指令不识别或配置报错。
装齐基础依赖和 OpenSSL 开发包
SSI 模块本身不依赖 OpenSSL,但实际使用中常与 HTTPS 场景共存,且编译环境需保障通用构建能力:
- CentOS / RHEL:运行
yum install -y gcc pcre-devel zlib-devel openssl-devel - Ubuntu / Debian:运行
apt-get install -y build-essential libpcre3-dev zlib1g-dev libssl-dev - 若仅启用 SSI 而不启用 HTTPS,
openssl-devel非强制,但建议保留——避免后续补加 SSL 模块时重复安装
configure 时显式启用 --with-http_ssi_module
进入解压后的 Nginx 源码目录(如 nginx-1.24.0),执行 ./configure 命令时必须包含该参数:
- 最简命令示例:
./configure --prefix=/usr/local/nginx --with-http_ssi_module - 推荐组合(兼顾常用功能):
./configure --prefix=/usr/local/nginx --with-http_ssi_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module - 注意:不要写成
--with-http-ssi-module或拼错名称,正确名称是--with-http_ssi_module
编译安装并确认模块已内建
执行编译安装后,需双重验证模块是否真正集成进二进制文件:
- 运行
make && sudo make install - 检查输出:
/usr/local/nginx/sbin/nginx -V 2>&1 | grep -o with-http_ssi_module,应返回with-http_ssi_module - 在配置文件中添加
location { ssi on; }并运行nginx -t,不报unknown directive "ssi"才算成功
配置 SSI 功能并处理常见限制
模块启用后,还需在 nginx.conf 中开启处理逻辑,并注意路径兼容性问题:
- 在
http、server或location块中加入ssi on; - 默认只处理
text/html类型响应,若需支持.shtml或其他类型,加配置:ssi_types text/html application/xhtml+xml; - Nginx 原生 SSI 不支持
../这类相对路径的include virtual,如需兼容 Apache/Tomcat 行为,需修改源码中的ngx_http_ssi_filter_module.c,注释掉ngx_http_parse_unsafe_uri相关校验逻辑,再重新编译











