phpEnv的Nginx默认不解析SSI,需在匹配.shtml等后缀的location块中启用ssi on并配置ssi_types text/html,确保文件后缀、Content-Type和路径权限一致,且不与PHP location冲突。

phpEnv 的 Nginx 环境默认不解析 SSI,必须手动启用 ngx_http_ssi_module 并配置作用域和 MIME 类型,否则 <!--#include--> 会原样输出或报错。
确认 phpEnv 中 Nginx 是否已编译 SSI 模块
phpEnv 是轻量级 PHP 开发环境,其内置 Nginx 多数版本已静态编译 ngx_http_ssi_module,但不默认启用。不能仅靠 nginx -V 2>&1 | grep -o with-http-ssi-module 判断是否生效——模块存在 ≠ 已启用。真正验证方式是:在配置中加入 ssi on 后 reload,再测 .shtml 文件是否解析。
- 若执行
nginx -t报错unknown directive "ssi",说明该 phpEnv 版本 Nginx 未编译此模块(极少见),需换用宝塔、LNMP 或自编译 Nginx - 若
nginx -t通过但 SSI 不执行,问题一定出在配置位置、ssi_types匹配或文件后缀三者之一 - phpEnv 通常把主配置放在
~/phpenv/nginx/conf/nginx.conf,站点配置在~/phpenv/nginx/conf/vhost/*.conf,优先改后者
在 location 块中正确启用 SSI(不是 server 或 http)
SSI 必须在能匹配到目标文件的 location 块内开启,且该块需能处理响应体内容。常见错误是把 ssi on 写在 location ~ \.php$ 或静态资源 location ~* \.(js|css|png)$ 里——这些 location 不负责返回 HTML 内容,SSI 自然不会触发。
- 推荐写法:新增一个专用于 SSI 的 location,例如
location ~ \.shtml$,并在其中启用ssi on - 必须同时设置
ssi_types text/html(即使后缀是.shtml,Nginx 默认只对text/html类型做 SSI 解析) - 不要把
ssi on放在location /下并期望它覆盖所有请求——Nginx 的 location 匹配是精确优先,location ~ \.php$会截断对/index.shtml的处理,导致 SSI 被跳过 - 若想让
.html也支持 SSI,可将ssi_types改为ssi_types text/html并确保该 location 能匹配.html请求(如location ~ \.(html|htm|shtml)$)
文件后缀、Content-Type 和路径权限必须一致
SSI 不生效最常卡在这三个点:浏览器请求的是 /test.html,但 Nginx 实际返回的 Content-Type 是 text/plain;或者 include 引用的路径在 Nginx 根目录外;又或者文件权限导致读取失败,但 ssi_silent_errors on 把错误吞掉了。
立即学习“PHP免费学习笔记(深入)”;
- 测试文件必须用
.shtml后缀(除非你明确配置了ssi_types支持.html),且访问 URL 必须带该后缀,不能靠 rewrite 隐藏 - 确保
include指令中的路径是相对于 Nginxroot的绝对路径,例如root /home/user/www,那么<!--# include file="/header.shtml" -->对应的真实路径是/home/user/www/header.shtml - 检查文件权限:
header.shtml需被 Nginx worker 进程可读(通常是www-data或daemon用户),用ls -l确认 - 临时关闭
ssi_silent_errors(设为off),看页面是否出现[an error occurred while processing the directive]—— 出现即说明 SSI 已启用,只是 include 路径或权限有问题
PHP 和 SSI 共存时的典型冲突
phpEnv 中常见把 PHP 和静态 HTML 混在同一目录,而 Nginx 的 location ~ \.php$ 会优先接管所有含 .php 字符串的请求。如果你误把 SSI 文件命名为 index.php.shtml,或在 PHP 输出中直接 echo 出 <!--#include--> 注释,Nginx 不会二次解析响应体——SSI 只在 Nginx 返回原始文件时触发,不在 PHP 输出流中生效。
- SSI 和 PHP 是两套机制:SSI 在 Nginx 层解析静态文件;PHP 是 FastCGI 动态执行。二者不能嵌套(比如在
.php文件里写<!--#include-->不会起作用) - 若需动态+包含混合,应由 PHP 负责包含逻辑(
include或file_get_contents),而非依赖 SSI - 避免在
location ~ \.php$块里加ssi on—— 它对 FastCGI 返回的内容完全无效,纯属误导配置 - 真实可用组合只有两种:
.shtml(纯静态 + SSI) 或.php(PHP 动态生成,自行处理包含)
最容易被忽略的是:phpEnv 的 Nginx 配置 reload 后,必须清空浏览器缓存或用 curl -I 确认响应头中 Content-Type 确实是 text/html,而不是 text/plain 或 application/octet-stream——后者会让 SSI 直接失效,连错误提示都不会显示。



















