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]
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.