Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Mojolicious, apache reverse proxy

by slatibart (Sexton)
on Sep 19, 2019 at 13:51 UTC ( #11106378=perlquestion: print w/replies, xml ) Need Help??

slatibart has asked for the wisdom of the Perl Monks concerning the following question:


I have apache running and need to run a mojolicious app under the path /rules

This is the apache config part

ProxyRequests Off ProxyPreserveHost On ProxyPass /rules http://localhost:3000/ keepalive=On ProxyPassReverse /rules http://localhost:3000/ RequestHeader set X-Forwarded-HTTPS "0"
Very simply mojolicious script with a route to / and a route to /user
app->hook(before_dispatch => sub { my ( $c ) = @_; my $url = $c->req->url; my $base = $url->base;# push @{ $base->path }, 'rules'; $base->path->trailing_slash(1); $url->path->leading_slash(0); }); get '/' => sub { my $self = shift; my $host = $self->tx->remote_address; my $content = "Host: $host"; $self->stash(content=>$content); $self->render('index'); }; get '/user' => sub { my $self = shift; my $host = $self->tx->remote_address; my $content = "Host: $host"; $self->stash(content=>$content); $self->render('index'); }; app->start; __DATA__ @@ index.html.ep % layout 'default'; <%== $content %> @@ layouts/default.html.ep <!DOCTYPE html> <html lang="en"> <head> <title>Test</title> </head> <body> %= button_to Home => '/' %= button_to User => '/user' %= link_to Home => '/' %= link_to User => 'user' %= content </body> </html>
Generated html looks good.
<!DOCTYPE html> <html lang="en"> <head> <title>Test</title> </head> <body> <form action="/rules"><input type="submit" value="Home"></form> <form action="/rules/user"><input type="submit" value="User"></form> <a href="/rules">Home</a> <a href="/rules/user">User</a> Host: </body> </html>
Using the home button / links works but for route /user mojolicous throws an error.

"None of these routes could generate a response for your GET request for //user, maybe you need to add a new one?"

Where is that extra slash coming from and how to get rid of that ?

Bonus question. If I add another layer   push @{ $base->path }, 'all/rules'; all slashes are transformed.

<form action="/all%2Frules"><input type="submit" value="Home"></form> <form action="/all%2Frules/user"><input type="submit" value="User"></f +orm> <a href="/all%2Frules">Home</a> <a href="/all%2Frules/user">User</a>
Any idea why ?

Thanks for your help.

Replies are listed 'Best First'.
Re: Mojolicious, apache reverse proxy
by daxim (Curate) on Sep 19, 2019 at 15:19 UTC
      Thanks for the hint.

      I've added AllowEncodedSlashes On to my vhost but it didn't changed the picture. These encoded urls paths are created by mojolicious and I have no idea why or find a hint in the documentation. And I've searched a lot.

Re: Mojolicious, apache reverse proxy
by Your Mother (Archbishop) on Sep 21, 2019 at 07:42 UTC

    Absolutely untested and inexperienced idea.

    push @{ $base->path }, "all", "rules";

    I sincerely doubt this is the right way to solve this but donít know mojo well enough to give a qualified answer. I expect this might at least do the path properly. If you donít get a full answer in another day, try Stack Overflow where you might get more attention from more mojo users.

      Surprisingly that works ! In a way it makes sense, however I don't think this ever documented somewhere.
Re: Mojolicious, apache reverse proxy
by trwww (Priest) on Sep 21, 2019 at 04:09 UTC

    Would love to see an answer of substance. Any time I've tried to get web apps to work under a "relative" URI scheme in all of the frameworks I've ever tried, bugs crawl out of the woodwork.

    I've long given up on the concept and build my apps to assume that they run under an "absolute" path, with the first part of the path like "/appname".

      Really? Itís semi-trivial in Catalyst and, to my knowledge but not experience, Mojolicious too. In Catalyst itís just ensuring you only use uri_for or especially uri_for_action because it errors on non-existent dispatch paths (also uri_with) to construct all URIs. They are all relative to the app base in that case.

        I'll freely admit that the most likely scenario is that I'm just dumb, and like I say its been a long time since I tried it, but re: uri_for and friends I feel like I remember it working but then my chains being busted because they choke on the prefix of the endpoint and/or other weird stuff. And OP has an example of stuff not working in Mojolicious in this very node, no?

        So I've ended up with a convention (that at this point I've cargo-culted from myself), using OPs example, like:

        ProxyPass /rules http://localhost:3000/rules keepalive=On ProxyPassReverse /rules http://localhost:3000/rules
Re: Mojolicious, apache reverse proxy
by Anonymous Monk on Sep 21, 2019 at 11:39 UTC
      So my bonus question is solved and I am able to mount my app under whatever/pat/needed

      However the routing is borked.

      Using the home button / links works but for route /user mojolicous thr +ows an error. "None of these routes could generate a response for your GET request f +or //user, maybe you need to add a new one?"
      I have to figure out where the extra slash is coming from and where to get rid of it. Simply adding another / in the get doesn't work

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11106378]
Approved by marto
Front-paged by Discipulus
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2022-01-26 18:07 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (69 votes). Check out past polls.