Nginx with PM2 for NuxtJs

by theDataTalks . 19 Apr 2020

The configurations & settings mentioned in this article were tested on Ubuntu operating system with
Node.js server. Nginx is used as a reverse proxy & PM2 is used as a production process manager.


Install Node.js, Nginx & PM2 with the proper version.
Install two NuxtJs applications example1 & example2.
And we have two domains & with one public IP

Nginx server blocks

Create two server block files

# From any folder, go to 
Nginx server block files location
$ cd /etc/nginx/sites-available/

# Create server block files & delete default file
$ ls >
$ ls >
$ rm default

Edit server block files

Read for the latest changes on Nginx server block settings. will run on port & will run on port

# Open the file in the nano editor
$sudo nano
# Enter the below configurations inside the file
map $sent_http_content_type $expires {
"text/html"                 epoch;
"text/html; charset=utf-8"  epoch;
default                     off;
server {
listen          80;# the port nginx is listening on
# setup your domain here
gzip            on;
gzip_types      text/plain application/xml 
                text/css application/javascript;
gzip_min_length 1000;
location / {
 expires $expires;
 proxy_redirect          off;
 proxy_set_header Host       $host;
 proxy_set_header X-Real-IP  $remote_addr;
 proxy_set_header X-Forwarded-For    
 proxy_set_header X-Forwarded-Proto  $scheme;
 proxy_read_timeout          1m;
 proxy_connect_timeout       1m;
    #set the address of the Node.js instance here
# Save & close the file
# Similary open file, 
enter the above configurations, 
save & close the file.
# Be sure to changes server_name & proxy_pass with 
& "" respectively

# Create symbolic links for server blocks from 
sites-available folder to sites-enabled folder
$ sudo ln -s /etc/nginx/sites-available/ 
$ sudo ln -s /etc/nginx/sites-available/ 

# Delete default file from sites-enabled folder
$ sudo rm /etc/nginx/sites-enabled/default

Start Nginx server

# To start & stop nginx
$ sudo service nginx start
$ sudo service nginx stop

# To test nginx for any errors
$ sudo nginx -t

# To restart nginx
$ sudo systemctl restart nginx

PM2 configuarions

Assumed two nuxt application installed in the below folder location

# Create PM2 ecosystem.config.js file. 
#"PM2 init" command creates ecosystem.config.js file
$ cd /home/user/
$ sudo PM2 init

#Edit ecosystem.config.js file in nano editor
$ sudo nano ecosystem.config.js
# Enter the below settings
module.exports = {
apps : [
 {	name: 'example1',
    port: 3000,
	script: "./node_modules/nuxt/bin/nuxt.js",
	args: 'start',
{	name: 'example2',
    port: 4000,
    script: "./node_modules/nuxt/bin/nuxt.js",
    args: 'start',
# Save & close the file

Start applications

The below command will start example1 & example2 applications.
You have successfully configured and you can test
as & in the browser.

$ PM2 start

Reference for the above article:

Don't be in delusion, understand the reality from historical data | Copyright © 2020 theDataTalks