nginxmojolicious

nginx with multiple services, clean url for internal (xhr) app requests?


I have two apps (Mojolicious) on the same server. I use nginx to route requests to each app depending on the incoming URL. App A is called for urls starting with http://1.2.3.4/A/... and similarly with App B.

Both apps are interactive, for example on clicking a button I want to POST to same server via XHR. In addition to the XHR requests, I have CSS and JS files.

I know how to remove the "A" and the "B" at the nginx level e.g.

location ~ ^/A(.*)$ {
  proxy_pass http://127.0.0.1:3000$1;
  ...
}

But when I do this obviously the XHR requests do not work because they go back to nginx without that "A" or "B".

Do I have to hardcode the "A" and the "B" in each url mentioned in each app? Do I have to say "/A/css/my.css" and "/B/css/my.css"?

I am looking of a way to remove the "A" and the "B" from each app for portability and simplicity.

And I am a bit confused too.


Solution

  • Mojo handler:

    (e.g., App A)
    get '/css/:file' => sub {
        my $c = shift;
        my $file = $c->param('file');
        # Remove the "A" prefix from URL
        $file =~ s{^A/}{};
        $c->reply->static("public/css/$file");
    };
    

    Nginx configuration:

    location /A {
        rewrite ^/A(.*) /$1 break;
        proxy_pass http://127.0.0.1:3000;
    
    }