Configure URLs with prefix in Mojolicious behind Reverse Proxy (ProxyPass)

后端 未结 3 1643
攒了一身酷
攒了一身酷 2021-01-05 21:21

I\'m looking for a reliable way to configure Mojolicious running behind an Apache reverse proxy under /app, so that url_for(\'/foo\') actually returns /ap

3条回答
  •  無奈伤痛
    2021-01-05 21:50

    You need to set base path for each request url in before_dispatch hook

    $app->hook(before_dispatch => sub {
      my $c = shift;
      $c->req->url->base->path('/app/');
    });
    

    Example:

    use Mojolicious::Lite;
    
    app->hook(before_dispatch => sub {
      shift->req->url->base->path('/app/');
    });
    
    
    get '/' => sub {
      my $c = shift;
      $c->render(text => $c->url_for('test'));
    };
    
    get '/test/url' => sub { ... } => 'test';
    
    app->start;
    

    And result:

    $ curl 127.0.0.1:3000
    /app/test/url
    

提交回复
热议问题