Question

How to run node.js server with Nginx

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


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hello,

In addition to what has already been mentioned, I could also suggest taking a look at this quick video tutorial on how to deploy a Node.js App on Ubuntu with PM2, NGINX and Cloudflare:

Hope that this helps. Best, Bobby