How to run node.js server with Nginx

May 16, 2018 4.8k views
Nginx DigitalOcean Node.js Ubuntu 16.04

Hi all,

I'm trying to figure out how to get my Node server.js to run on my droplet with Let's Encrypt / nginx

It was working by just ssh-ing into the droplet, cd into the project folder (cloned from Github repo) and running 'forever start server.js'.

But then I ran though this tutorial (https://www.youtube.com/watch?v=m9aa7xqX67c) to install a Let's Encrypt cert and now it only shows:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

I can start or kill that default nginx menu, but now I'm not able to cd into the project folder and run forever start. I looked through many articles on what may work or not but I can't figure anything out for the life of me. Definitely new to all the server / nginx stuff.

My /etc/nginx/sites-available/default looks like this:

server {
        listen 80;
        server_name mydomain.com www.mydomain.com;
        return 301 https://$host$request_uri;
}

server {
        listen 443 ssl;
        server_name mydomain.com www.mydomain.com;
        ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

...
}

I tired setting a proxy as I saw in another forum to the location, and setting my node server.js to listen on that port, but that didn't work either. It seems as if it's either going straight to the nginx window or to 'site cannot be reached' if nginx is stopped.

        location /  {
                proxy_pass    http://127.0.0.1:3010/;
                try_files $uri $uri/ =404;
        }

Any thoughts are appreciated!
Keith

1 Answer
aha MOD May 17, 2018
Accepted Answer

Hmmm all that sounds like it should work...

Just to be clear, this is what your nginx config should look like:

server {
        listen 80;
        server_name mydomain.com www.mydomain.com;
        return 301 https://$host$request_uri;
}

server {
        listen 443 ssl;
        server_name mydomain.com www.mydomain.com;
        ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
        location /  {
                proxy_pass    http://localhost:3010;
                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;
        }
}

and make sure your server.js node app is running on port 3010.

i.e.: in express.js...

const express = require('express');
const app = express();

app.get('/', function (req, res) { res.send('Hello World!')});

app.listen(3010, function() { console.log('Example app listening on port 3010!'); });

Then confirm nginx is running with sudo service nginx status and confirm your node app is running with forever list or check the forever logs with forever logs server.js

More reference on running node.js in reverse proxy with nginx here: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

by Brennen Bearnes
Node.js is an open source JavaScript runtime environment for easily building server-side and networking applications. Node.js applications can be run at the command line but this guide focuses on running them as a service using PM2, so that they will automatically restart on reboot or failure, and can safely be used in a production environment.
  • Oh my godddd it works! I had everything EXCEPT the few lines of

               proxy_http_version 1.1;
               proxy_set_header Upgrade $http_upgrade;
               proxy_set_header Connection 'upgrade';
               proxy_set_header Host $host;
    

    I also had this line - not sure if this was causing problems as well.

                try_files $uri $uri/ =404;
    

    All this is new to me (clearly). I appreciate you taking the time to reply. I'm so stoked now!

Have another answer? Share your knowledge.