You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何强制Nginx忽略CDN响应Content-Type,对.html后缀文件执行SSI解析

Nginx强制对.html后缀文件执行SSI解析解决方案

问题根因

你当前配置里的add_header指令仅会修改返回给客户端的响应头,不会修改上游CDN返回给Nginx内部处理的响应头,因此Nginx拿到的仍是application/octet-stream类型,触发了默认仅解析text/html类型的SSI限制。

方案1:简易配置(测试/小规模场景用)

直接在location块中添加ssi_types指令放宽SSI处理的类型限制,搭配后缀判断缩小生效范围:

location /path/ {
    ssi on;
    # 允许SSI处理application/octet-stream类型的响应,也可写*允许所有类型
    ssi_types application/octet-stream;
    # 仅对.html后缀的请求保留SSI开启状态
    if ($request_uri !~* \.html$) {
        ssi off;
    }
    add_header Content-Type text/html;
    proxy_pass https://example-cdn.com/path/;
}

方案2:高性能配置(生产环境推荐)

使用map指令提前做后缀匹配,避免在location块中使用if导致的逻辑异常问题,性能更稳定:
首先在Nginx全局http块中添加映射规则:

http {
    # 其余全局配置保持不变
    map $request_uri $enable_ssi {
        default off;
        ~*\.html$ on;
    }

    server {
        # 其余server配置保持不变
        location /path/ {
            ssi $enable_ssi;
            # 允许SSI处理任意Content-Type的响应
            ssi_types *;
            add_header Content-Type text/html;
            proxy_pass https://example-cdn.com/path/;
        }
    }
}

修改配置后执行nginx -t校验配置无误,再重载配置即可生效。

内容的提问来源于stack exchange,提问作者Tamilarasu Ulaganathan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 16:15:03