This is my setup for Mojolicious, but I think that the hoops I have to jump through there are not too different from the hoops for Dancer2:
My setup runs locally on http://localhost:3001 and gets remotely exposed as https://datenzoo.de/notes.
The Apache config is:
ProxyVia Block
SSLProxyVerify none
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off
<Location "/notes">
ProxyPass https://localhost:3001
RequestHeader set X-Forwarded-Host 'datenzoo.de'
Header edit Location ^https(\:\/\/localhost/.*)$ https://datenzoo.de/n
+otes/$1
Header edit Location ^https(\:\/\/localhost)$ https://datenzoo.de/note
+s/
</Location>
In my Mojolicious code, I have to munge each request to have the proper value for the (proxied) URL:
# If we are behind a reverse proxy, prepend our path
if ( my $path = $ENV{MOJO_REVERSE_PROXY} ) {
my $path_uri = Mojo::URL->new($path);
my @path_parts = grep /\S/, split m{/}, $path_uri->path;
app->hook( before_dispatch => sub( $c ) {
my $url = $c->req->url;
warn "URL is <$url>";
my $base = $url->base;
unshift @{ $base->path }, @path_parts;
$base->path->trailing_slash(1);
$url->path->leading_slash(0);
#$url->scheme($path_uri->protocol);
$base->scheme($path_uri->protocol);
if( my $f = $c->req->headers->header('X-Forwarded-Host')
and not $path_uri->host ) {
# We could guess the host here if it wasn't set in MOJO_RE
+VERSE_PROXY
# This requires that the outside-facing server resets
# X-Forwarded-Host , so that header is not allowed to be u
+ser-controllable
(my $host) = split /,/, $f;
#$url->host( $host );
$base->host( $host );
} else {
#$url->host($path_uri->host);
$base->host($path_uri->host);
}
warn "Base is <$base>";
warn "URL is now <$url>";
$url->base( $base );
});
}
This still requires the code to know (via $ENV{MOJO_REVERSE_PROXY} the external URL, but I found no better way.