Best Practices When Running Node.Js With Port 80 (Ubuntu/Linode)

Best practices when running Node.js with port 80 (Ubuntu / Linode)

Port 80

What I do on my cloud instances is I 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 I launch my Node.js on port 3000. Requests to port 80 will get mapped to port 3000.

You should also edit your /etc/rc.local file and add that line minus the sudo. That will add the redirect when the machine boots up. You don't need sudo in /etc/rc.local because the commands there are run as root when the system boots.

Logs

Use the forever module to launch your Node.js with. It will make sure that it restarts if it ever crashes and it will redirect console logs to a file.

Launch on Boot

Add your Node.js start script to the file you edited for port redirection, /etc/rc.local. That will run your Node.js launch script when the system starts.

Digital Ocean & other VPS

This not only applies to Linode, but Digital Ocean, AWS EC2 and other VPS providers as well. However, on RedHat based systems /etc/rc.local is /ect/rc.d/local.

Node.js app can't run on port 80 even though there's no other process blocking the port

The error code EACCES means you don't have proper permissions to run applications on that port. On Linux systems, any port below 1024 requires root access.

Node webserver needs the port number

Your server is listening on pot 8080, when there is no port on the URL, that means that the server is listening on port 80 (or 443 for HTTPS).

You should read the documentation for your server and see where the setting for the listening port resides and change the 8080 into 80.

For example, in a random script.js file using net or http modules:

server.listen(8080); // Requires that you add :8080 to the URL

server.listen(80); // Doesn't requires that you add :80 to the URL,
// if you add it it will be removed by your browser.

Node.js for a website

So, sort of yes, but also sort of no. To build a back end with node you need to use a framework like express, and to serve it you'd need something like this. But to safely expose it to the open internet, you should definitely use apache or nginx to proxy requests to the port that your app is listening on. This is because it's extremely unsafe to expose your node server to the internet, and because both ports 80 and 443 require root privilege, making it even more unsafe. But if you really want to serve directly to your js backend, you can do so with something like this Best practices when running Node.js with port 80 (Ubuntu / Linode) for linux or this https://unix.stackexchange.com/questions/319734/pf-forwarding-all-packets-on-port-80-from-any-interface-to-socks-proxy for mac. This also requires root privilege to implement, and is still extremely unsafe.

significance of node.js port choice

If you want to run node.js directly without any supporting web server or reverse proxy (no nginx, varnish, apache, etc), you need to listen on port 80 for HTTP and (optionally) 443 for HTTPS if you want normal URLs to work. Otherwise users will need to type the port number in the URL like http://example.com:3000 which is unheard of for public-facing sites.

However, you almost certain DO want to use a separate web server as I describe in detail here, in which case any port over 1024 is fine.

How do I safely run a Node.js server on port 80 with Amazon Elastic Beanstalk?

Beanstalk has a proxy build into it listening on port 80. Your Node.js app should only listen on process.env.PORT. Once it's done that, you'll be good to go.



Related Topics



Leave a reply



Submit