I wrote a Dancer2 application to be used by about 10 people from work. It ran fine on http://localhost, but the pages needed to be available from the outside world. Our IT admin told me the standard practice is to run it on a virtual host and use Apache's mod_proxy.

Dancer2::Manual::Deployment even mentions such a possibility, so I followed the instructions. I included

behind_proxy: 1
to config.yml, and the admin configured the proxy similarly as shown in the manual (see Using-Apache's-mod_proxy of Dancer2::Manual::Deployment.

I was given a prefix under which the app would be running, e.g. https://example.com/prefix/login.

But it doesn't work correctly. For example, the css files are ignored. Or rather, they can't be found.

The main.tt template was created with the following link (part of the initial scaffolding):

<link rel="stylesheet" href="<% request.uri_base %>/css/style.css">

But the request.uri_base doesn't expand to /prefix, it remains empty.

Similarly, I use Dancer2::Plugin::Auth::Tiny for user authentication. Again, I almost copied verbatim the synopsis code:

get '/' => needs login => sub { # ... }; get '/login' => sub { template 'login' }; post '/login' => sub { my $user = body_parameters->get('uname'); if (is_valid($user, body_parameters->get('psw'))) { session(user => $user); redirect('/') } else { template index => {error => 'Wrong username or password'} } };

But again, when I try to open the page, the authentication plugin redirects the browser to /login instead of /prefix/login.

I was able to fix the problems by

Mentioning the prefix in the config definitely feels wrong. Hardcoding the prefix into the app? It also means the app can't be run locally on localhost for testing anymore.

How should I properly write the app, configure it, and configure Apache to make it work both locally and in production, without hardcoding the prefix anywhere in the app?

Interestingly, all Python flask and whatever apps written by other colleagues run as written without problems.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Dancer2 App Deployment through Apache Proxy by choroba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.