Thank you and all others who helped me. I had a conversation with
brtastic on Perl Applications and Algorithms Discord, he first recommended trying
Plack::Middleware::ReverseProxy, but it didn't solve everything, so I wrote my own middleware like this:
package Plack::Middleware::Proxy;
use warnings;
use strict;
use parent 'Plack::Middleware';
use Plack::Util::Accessor qw( prefix scheme );
sub call {
my ($self, $env) = @_;
if ($env->{REMOTE_ADDR} ne '127.0.0.1') {
$env->{SCRIPT_NAME} = $self->prefix;
$env->{HTTP_X_FORWARDED_PROTO} = $self->scheme;
}
my $res = $self->app->($env);
return Plack::Util::response_cb($res, sub {
my $res = shift;
if ($res->[0] == 302) {
my %header = @{ $res->[1] };
my $prefix = $self->prefix;
$header{Location} =~ s{^\Q$prefix/}{/$prefix/};
@{ $res->[1] } = %header;
}
return
});
return $res
}
__PACKAGE__
It sovles several problems at once:
- It fixes links like /login to include the prefix when rendered through the proxy. All such links must have the form <% response.uri_base %>/path in the template files.
- On the VM, the server runs on http, but it's only accessible from the outside world through https (the proxy redirects http to https). Some links kept the http scheme, this fixes the behaviour.
- In the code, I have several instances of redirect('/');. For some reason, this lead to errors with the path becoming prefix/prefix, the postprocessing of the response fixes this. I had to use the callback, because the app also streams files, so a simple change to $res would crash the download.
I use it like this:
use Plack::Builder;
builder {
enable 'Plack::Middleware::Proxy', prefix => 'prefix', scheme => '
+https';
'MyApp'->to_app;
};
So, in the end, I also need to have the prefix in the code, but at least as a parameter to middleware, which seems much nicer.
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]