SSH -L Port Forward Gives "Empty Reply From Server" on localhost
A local port forward to localhost can connect fine and still return nothing. Here is why, and the one-line fix.
The symptom
A local port forward is set up to reach a web service on a target host that only listens on its own loopback address:
ssh -L 80:localhost:80 user@192.168.x.x
The tunnel connects, but requesting it locally fails:
curl http://localhost/ curl: (52) Empty reply from server
The same request run directly on the target host works normally and returns a page.
Check what the service is actually listening on
On the target host:
netstat -anop tcp
TCP 127.0.0.1:80 0.0.0.0:0 LISTENING 4
Only the IPv4 loopback address is listed. There is no matching entry for the IPv6 loopback ([::1]:80) and no entry on the host's network interface address either, so the service is bound to IPv4 loopback only.
What curl -v shows
curl -v http://localhost/ * Host localhost:80 was resolved. * IPv6: ::1 * IPv4: 127.0.0.1 * Trying [::1]:80... * Established connection to localhost (::1 port 80)
The name localhost resolves to both an IPv4 and an IPv6 address, and the client follows RFC 6724's address-selection order, which prefers IPv6 when both are available.
The local SSH client happens to listen on both 127.0.0.1:80 and [::1]:80, so this first connection succeeds. The SSH server on the far end then resolves "localhost" again to make its own outbound connection, also prefers ::1, and tries [::1]:80 — where nothing is listening. That connection is refused, the forwarding channel is closed, and the client sees it as a connection that succeeded but returned nothing: Empty reply from server.
The fix
Use the explicit IPv4 address on both ends instead of the ambiguous hostname:
ssh -L 80:127.0.0.1:80 user@192.168.x.x
curl http://127.0.0.1/
This removes the hostname resolution step on both sides, so there is no longer a choice between IPv4 and IPv6 to get wrong.
Related: allow a port from specific IPs, iptables command not found.
FAQ
Why does curl prefer IPv6 for localhost?
When a hostname resolves to both an IPv4 and an IPv6 address, most systems follow RFC 6724's address-selection rules, which rank the IPv6 loopback ahead of the IPv4 one by default.
Why does the same URL work on the target host but not through the tunnel?
On the target host, the request is made and resolved locally. Through the tunnel, the remote SSH server makes a second, independent resolution and connection to the forwarding target, and that one can fail even though the first connection succeeded.
Does this only affect port 80?
No. Any ssh -L forward that uses localhost as the target, where the destination service only listens on one address family, can show the same behavior.