Node.js: ECONNREFUSED on port 80

I wrote a web server using Node.js. When I tried to test my server using the tester I wrote for it, I only succeeded if the port that I use for the server is not 80. I checked netstat and no other application uses port 80. The error I I get on the console:

Error: connect ECONNREFUSED at errnoException (net.js:640:11) at Object.afterConnect [as oncomplete] (net.js:631:18) 

What can be done in this case?

+7
source share
5 answers

You may not have the right to connect to ports below 1024 (including 80) as a regular user (for example, not root), try a higher port number (above 1024, for example 8080).

[change]

Depending on your target system and assuming that you are its administrator, you can probably solve the problem as follows:

  • Run your program using sudo ", for example: sudo node ./myprogram.js .

  • Or log in as the " root " user on your system and run the program as usual.

However, do not get used to doing any of them on a regular basis (until you understand why), they can potentially introduce security holes that could be exploited by attackers who are motivated.

+3
source

You should not make the habit of running node as a privileged user. Here is the method I use on 6 different machines:

Use iptables to forward incoming traffic to port 80 to 8080 as follows:

 sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080 

Then be sure to save

 sudo iptables-save 

You also want to add this to rc.local so that they are installed when you reboot or start the instance (in the case of EC2 or another cloud)

Then you can safely start node listening on the 8080 like any user.

+17
source

This is basically a complement to the answer from maerics, but since I want to add some sample code, I am writing another answer with additional information on how to prevent security problems.

Typically, web servers run as root, as this is necessary to bind to ports below 1024, but then they change the user they run to a non-privileged user.

You can do this with node.js using process.setuid("username") .

In the following example, the server starts, and then the root permissions of the user "www-data" are deleted after binding to port 80 when starting as root:

 function StartServer() { console.log("Starting server..."); // Initalizations such as reading the config file, etc. server = http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); try { server.listen(80, "0.0.0.0", function(){ process.setuid("www-data"); }); } catch(err) { console.error("Error: [%s] Call: [%s]", err.message, err.syscall); process.exit(1); } } 
+10
source

An alternative that I have not seen in the above list also allows your process to communicate with the privileged port (<1024) by running the following command in the node executable. (Adapt path)

 setcap 'cap_net_bind_service=+ep' /opt/meteor/.meteor/tools/latest/bin/node 

It is annoying that you have to re-issue this command every time you update or modify the node executable.

Hi,

0
source

I am using a Nginx server (port 80) before Node.js (any port> 1024). It works great.

0
source

All Articles