To enable this module you have to recompile the frontend server with
the following options:
Example 12-4. Book/ProxyRemoteAddr.pm
package Book::ProxyRemoteAddr;
use Apache::Constants qw(OK);
use strict;
sub handler {
my $r = shift;
# we'll only look at the X-Forwarded-For header if the request
# comes from our proxy at localhost
return OK unless ($r->connection->remote_ip eq "127.0.0.1") &&
$r->header_in('X-Forwarded-For');
# Select last value in the chain -- original client's IP
if (my ($ip) = $r->headers_in->{'X-Forwarded-For'} =~ /([^,\s]+)$/) {
$r->connection->remote_ip($ip);
}
return OK;
}
1;
Next, enable this handler in the backend's
httpd.conf file:
PerlPostReadRequestHandler Book::ProxyRemoteAddr
and the right thing will happen transparently for your scripts: for
Apache::Registry or
Apache::PerlRunscripts, you can access the remote
IP through $ENV{REMOTE_ADDR}, and for other
handlers you can use
$r->connection->remote_ip.
Generally, you shouldn't trust the
X-Forwarded-For header. You should only rely on
the X-Forwarded-For header from proxies you
control yourself—this is why the recommended handler we have
just presented checks whether the request really came from 127.0.0.1
before changing remote_ip. If you know how to
spoof a cookie, you've probably got the general idea
of making HTTP headers and can spoof the
X-Forwarded-For header as well. The only address
you can count on as being a reliable value is the one from
$r->connection->remote_ip.
| | |
12.8. mod_rewrite Examples | | 12.10. Frontend/Backend Proxying with Virtual Hosts |