Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I needed the forwarded host on a backend mod_perl server from mod_proxy under Apache 1.3. The mod_perl docs show how to accomplish this for IP by parsing the X-Forwarded-For header in startup.pl and setting $r->connection->remote_ip to that value. To avoid patching mod_proxy I added a few lines to mod_perl's My::ProxyRemoteAddr that sets $r->header_in('Host') to the value of the X-Forwarded-Host header.

It works but a question remains since I can't find examples online of sane people doing it this way. Is this a sound approach? If not, please correct me. If so, maybe the already very excellent mod_perl docs could benefit by adding this information. Thanks for your consideration.

sub My::ProxyRemoteAddr ($) { my $r = shift; return Apache::Constants::OK unless ($r->connection->remote_ip eq "127.0.0.1") and $r->header_in('X-Forwarded-For') and $r->header_in('X-Forwarded-Host'); if (my ($ip) = $r->header_in('X-Forwarded-For') =~ /([^,\s]+)$/) { $r->connection->remote_ip($ip); } if (my ($fh) = $r->header_in('X-Forwarded-Host') =~ /([^,\s]+)$/) { $r->header_in('Host',$fh); } return Apache::Constants::OK; }

Replies are listed 'Best First'.
Re: mod_perl ProxyPreserveHost Emulation
by perrin (Chancellor) on Jul 22, 2006 at 18:44 UTC
    How did this avoid patching mod_proxy? The mod_proxy that works with Apache 1.x doesn't pass X-Forwarded-For or X-Forwarded-Host unless you patch it.
      While searching for solutions I learned that mod_proxy has been passing X-Forwarded-For, X-Forwarded-Host and X-Forwarded-Server since version 1.3.25 from the following messages dated 2002 and 2003. So I tried and it worked. The relevant code in mod_proxy begins on line 315 of my copy of apache_1.3.36/src/modules/proxy/proxy_http.c.
        Good, that makes it easy. And your use of the X-Forwarded-Host header looks fine to me.