Deploying a Node.js application
on Passenger + Nginx

This guide teaches you how you can deploy your app to a server. This guide assumes that you have already done the following, and that you know how to do them:

  • Passenger and Nginx are already installed on the server.
  • You have already transferred the application's code to the server.
  • You have already installed Node.js.
  • You have already installed all necessary dependencies that your application needs.

If you miss one of these prerequisites or don't know how to perform it, then we recommend you to follow the Deployment tutorial. The deployment tutorial is end-to-end guide and teaches you how to install every component.

Table of contents

  • Loading...

Deploying an app to a virtual host's root

To deploy an app to an Nginx virtual host's root path, the following steps must be taken:

  • Add a virtual host entry (server block) to your Nginx configuration file.
  • The server block's root must point to your application's public subdirectory.
  • You must also set passenger_enabled on in the server block.

Here is an example:

server {
    listen 80;
    server_name yourserver.com;

    # Tell Nginx and Passenger where your app's 'public' directory is
    root /path-to-app/public;

    # Turn on Passenger
    passenger_enabled on;
    # Tell Passenger that your app is a Node.js app
    passenger_app_type node;
    passenger_startup_file app.js;
}

Replace yourserver.com with your server's host name, replace /path-to-app with your application's code directory path and replace app.js with your app's entry point file.

When you are done, restart Nginx. If you installed Nginx via our Debian or RPM packages:

$ sudo service nginx restart

Otherwise, if you installed Nginx from source and don't have an init script:

$ sudo kill $(cat /opt/nginx/logs/nginx.pid)
$ sudo /opt/nginx/sbin/nginx

You are now done. If you run into any problems, please refer to the troubleshooting guide.

Deploying an app to a sub-URI or subdirectory

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.

You can also deploy an app to a sub-URI instead of the root URI. For example, suppose that you already have a virtual host for the application /websites/phusion:

http {
    ...

    server {
        listen 80;
        server_name www.phusion.nl;
        root /websites/phusion/public;
        passenger_enabled on;
    }
}

And you want your application, located in /websites/secondapp, to be accessible from the URL http://www.phusion.nl/subpath. To do this, you need to perform the following:

  • Create a location with parameter ~ ^/<SUBURI>(/.*|$). This is a regular expression that says: "match everything that is exactly , or starts with /".
  • Inside the location block, set alias <PATH TO YOUR APPLICATION PUBLIC SUBDIRECTORY>$1.
  • Inside the location block, set passenger_base_uri <SUBURI>.
  • Inside the location block, set passenger_app_root <PATH TO YOUR APPLICATION ROOT>.
  • Inside the location block, set passenger_document_root <PATH TO YOUR APPLICATIOS PUBLIC SUBDIRECTORY>.
  • Inside the location block, set app_type and startup_file accordingly.

Here is an example:

http {
    ...

    server {
        listen 80;
        server_name www.phusion.nl;
        root /websites/phusion/public;
        passenger_enabled on;

        # This block has been added.
        location ~ ^/subpath(/.*|$) {
            alias /websites/secondapp/public$1;  # <-- be sure to point to 'public'!
            passenger_base_uri /subpath;
            passenger_app_root /websites/secondapp;
            passenger_document_root /websites/secondapp/public;
            passenger_enabled on;

            passenger_app_type node;
            passenger_startup_file app.js;
        }
    }
}

When you are done, restart Nginx. If you installed Nginx via our Debian or RPM packages:

$ sudo service nginx restart

Otherwise, if you installed Nginx from source and don't have an init script:

$ sudo kill $(cat /opt/nginx/logs/nginx.pid)
$ sudo /opt/nginx/sbin/nginx

You are now done. If you run into any problems, please refer to the troubleshooting guide.