How to run Node.js server with Nginx

guide node nginx

Alex Whinfield 11th February 2020

Node.js is an open source Javascript runtime environment built on top of Chrome’s V8 engine. It's rise in popularity over the last few years is due to many factors, some of which are its impressive performance benchmarks and ease of portability between systems.

If you’re looking to host a Node.js application on DigitalOcean then this guide should be a useful tool in getting you up and running quickly!

Before we get started

If you have not done so already, please setup a new Digital Ocean droplet using Ubuntu 18.04. You can do this by creating a new droplet from within your dashboard and selecting the Ubuntu 18.04 distribution when asked which image you would like to install.

Create Droplet Distro Choice

Create Droplet Distro Choice


We are also assuming for this example your Node.js application will be running on 127.0.0.1:3000.


Step 1 - Installing Node.js v12

After accessing your Droplet through SSH we can go ahead and begin installing Node.js v12 (LTS, long term support) with the following commands:

curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
apt-get install -y nodejs

We can check our installation was successful by verifying the newly installed Node.js version `node -v`


Step 2 - Setting up Nginx

Before we install Nginx we are also going to make sure our packages are up to date as well using `apt-get update`.

apt-get update
apt-get install nginx -y


Just like with the Node.js installation we can verify our install was successful by checking the Nginx version number `nginx -v`.

By default Nginx will not start automatically on the server, so you will need to use the following command to get everything up and running.

sudo /etc/init.d/nginx start

At this point in the guide you should now be able to see the Nginx default welcome page on your droplet by visiting the droplet’s public IP address (you can find this in the Digital Ocean dashboard or by running `hostname --all-ip-addresses` within your terminal).

Welcome to nginx

Welcome to nginx

Now that we’ve installed Node.js v12 and Nginx successfully, the next part of this guide will focus on configuring Nginx to work with your node application’s entry port.

Step 3 - Configuring Nginx

All Nginx configuration files are located within the `/etc/nginx` directory, with the primary configuration file being `/etc/nginx/nginx.conf`.

Firstly, we are going to create a new configuration file for our application. This new file can be named anything you wish but for this example I am going to go with `app.conf`.

It’s important at this stage to ensure the entry port you are going to be using for your Node.js application is set within the configuration file.

 vim /etc/nginx/conf.d/app.conf


server {
 listen 80;
 server_name domain.co.uk;

 location / {
   proxy_pass http://localhost:3000;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection 'upgrade';
   proxy_set_header Host $host;
   proxy_cache_bypass $http_upgrade;
  }
}

 You can check your Nginx configuration contains no obvious errors using `sudo nginx -t`.

Now that we’ve set our configuration to work with the application’s entry port you can now go ahead and remove the default configuration and restart our Nginx instance.

sudo rm /etc/nginx/sites-enabled/default

sudo systemctl restart nginx


Conclusion

Congratulations! You should now have your Node.js application running through Nginx’s reverse proxy server. The great thing about this configuration is that it can be expanded to work with additional entry ports by appending new `location` blocks:

 location /api/ {
  proxy_pass http://localhost:3001;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection 'upgrade';
  proxy_set_header Host $host;
  proxy_cache_bypass $http_upgrade;
}

We hope you found this guide to be helpful and if you have any questions please don’t hesitate to get in touch with us.

Get Started Now

Get started with SnapShooter for free!
Backup one droplet, volume or mysql database