pauljohn32
pauljohn32

Reputation: 2255

Passenger sub uri's

The Phusion Passenger instructions are great in most regards. They have far better-than-average install guides on setting up NGINX, the passenger app, testing python, and so forth. The instructions on setting up several separate apps on a single server are, well, deficient. The main reason I adopted Passenger is the ability to host several apps.

I followed the Phusion Passenger instructions for setting up NGINX server with a sub URI (https://www.phusionpassenger.com/library/deploy/nginx/deploy/python/). I thought this would allow me to run separate apps with different sub folders. I have several python applications in /var/www, like so:

/var/www/dashboard
/var/www/peniso

I have a Python virtual environment for each one, in the separate subfolders venv. Each one works individually. But how to make all available at once? The problem I see is that no matter which sub URI is used, the same app runs.

Here's what I tried in my /etc/nginx/sites-enable/dashboard.conf file:

server {
    listen 80;
    server_name testapp.myexample.com;

    # Tell Nginx and Passenger where your app's 'public' directory is
    root /var/www/dashboard/public;

    passenger_app_type wsgi;
    passenger_startup_file passenger_wsgi.py;

    # Turn on Passenger
    passenger_enabled on;
    passenger_python /var/www/dashboard/venv/bin/python3.7;

    location ~ ^/mydash(/.*|$) {
        alias /var/www/dashboard/public$1;
        passenger_base_uri /mydash;
        passenger_app_root /var/www/dashboard;
        passenger_document_root /var/www/dashboard/public;
        passenger_enabled on;
        passenger_python /var/www/dashboard/venv/bin/python3.7;
   }


   location ~ ^/efergy(/.*|$) {
        alias /var/www/peniso/public$1;
        passenger_base_uri /efergy;
        passenger_app_root /var/www/peniso;
        passenger_document_root /var/www/peniso/public;
        passenger_enabled on;
        passenger_app_env development;
        passenger_python /var/www/peniso/venv/bin/python3.7;
   }
}

I browse http://testapp.myexample.com/mydash or http://testapp.myexample.com/efergy I see is the same app showing. In the top part, before the sub-sections, I can replace "dashboard" with "peniso" and it changes which 1 app runs. I've flipped the ordering of the folders, also changed the root from one to the other. Still only one app seems to be available.

How do you configure Passenger to work with several different apps?

Eventually, I need to host some Python and some Node.js apps, once I understand how to get the directories under /var/www to work right.

Upvotes: 1

Views: 301

Answers (1)

pauljohn32
pauljohn32

Reputation: 2255

I have a solution that is specific to Python Ploty dashboard written with the abstraction layer called Dash. I expect similar fix can be found for all Plotly apps, but did not implement it. The Dash Plotly app can be modified to understand the suburi of the call. If the web URL is http://example.com/energy, then inside the Plotly app a simple change is needed.

Begin with the standard standard section:

app = dash.Dash(__name__,
    meta_tags=[
        {
            "name": "viewport",
            "content": "width=device-width"
        }
    ]
)

to include the last part of the web address

app = dash.Dash(__name__,
    meta_tags=[
        {
            "name": "viewport",
            "content": "width=device-width"
        }
    ],
    requests_pathname_prefix='/energy/'
)
app.config.suppress_callback_exceptions = True

The last line there is something I picked up in the Plotly list at the same time, I do not know if it is vital.

In the Passenger documentation, I've discovered that the sub-uri problem is serious for all kinds of apps except Rails, where they have a pure-passenger solution. The whole thing is not very well documented. But they do have a warning in the node.js documents (https://www.phusionpassenger.com/library/deploy/nginx/deploy/nodejs/):

Sub-URI deployments in Node.js require framework-specific 
adjustments in the application. For example, in Express 4.0+, 
you should use a router. An alternative is to use url 
rewriting to avoid the need for sub-URIs altogether. 

In the Passenger GitHub issue list, I created a request for documentation details on the "use of url rewriting" because that appears the only truly feasible answer in most cases (https://github.com/phusion/passenger/issues/2254). It is not workable for me to figure out case-specific fixes for every different node.js app that we want to run.

Just so everybody understands the situation, the Passenger framework structure does work to guide the server to the correct folder, but when it comes time to actually execute the app, nginx & Passenger, for reasons I don't understand, revert to run the first app they find in the configuration file. As soon as you fix this so each app knows how to be called--maybe nginx reads their announcements??--then this does work.

Upvotes: 0

Related Questions