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

NGINX反向代理替换响应体特定文本失败求助

问题描述

我需要通过NGINX反向代理实现替换text/html类型页面响应体中的特定文本。访问NGINX基础URL时会导向https://www.google.com,但响应并未经过NGINX,导致sub_filter指令无法生效。我在本地Tomcat服务器环境中用Proxy_Pass+rewrite+sub_filter的方案可行,但针对谷歌页面时完全失效。当前核心需求是:配置NGINX代理谷歌页面,让响应返回至NGINX后再执行文本替换。

以下是我尝试过的4种NGINX配置,所有配置都能实现跳转,但响应均未经过NGINX:


配置1

server {
    listen       85;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
        rewrite ^/(.*)$ https://www.google.com/$1 permanent;
        sub_filter '</body>' '</body><div><p>Replace success</p></div>';
        sub_filter_once off;
    }
}

配置2

server {
    listen       85;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass https://www.google.com;
        sub_filter '</body>' '</body><div><p>test</p></div>';
        sub_filter_once off;
    }
}

配置3

server {
    listen       85;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
        add_header Access-Control-Allow-Origin $http_origin;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-NginX-Proxy true;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass https://www.google.com;
        sub_filter '</body>' '</body><div><p>test</p></div>';
        sub_filter_once off;
    }
}

配置4

upstream test {
    ip_hash;
    server google.com:443;
}

server {
    listen       85;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
        proxy_pass https:test;
        add_header Access-Control-Allow-Origin $http_origin;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-NginX-Proxy true;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    
        sub_filter '</body>' '</body><div><p>test</p></div>';
        sub_filter_once off;
    }
}

问题分析与解决方案

之前配置的问题点

  1. 配置1:使用rewrite ... permanent直接给客户端返回301重定向,客户端会直接请求谷歌服务器,完全绕开NGINX,sub_filter根本没有执行机会。
  2. 配置2、3、4:
    • 未正确设置Host头为谷歌域名,谷歌服务器会返回302重定向到官方域名,客户端直接跳转,响应不经过NGINX。
    • 谷歌默认返回gzip压缩的响应,sub_filter仅能处理未压缩的文本内容,无法直接替换压缩后的响应体。
    • 配置4中proxy_pass语法错误,应为https://test而非https:test。

正确配置示例

server {
    listen       85;
    server_name  localhost;

    access_log  logs/google-proxy.access.log  main;

    location / {
        # 无需本地文件,注释掉root和index
        # root   html;
        # index  index.html index.htm;

        # 核心:设置Host头为谷歌官方域名,避免谷歌返回重定向
        proxy_set_header Host www.google.com;
        # 传递客户端真实IP相关信息
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # 禁用SSL会话复用,避免握手异常
        proxy_ssl_session_reuse off;
        # 将谷歌响应中的重定向地址替换为NGINX地址,确保流量始终经过NGINX
        proxy_redirect https://www.google.com/ /;

        # 方案1:阻止谷歌返回压缩内容,让sub_filter直接处理文本
        proxy_set_header Accept-Encoding "";
        # 方案2:如需保留压缩,启用NGINX的解压与重新压缩功能
        # gunzip on;
        # gzip on;

        # 代理到谷歌服务器
        proxy_pass https://www.google.com;

        # 文本替换配置
        sub_filter '</body>' '</body><div><p>替换成功</p></div>';
        sub_filter_once off;
        # 指定仅处理text/html类型响应
        sub_filter_types text/html;
    }
}

关键配置说明

  • proxy_set_header Host www.google.com:让谷歌服务器识别请求为合法的官方域名请求,不会触发重定向。
  • proxy_redirect https://www.google.com/ /:拦截谷歌返回的重定向响应,将目标地址替换为NGINX的地址,确保客户端始终通过NGINX访问。
  • proxy_set_header Accept-Encoding "":告诉谷歌服务器不要返回压缩内容,保证sub_filter能直接处理明文响应。如果需要压缩,启用gunzip on让NGINX先解压处理,再重新压缩返回给客户端。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 16:10:54