Mailing List Archive

[mod_backhand-users] proxied mod_perl redirects
> On a separate note, the one remaining issue I have with using
> mod_backhand as a proxy for mod_perl is the lack of an analogue to
> mod_proxy's ProxyPassReverse directive.
>
> Under my current setup mod_backhand is on port 80 and mod_perl is on
> port 81. If mod_perl issues a redirect (for instance from
> 'http://mydomain.com' to 'http://mydomain.com/') the resulting url is
> passed directly back to the client. I'd like to be able to munge the
> headers a bit, so I can force the client to come back through
> mod_backhand.
>
> I'd like to be able to modify the headers at step #4 below.
>
> 1.) client->backhand: GET http://mydomain.com
> 2.) backhand->mod_perl: forwards to mod_perl
> 3.) mod_perl->backhand: redirect to http://mydomain.com:81/
> 4.) backhand->client: redirect to http://mydomain.com:81/
>
> Has anyone thought about or done anything along these lines?
>
> -Blake

Well, I've come up with a workable solution for the above situation.
I wrote a perl module to override the directory redirection behavior
of my mod_perl servers. Since mod_backhand forwards the 'Host'
header, I use it to construct the redirected URL.

The above scenerio now becomes:

1.) client->backhand: GET http://mydomain.com
2.) backhand->mod_perl: forwards to mod_perl (including Host:mydomain.com)
3.) mod_perl->backhand: redirect to http://$HOST/ (i.e. http://mydomain.com/
4.) backhand->client: redirect to http://mydomain.com/

This solves for my specific case, but it might take some tweaking to
get it to work in similiar situations. Of course it also relies on
proxying to a mod_perl enabled server, thus its scope is much more
limited than mod_proxy's 'ProxyPassReverse' directive. I'd still like
to see that functionality added to mod_backhand, but this might be
helpful to someone until then.

-Blake


The two directives you'll need in mod_perl's httpd.conf file are:

UseCanonicalName Off
PerlTransHandler Apache::ProxiedDirectoryRedirect

Feel free to distribute/modify at will.

-------------- BEGIN Apache::ProxiedDirectoryRedirect.pm -------------
package Apache::ProxiedDirectoryRedirect;

use strict;
use Apache::Constants qw(:common REDIRECT);

sub handler {
my $r = shift;
my $uri = $r->uri;
my $filename = $r->document_root . $r->uri;
if ($filename !~ m|/$| && -d $filename) {
my $host = $r->header_in('host');
$host ||= $r->server->server_hostname();
$r->header_out(Location=>"http://$host" . $r->uri . '/');
$r->status(REDIRECT);
$r->send_http_header;
return 'REDIRECT';
} else {
return DECLINED;
}
}

1;
-------------- END Apache::ProxiedDirectoryRedirect.pm -------------