How to run node.js server with Nginx
How to run node.js server with Nginx
NEW Join the DigitalOcean + DEV Hackathon. Build something awesome to end 2020.
QUESTION
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’.
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.
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;
...
}
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;
}
RELATED
Question
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
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;
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
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
Reply Report
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
I also had this line - not sure if this was causing problems as well.
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
QUESTION QUESTION
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
BECOME A CONTRIBUTOR
DigitalOcean Products Virtual Machines Managed Databases Managed Kubernetes Block Storage
Object Storage Marketplace VPC Load Balancers
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
https://www.digitalocean.com/community/questions/how-to-run-node-js-server-with-nginx 8/8