有没有办法删除 apache 反向代理请求标头?

发布于 2024-12-03 02:15:38 字数 822 浏览 1 评论 0原文

当充当反向代理时,apache 添加 x-forwarded 标头,如此处所述。

http://httpd.apache.org/docs/2.2/ mod/mod_proxy.html#x-headers

在我的配置中,我已将服务器 A 配置为转发代理。有一条这样的规则:

RewriteRule proxy:(.*example.com)/(.*) $1.mysecondserver.com/$2 [P]

这条规则让服务器从我的其他服务器之一请求资源。

在第二台服务器(源)上,我有一个用于资源的虚拟主机容器和另一个重写规则,如下所示:

RewriteRule some-regex some-url [P]

它可能看起来没有意义,但还有很多其他事情发生,我遗漏了,因为它不是问题的一部分。

然而,最终的请求有这些标头:

[X-Forwarded-For] => ip of 1st server
[X-Forwarded-Host] => example.myseconserver.com
[X-Forwarded-Server] => example.com

我希望这些标头消失。

我似乎无法使用 mod_headers 取消设置它们。我可以向其中添加更多条目,但无法删除它们。

有什么想法吗?

When acting as a reverse proxy, apache adds x-forwarded headers as described here.

http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#x-headers

In my configuration I have configured server A as a forward proxy. There is a rule like this:

RewriteRule proxy:(.*example.com)/(.*) $1.mysecondserver.com/$2 [P]

This rule lets the server request the resource from one of my other servers.

On the second server (origin) I have a virtual host container for the resource and another rewrite rule like this:

RewriteRule some-regex some-url [P]

It may not seem to make sense like this but there is a lot of other stuff going on that I left out as it is not part of the problem.

However that final request has these headers:

[X-Forwarded-For] => ip of 1st server
[X-Forwarded-Host] => example.myseconserver.com
[X-Forwarded-Server] => example.com

I want those headers gone.

I seem to be unable to unset them with mod_headers. I can add more entries to them, but I can not remove them.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萝莉病 2024-12-10 02:15:38

从 Apache 2 开始,正如这个漂亮的答案所说,

ProxyAddHeaders Off

理论上禁用它。根据我的经验,它没有任何效果。然而,与

<Proxy *>
  ProxyAddHeaders Off
</Proxy>

和 结合起来,

  RequestHeader unset X-Forwarded-Host
  RequestHeader unset X-Forwarded-For
  RequestHeader unset X-Forwarded-Server

它开始在某个地方发挥作用。

Since Apache 2, as this pretty answer says, the

ProxyAddHeaders Off

theoretically disables it. In my experiences, it had no effect. However, combined with

<Proxy *>
  ProxyAddHeaders Off
</Proxy>

and, with

  RequestHeader unset X-Forwarded-Host
  RequestHeader unset X-Forwarded-For
  RequestHeader unset X-Forwarded-Server

somewhere it started to work.

回忆追雨的时光 2024-12-10 02:15:38

更正的答案:没有办法做到这一点,因为它

在 mod_proxy_http.c 的源代码中进行了硬编码以修复此问题,搜索以下部分:

    apr_table_mergen(r->headers_in, "X-Forwarded-Server",
                 r->server->server_hostname);
}

然后立即添加此代码:

// remove any X-Forwarded headers
apr_table_unset(r->headers_in, "X-Forwarded-For");
apr_table_unset(r->headers_in, "X-Forwarded-Host");
apr_table_unset(r->headers_in, "X-Forwarded-Server");

然后通过运行 apxs2 -cia mod_proxy_http 进行编译。 c

corrected answer: there is no way to do that since its hardcoded

to fix this in the source code of mod_proxy_http.c search for the following part:

    apr_table_mergen(r->headers_in, "X-Forwarded-Server",
                 r->server->server_hostname);
}

and immediately after that add this code:

// remove any X-Forwarded headers
apr_table_unset(r->headers_in, "X-Forwarded-For");
apr_table_unset(r->headers_in, "X-Forwarded-Host");
apr_table_unset(r->headers_in, "X-Forwarded-Server");

then compile by running apxs2 -cia mod_proxy_http.c

枕花眠 2024-12-10 02:15:38

我在 CentOS 5 上的 httpd 2.2 上遇到了同样的问题。无法安装 httpd 2.4。但由于某些原因我无法完全切换到nginx。所以我通过在httpd和目标地址之间插入nginx代理来做到这一点。所以我有: httpd(localhost:80/path) -> nginx(localhost:81/path) ->; http://your.destination/path。安装步骤如下:

  1. 按照这些说明安装nginx
  2. ,配置nginx以避免安全问题。
  3. 在 nginx 中添加一个位置,该位置将删除这些 httpd 的反向代理请求标头。它可以看起来像这样:

    位置/路径{
        proxy_set_header x-forwarded-for "";
        proxy_set_header x-转发主机“”;
        proxy_set_header x-转发服务器“”;
        proxy_pass http://your.destination/path;
    }
    

I had the same problem on httpd 2.2 on CentOS 5. Installing httpd 2.4 wasn't possible. But because of some reasons I couldn't switch to nginx completly. So I did it by inserting nginx proxy between httpd and the destination address. So I had: httpd(localhost:80/path) -> nginx(localhost:81/path) -> http://your.destination/path. Installation steps are the following:

  1. Install nginx according to these instructions
  2. Configure nginx to avoid security problems.
  3. Add an location in nginx that will remove those httpd's reverse proxy request headers. It can look like this:

    location /path {
        proxy_set_header x-forwarded-for "";
        proxy_set_header x-forwarded-host "";
        proxy_set_header x-forwarded-server "";
        proxy_pass http://your.destination/path;
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文