1

I'm using a node.js app on port 80 of 127.0.0.2:80

server.listen(80, '127.0.0.2', function() {...

And I want to use Apache on port 80 of another ip address, say 127.0.0.3:80, so I have this in my httpd.conf

Listen 127.0.0.3:80
ServerName 127.0.0.3:80

But it gives me the error

Apache port (80) is already used by another application!

Why does Apache care about a random port 80 regardless of which IP's port 80 it itself is programmed to listen at? As far as it should be concerned port 80 on 127.0.0.3:80 is still free.

I've even gone ahead and tried this with other IP series (192.xxx, 122.xxx) but Apache wants port 80 free, full stop.

Weird thing is, if I start Apache first, it launches with no problem, and then I can even start node.js all with same configuration above and they both work fine. But precious Apache for some reason doesn't like anybody listening on port 80 of any IP address before its highness has had the chance to start first.

Am I misunderstanding things that if an application is listening on 127.0.0.2:80 then only that IP's port 80 should be busy, other IP's port 80 should still be free 127.0.0.3:80? Or is it Apache that's misbehaving?

3

Please note I have a suggested basic solution at the end of this answer.

HTTP uses TCP by default, and while UDP does support multiple programs binding to a single port, TCP usually does not. So port 80 would appear not be free for a given IP in this instance as there can most likely be only one program using port 80 (TCP) with that IP at a time on the local machine.

Regarding separate loopback IPs, remember that the entire 127.x.x.x netblock is reserved for local loopback. So 127.0.0.1 refers to the same computer (i.e. localhost) as 127.0.0.2 (or 127.0.0.3 or 127.127.127.127, etc.). And since you are using the 127.x.x.x loopback address rather than "real" IPs (i.e. ones that identify separate real or virtual NICs), port 80 with a given IP may already in use by another the application when you start the second one.

And yes, while you can have Apache do virtual hosting with local loopback IPs (i.e. 127.0.0.1 and 127.0.0.2 can return different pages), the Apache process is still tied to port 80 (TCP) globally by default.

Weird thing is, if I start Apache first, it launches with no problem, and then I can even start node.js all with same configuration above and they both work fine.

I'm not sure what "work[ing] fine" is referencing here, but after starting Apache then attempting to start Node.js http-server on port 80 (npm install http-server -g), I got an error even if an address such as 127.0.0.4 was specified.

image of node.js http server failing to start

In my config file, I had to make certain that Apache was specifically listening on 127.0.0.1:80 and http-server was listening on 127.0.0.4:80. If I just had Listen 80 in Apache, http-server failed. Apache refusing to start is completely normal, both for Apache itself and in terms of the principle of only one TCP IP/port combination per process.

Solution

The easy solution to this problem is to run multiple NICs on the same machine (each with it's own unique IP). This way, each NIC can have its own port 80 binding (IP+port = complete destination address). Another possibility is to try "virtual interfaces" for the NIC (so you can give two "virtual" IPs to the same NIC).


Reference

Stack Overflow - Can two applications listen to the same port?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.