apachemod-rewriteapache2mod-proxymod-headers

Is there a way to remove apaches Reverse Proxy Request 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?


Solution

  • 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