3

I read about port forwarding, and could understand that it enables the internet to send requests to your private network and access some service which is running on one of the computers in your private address.

But my doubt is what will happen if suppose I have 2 computers hosting a web server on port 80. In this case how router will know which computer to send request. I know if there is only 1 computer then router will know but doubt is in case of 2.

A situation could be like I have 2 computers on my private network and each one is hosting a different website. Now I want both of my servers to be running on port 80 so that user need not to type the port number (or may be because of some reason I want both web servers to have same port number). What happens now?

2
  • It could be possible for uPNP to automatically open the port forwarding for your router and then the first computer would be accessible from the internet. If a second pc does the same, uPNP will simply be ignored and the first pc remains the active one. To counter this, look at one of the answers; manual port forwarding rules are required. – LPChip Feb 25 '16 at 21:56
  • @LPChip Even manual port forwarding will not handle the situation I have outlined in my question. It can be solved using reverse-proxy, please read "Darth Android" answer and comments – hagrawal Feb 25 '16 at 21:58
8

The router does not care. You have to tell it which of the two computers to forward to, and it ignores the other one.

If you want both computers to be accessible on the internet, you have to forward two different ports (i.e., 80 and 81) on the router, one to each of the two computers.


If you want users to not have to input the port number when browsing to either websites, you need to set up a reverse proxy like nginx somewhere behind the router (on a third computer, or on one of the first two computers), and make the router forward to the proxy. The proxy can then be configured to look at the URI that the client was trying to access (hostname is most common, look up "webserver virtual hosts", though you could base the decision upon other parts of the URI like the path), and forward the request to one of the original servers (which can be running on any port).

10
  • Or you could configure one web server to forward requests to the other web server based on the Host header. The you tell the router to forward incoming connections to TCP port 80 to that web server. – David Schwartz Feb 25 '16 at 21:59
  • @DavidSchwartz No, I don't think this is good. By I want one server to be overloaded because of other server, just to decide that this is actually a request for other server. – hagrawal Feb 25 '16 at 22:01
  • @hagrawal You're imagining some super router and both servers being anemic? Why? Are you just looking for ways to make problems that are hard to solve? Surely that decision has to be made somewhere, no? – David Schwartz Feb 25 '16 at 22:01
  • On the contrary I am not imagining a super server who wishes to be overloaded because of other server's load. This is definitely a bad design, many reasons. For my problem solving I will always choose a 3rd server to make this decision. – hagrawal Feb 25 '16 at 22:08
  • 2
    @hagrawal While you might not be using the hostname aspect, it is definitely correct, in that it will work and is in fact how many large webservers operate (see "webserver virtual hosts") to solve this class of problem (multiple web servers being served on a single IP on a single port). I'm inclined to leave it in the answer for this reason. – Darth Android Feb 25 '16 at 22:18
1

Port forwarding forwards incoming connections from router's port to specific IP in your local network. It doesn't magically detect which machines in your network run required service, you have to configure it manually what setting up port forwarding.

Here's how it looks in my router's configuration panel:

Screenshot

1
  • +1 for your information but you didn't got my question, my question is what if 2 computers are running same server and on same port, how this situation will be handled? Can it be handled using port forwarding or there is some other methodology for handling this situation? – hagrawal Feb 25 '16 at 21:43
1

I think your intuition is correct.

I don't think it can know/decide via port forwarding. You can only port forward to one of the computers, and that has to be decided by your configuration of the router i.e. you decide either computer A is to be the one that the router forwards requests on port 80 to, or computer B is.

5
  • Ok, now what next? How this situation is going to be handled? Can it be handled using port forwarding or there is some other methodology for handling this situation? – hagrawal Feb 25 '16 at 21:45
  • @hagrawal As with any port forwarding, you tell the NAT router a private IP like 192.168.1.4(CompA) or 192.168.1.7(CompB). The router might let you specify a MAC address or a name, but it's the same principle, it can only go to one computer and that's the one you specified in the router for it to go to. – barlop Feb 25 '16 at 21:46
  • Ok, I got that point. Check this - A situation like I have 2 computers on my private network and each one is hosting a different website. Now I want both of my servers to be running on port 80 so that user need not to type the port number. What happens now? – hagrawal Feb 25 '16 at 21:49
  • 1
    @hagrawal You need to run a reverse proxy like nginx on a third computer, forward the router to the third computer, and then have the reverse proxy forward to the first two computers based upon the desired website. – Darth Android Feb 25 '16 at 21:51
  • @DarthAndroid Ok, this is cool, can you please add it your answer and I can accept it. So, basically, please correct me if I am wrong, this situation cannot be handled purely using port forwarding. – hagrawal Feb 25 '16 at 21:55

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.