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

Nginx反向代理sub_filter无法替换iframe域名问题求助

解决Nginx反向代理中iframe域名未替换的问题

你的问题出在sub_filter配置的作用域以及部分代理头设置上,以下是具体解决方法:

1. 调整sub_filter配置的位置

将sub_filter、sub_filter_types、sub_filter_once这些配置从server块移到location /块内部——server块的sub_filter规则默认不会作用于proxy_pass返回的响应内容,必须在对应的代理location中配置才能生效。

2. 优化代理请求头(可选但推荐)

如果后端应用会根据请求头生成iframe的src链接,添加X-Forwarded-Host头可以让后端感知到用户实际访问的域名,从根源减少需要替换的场景;同时保留Host头为example.com以兼容后端的域名校验(如果有的话)。

修改后的完整配置

server {
    listen       [::]:10000  ssl;
    server_name  abc.com;
    root         /usr/share/nginx/html;

    proxy_redirect https://example.com/ $scheme://abc.com/;

    location / {
        proxy_pass https://example.com;
        proxy_set_header Host example.com;
        proxy_set_header X-Forwarded-Host abc.com;
        
        sub_filter_types *;
        sub_filter example.com abc.com;
        sub_filter_once off;
    }
}

额外排查点

  • 检查iframe的src是否是动态生成的(比如通过前端JS在页面加载后拼接),如果是这种情况,Nginx的sub_filter无法处理客户端动态生成的内容,需要在前端代码或后端接口中调整域名输出。
  • 清除浏览器缓存后再测试,避免旧的页面缓存导致替换不生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 10:04:52