I'm just reading up on port forwarding, as thinking about using a RaspberryPi to host a web server.
One thing that's occurred to me is this. All requests on a certain port get forwarded to a particular internal IP address. Are responses forwarded in the same way? As surely this would prevent other computers on the network from accessing the internet, as all responses from visited websites would go to the server.
13 Answers
In order to answer this, you need to know how network traffic works between a client and server.
When you have a client-server program, the server sets up a listening port.
Any client can then connect to this listening port and ask to be connected. Because you can only make one single connection to a port, as soon as the server accepts the connection (goes in a split second) the server then starts a new connection back to the client (usually via uPNP, optionally specifying which port it will use) to do the actual data transfer. The initial port from the client to the server is dropped to free the listening port.
Because the server is behind a NAT (router), when the client connects to the public ip with the portnumber, the router answers, not the server behind it. Because the router doesn't have the software of the server (in your case a webserver) it doesn't know how to answer and the connection fails because the router simply ignores the request.
By adding a portforward, you tell the router what to do when someone tries to connect to the public ip with that specified port. In your case, the rule says: relay the traffic to the raspberry pi.
Now, as you can see, the only thing that happens is that the router learns what to do when a request is being made to the public ip and port. PC's inside the network do not request access by the public ip, but by the private ip instead. In addition, the router allows to setup which public port it listens to. So if you have 3 raspberry pi's, each having their own ip address, you could setup 3 rules:
- port 8081 to pi1, port 80
- port 8082 to pi2, port 80
- port 8083 to pi3, port 80A client then connects to your public ip appending :8081 to open that port instead, and all still works.
There are no physical links laid, so all ports remain as they are. Its just that the router understands what to do with a port now, and where the traffic should head.
Do note that not all routers are capable of loopback traffic, meaning, they expect connections on the NAT with public ip address to be coming from the WAN port, not any of the LAN port, so it may not be possible to test the forward rules on your router from inside the network.
4I'm not a Expert on Network Topics, but the Port 80 is only the port the server is listening to. The client listens on a random port above 1024 for the server responses.
Example:
You --> asdf.com:Port 80
asdf.com --> you:Port 12345 (response) So having port 80 forwarded to the raspberry, will not affect other PCs in the network when using the network regulary.
Of course you can only forward the port to one PC in your home network, so you can only run one raspberry-server at a time.
From inside your home-network, all servers are still accessible via their local IP-adress.
Of course this applies to all ports, not only 80
So when you set up your home router to forward port 80 to your Raspberry Pi, you are forwarding port 80 of your public IP address. This address is assigned to your router from your ISP. No one on the outside (the internet) can see whats behind your router (your internal LAN). So when a request comes from the outside for a webpage, the router forwards it to your Raspberry Pi. The responses are sent back to your router, which knows where the request came from and responds back. Internally, nothing is different. You would access your Raspberry Pi through its internal IP address on port 80.