1. Home
  2. Languages
  3. How to Serve the Static Content of Your Application

How to Serve the Static Content of Your Application

This article explains how to make sure that the static content of your Node.js, Ruby or Python application is served by the server.

The glossary contains explanations on a wide range of subjects and can be consulted to clarify certain terms.

Prerequisites

Enter the following address in your web browser: https: //mg.n0c.com/en/.

Introduction

N0C servers use Passenger to run your Node.js, Ruby and Python web applications.

One of the philosophies of Passenger is to take into account the fact that, in practice, many web applications share the same conventions with regard to structural characteristics. Among other things, most web applications have a directory in which they store static files.

To make life easier, Phusion Passenger “assumes” these conventions by default so that, if your application conforms to them, you will get a lot of out-of-the-box functionalities, without any configuration. In the case of Node.js, for example, Passenger considers that your application directory has the following structure:

directory of your application
  |
  +-- app.js
  |
  +-- public/
  |
  +-- tmp/

Where :

  • app.js is the entry point for your Node.js application. Phusion Passenger starts your application by loading this file. It must register a request handler, as we will see later;
  • the public directory contains static files that are automatically served by the web server. For example, if there is a public/image.jpg file, all requests to /image.jpg will be processed by the web server and never passed on to the application;
  • the tmp directory can be used by the application, but is also used by Phusion Passenger to restart the application.

In Ruby, config.ru is the entry point for your application. For Python, run.py is the entry point to use.

All your the static files of your application, such as images, css files, js files, etc., should be stored in the public directory. In addition, you need to place an .htaccess file in which you can specify to add cache headers and their expiry time. In fact, if the htaccess remains in public_html, it will not be used for your application.

To achieve this, you first need to prepare Phusion Passenger to serve the static content of your application. That is what this article is all about.

Automatic Preparation of Passenger

Some steps are automatic and performed in the MG Panel:

  • creation of directory structure;
  • defining app.js, config.ru or run.py as the entry point for your application.

The remaining steps are manual.

Manual Preparation of Passenger

Here are the manual steps you need to follow to get Phusion Passenger to serve and manage your application.

Step 1 — Configure and Restart Your Application

Once you have configured your application in the language of your choice, simply restart it. This triggers the start-up of your Phusion Passenger application.

Please refer to the following articles, which explain how to configure and restart your application:

Step 2 — Create a Public Directory for Static Files

Create a public directory and move all static files into it.

Please refer to the paragraph explaining how to move a file in the article “How to use the file manager”.

Step 3 — Place the .htaccess File in the Public Directory

By default, N0C storage contains a public directory. All you need to do is place the .htaccess file in it.

You can find the folder containing the .htaccess of your application by going to the “File manager” option on your N0C panel. On this page, you will be able to find all your domains as well as the root of the document linked to that domain.

Please refer to the article How to use the file manager.

If a framework was used and it did not have a publicfolder generated (or if it had a different name, such as “browser”), you would need to create a symlink named public that would point to the public folder generated by the framework.

Documentation

You can find documentation on the subjecton the Web, in particular at: https://github.com/phusion/passenger/wiki/Phusion-Passenger%3A-Node.js-tutorial#prepare-your-app.

Updated on November 20, 2024

Related Articles