How can I get Node.js to listen on port 80?

September 3, 2013 36.1k views
Hi! I followed the instructions here, , and I got a Node app up and running on port 3000. When I direct requests to that port, I see my app, but not when I send a request without specifying a port. I tried running `$ PORT=80 node app`, and Node threw an error and quit. It worked when I used PORT=8080 though. On IRC, @kamal_ suggested I install and configure NGINX to to serve the static files "and pass everything else to Node.js", and now I have NGINX up and running. When I go to my droplet's IP now, I see "Welcome to nginx!" instead of having the connection refused, but I still have to append :3000 to the URL to see my Node app. This is the NGINX config file I got from @kamal_: What are the next steps to making the app load on requests to the default port? Thanks in advance!
9 Answers
For people looking for a quick and easy solution, not the most robust, but it will get you running, try:

redirect port 80 to port 3000 with this command:

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000

Then launch Node.js on port 3000. Requests to port 80 will get mapped to port 3000.

You should also edit your /etc/rc.d/rc.local file and add that line minus the sudo. That will add the redirect when the machine boots up.

found this (here)[] Hope this helps someone.
  • I did this, didn't work... How do I restore the defaults?

    edit: apparently sudo iptables -F resets iptables to default. still not working though, maybe i screwed something else up along the way.

    edit 2: now it's working again, just a slight delay.

Oy, my links did not show up in the previous post. Here they are:

Tutorial: How To Install Node.js with NVM (Node Version Manager) on a VPS

NGINX config file:
by Nik van der Ploeg
Here's our tutorial on installing Node.js with NVM on your cloud server.
I found a solution, that someone else shared in the IRC channel:

$ setcap 'cap_net_bind_service=+ep' /path/to/.nvm/v0.10.17/bin/node
$ apt-get remove nginx
$ cd /path/to/app
$ PORT=80 node app

When I first read the suggestion to use setcap, I thought I was supposed to pass in /path/to/app.js as the final argument, and that obviously did not work. Now that I used the path to node, I can set the port without any trouble.

I'm sure there are good reasons to use NGINX. If so, I'm open to using that configuration once I can figure it out.
Great, glad it's working now :]

It's usually better to put nodejs behind nginx as nginx does a few things faster than nodejs such as serving static files, proxying, url rewriting, SSL, redirection, load balancing, and more.
Thanks again, Kamal! It's obvious I need to learn nginx soon. I'm just glad to get it working!
So, for reasons that are probably obvious to the seasoned VPS pros out there, this solution was not robust enough, and I needed to follow up on Kamal's advice sooner rather than later. The main trouble was that Node.js doesn't just keep running by itself with the above command. It needs a script called Forever to work properly, and a cronjob to restart the app when the machine reboots.

I thought I should find some entry-level information before coming back to Kamal for more advice, though. I'm grateful for the support, but I don't want to overwhelm his generosity with basic questions.

Who knew this was here? It's the first hit on Google when I searched for "vps set up node.js nginx", and it was the perfect starting point for me.

The differences between the virtualhost config file from the tutorial and the one linked above are a bit arcane for me, and if anyone can explain it to me, I'd be much obliged.

Are there any other issues I should look out for at this point? What questions should I be asking?

Thank you again, Kamal, so much for your guidance!
John, I'm really glad to help!

The difference between and is:

  • The former allows nginx to serve the static assets, while the latter just passes every single request to nodejs.
  • proxy_passing to an upstream instead of http://localhost:port allows you to deploy multiple nodejs instances and have nginx load balance between all of them by adding the IP addresses/ports to the node upstream.

Other than that, everything is pretty much the same.

You also need to keep the nodejs process running even if you're using nginx to reverse proxy to nodejs so you will have to use Forever or a similar program.

I'm really glad you want to learn how to run stuff and make them work on a VPS instead of just wanting everything to be running as quick as possible without understanding what you're doing. Keep up the good work and do not hesitate to post more questions, I'll be glad to help!

by Christian Fei
This is our tutorial on hosting multiple Node.js apps on a single VPS with nginx, forever, and crontab.
Thanks for this ressources! Check this course it's straight to point and it gives you all the keys to a good start for a complete masteration of node.js by taking all the power of it.
  • That is a pretty decent tutorial. I actually bought it before seeing this post... still haven't finished it but its good so far :)

Launching the app with sudo works fine. There's probably security implications. I'm not a backend guy. But it will work for now.

Edit: No, it's not... I give up.

Have another answer? Share your knowledge.