NETWORK INCIDENTS

Self-Hosted App Works at Home but Not Outside? Check Your Port Forward

Network+ 2.1 Address translation: NATNetwork+ 3.4 Dynamic addressing: DHCP reservationsNetwork+ 5.5 Command-line tools: ip, ping

Your self-hosted media server works on the home Wi-Fi. From your phone on mobile data, it won't load. Nothing was changed on purpose. Let's find what broke.

A media server on a home network loads fine at home but not from a phone on mobile data. The phone, a stand-in for the internet, the home router (NAT and one port forward, TCP 8096), a switch, the server and a laptop at home, each in its own container.

The symptom. From the phone, curl -m 5 http://203.0.113.10:8096 fails, but from the laptop at home the server loads.

The lab

Here is the lab. On the left, a phone on mobile data, out on the internet. In the middle, the home router, with one public address facing the internet. Behind it, the home network: a switch, the media server, and a laptop. Like most home routers, it's really several devices in one box: a router, a firewall, and NAT, which shares that one public address with everything at home. Connections from the internet can't reach anything inside unless a rule lets them in. Here, that rule is a port forward: connections from outside to port 8096 get passed to the server.

The symptom

on phone
curl -m 5 http://203.0.113.10:8096
curl: (7) Failed to connect to 203.0.113.10:8096 after 3055 ms: Could not connect to server
What this shows

From the phone, curl asks for the server through the home's public address, 203.0.113.10, port 8096. It fails: could not connect to server. The page never starts loading.

on phone
ping -c 3 203.0.113.10
PING 203.0.113.10 (203.0.113.10) 56(84) bytes of data.
64 bytes from 203.0.113.10: icmp_seq=1 ttl=63 time=0.045 ms
64 bytes from 203.0.113.10: icmp_seq=2 ttl=63 time=0.034 ms
64 bytes from 203.0.113.10: icmp_seq=3 ttl=63 time=0.049 ms

--- 203.0.113.10 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2027ms
rtt min/avg/max/mdev = 0.034/0.042/0.049/0.006 ms
What this shows

Is the house reachable at all? A ping to the same public address gets three replies out of three. The router is up, and the path from the phone to the house works. So the problem is past the router's front door.

Inside the house

on server
ip -br addr show dev eth1
eth1@if1335      UP             10.0.10.51/24 fe80::a8c1:abff:fe58:ddbb/64
What this shows

On the server itself, ip with the brief option shows its address in one line. The server is up, and its address is 10.0.10.51.

on laptop
curl -m 5 http://10.0.10.51:8096
<!doctype html>
<title>homelab media server</title>
<h1>homelab media server</h1>
<p>It works.</p>
What this shows

From the laptop at home, the same request to the server's own address loads the page. It works. The server and the app are fine. Only the route in from outside is broken.

Finding the cause

on router
iptables -t nat -L PREROUTING -n -v
Chain PREROUTING (policy ACCEPT 2 packets, 124 bytes)
 pkts bytes target     prot opt in     out     source               destination
    3   180 DNAT       tcp  --  eth1   *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8096 to:10.0.10.50:8096
What this shows

That route in is the port forward, so look at it on the router. This lists the address translation rules applied to traffic as it arrives. There's one rule: TCP to port 8096 coming in on the internet side, eth1, gets sent to 10.0.10.50, port 8096. The counter shows the rule has been matching connections. But the server is at 10.0.10.51, not 10.0.10.50.

on router
ip neigh
10.0.10.50 dev eth2 FAILED
203.0.113.1 dev eth1 lladdr aa:c1:ab:7b:d3:67 REACHABLE
What this shows

ip neigh shows the router's neighbour table: every device it has tried to reach directly, on both sides. On eth1, the internet side, the internet provider's router, with its MAC address. On eth2, the home network: 10.0.10.50, failed. The router went looking for it, and nothing answered. The forward is sending visitors to an address where nobody lives anymore.

The fix

on router
iptables -t nat -R PREROUTING 1 -i eth1 -p tcp --dport 8096 -j DNAT --to-destination 10.0.10.51:8096
(no output)
What this shows

The fix is on the router. This replaces that first rule with the same forward, now pointing at 10.0.10.51.

on router
iptables -t nat -L PREROUTING -n -v
Chain PREROUTING (policy ACCEPT 2 packets, 124 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DNAT       tcp  --  eth1   *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8096 to:10.0.10.51:8096
What this shows

Listing the rules again, port 8096 now goes to 10.0.10.51.

on phone
curl -m 5 http://203.0.113.10:8096
<!doctype html>
<title>homelab media server</title>
<h1>homelab media server</h1>
<p>It works.</p>
What this shows

Back on the phone, the same request to the home's public address. This time the page loads. It works.

on router
ip neigh
10.0.10.50 dev eth2 FAILED
203.0.113.1 dev eth1 lladdr aa:c1:ab:7b:d3:67 DELAY
10.0.10.51 dev eth2 lladdr aa:c1:ab:58:dd:bb REACHABLE
What this shows

And the router's table again. Now there's 10.0.10.51, the server's new address, reachable, with its MAC. The old address is still listed as failed: the router remembers it tried, but nothing sends traffic there anymore.

Why it broke

So what changed? The server came back from a power cut with a different address, 10.0.10.51 instead of 10.0.10.50, which is what happens when a router hands out addresses that aren't reserved. The port forward points at one exact address, so it kept sending visitors to the old one. Inside the house nothing looked wrong, because the laptop could still find the server. To stop it happening again, give the server an address that can't move: a DHCP reservation on the router, or a static address on the server.

Forward to an address that can't move

When a self-hosted app works at home but not from outside, check where the port forward points, and make sure the server's address can't change underneath it.

Try it yourself

The whole lab runs on your own computer with Docker and containerlab: get it on GitHub, then ./lab.sh up and ./lab.sh break. Setup steps are on Run the labs.