0% found this document useful (0 votes)
10 views

How to run node.js server with Nginx

Explain how to deplaoy a node js server with nginx proxy

Uploaded by

patacq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

How to run node.js server with Nginx

Explain how to deplaoy a node js server with nginx proxy

Uploaded by

patacq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

22/12/2020 How to run node.

js server with Nginx | DigitalOcean

NEW Join the DigitalOcean + DEV Hackathon. Build something awesome to end 2020.

QUESTION

How to run node.js server with


4

Nginx
Posted May 16, 2018  136.7k Nginx Node.js DigitalOcean 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;

https://www.digitalocean.com/community/questions/how-to-run-node-js-server-with-nginx 1/8
22/12/2020 How to run node.js server with Nginx | DigitalOcean

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

Add a comment Subscribe By: keithj0nes

RELATED

App Platform: Run Node apps without managing servers


Product

My Nodejs Backend ends is not accessible

Question

cpu from 13% to 100% in one minute

Question

https://www.digitalocean.com/community/questions/how-to-run-node-js-server-with-nginx 2/8
22/12/2020 How to run node.js server with Nginx | DigitalOcean

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.

1 answer

AHA  May 17, 2018


7

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…

https://www.digitalocean.com/community/questions/how-to-run-node-js-server-with-nginx 3/8
22/12/2020 How to run node.js server with Nginx | DigitalOcean

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

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,

Reply Report

keithj0nes May 17, 2018

2
Oh my godddd it works! I had everything EXCEPT the few lines of

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;

https://www.digitalocean.com/community/questions/how-to-run-node-js-server-with-nginx 4/8
22/12/2020 How to run node.js server with Nginx | DigitalOcean

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!
Reply Report

Submit an Answer

Related

QUESTION QUESTION

My Nodejs Backend cpu from 13% to 100% in


ends is not accessible one minute
I am new to digital ocean I have a website (it's the
after following tutorials on only site hosted on the
deploy an mern stack droplet) and sometimes
application, i am serving (about 1 time each month,
the api and front the api, but not on the same day)

QUESTION QUESTION

Problem installing PHP Error Log:


wordpress on authz_core:error - For
subdomain (nginx / files that don't exists on
ubuntu) my server

https://www.digitalocean.com/community/questions/how-to-run-node-js-server-with-nginx 5/8
22/12/2020 How to run node.js server with Nginx | DigitalOcean

Hi, I've been trying to I am getting hundreds of


configure my single hits on my server over time
droplet to host two that generate an error like

Looking for something else?

 Ask a new question  Search for more help

GET OUR BIWEEKLY NEWSLETTER

Sign up for Infrastructure as a


Newsletter.

HUB FOR GOOD

Working on improving health


d d ti d
https://www.digitalocean.com/community/questions/how-to-run-node-js-server-with-nginx i 6/8
22/12/2020 How to run node.js server with Nginx | DigitalOcean

and education, reducing


inequality, and spurring
economic growth? We'd like to
help.

BECOME A CONTRIBUTOR

You get paid; we donate to tech


nonprofits.

Featured on Community Kubernetes Course Learn Python 3 Machine Learning in Python


Getting started with Go Intro to Kubernetes

DigitalOcean Products Virtual Machines Managed Databases Managed Kubernetes Block Storage
Object Storage Marketplace VPC Load Balancers

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the


cloud and scale up as you grow – whether you’re
running one virtual machine or ten thousand.

Learn More

https://www.digitalocean.com/community/questions/how-to-run-node-js-server-with-nginx 7/8
22/12/2020 How to run node.js server with Nginx | DigitalOcean

Company

About
Leadership
© 2020 DigitalOcean, LLC. All rights reserved.
Blog
Careers
Partners
Referral Program
Press
Legal
Security & Trust Center

Products Community Contact

Pricing Tutorials Get Support


Products Overview Q&A Trouble Signing In?
Droplets Tools and Integrations Sales
Kubernetes Tags Report Abuse
Managed Databases Product Ideas System Status
Spaces Write for DigitalOcean
Marketplace Presentation Grants
Load Balancers Hatch Startup Program
Block Storage Shop Swag
API Documentation Research Program
Documentation Open Source
Release Notes Code of Conduct

https://www.digitalocean.com/community/questions/how-to-run-node-js-server-with-nginx 8/8

You might also like