Ssh – Reverse SSH tunnel works for SSH but not for HTTP

httpsshssh-tunneling

What works

I have the following setup:

server <-NAT-> VPS <-> client

I have set up ssh to my server through a reverse tunnel and the following settings:

  • on VPS,

GatewayPorts yes

  • on server:

autossh -f -M 57575 -N -R <VPS ip>:9991:localhost:<server ssh port> vps

With this setup, I can simply ssh -p 9991 <server user>@<VPS ip> and log into my server from the client.

What doesn't

  • on VPS,

GatewayPorts yes, and otherwise identical setup to SSH case

  • on server:

autossh -f -M 57576 -N -R <VPS ip>:9992:localhost:8988 vps

python -m http.server 8988 to start the http server. I can confirm it binds to 0.0.0.0 and I can access it on the local machine.

However, when I try to access the website via http://<VPS ip>:9992 I get an ERR_EMPTY_RESPONSE.

Looking at netstat, the two situations seem identical. On VPS:

tcp 0 0 0.0.0.0:9992 0.0.0.0:* LISTEN -

tcp 0 0 0.0.0.0:9991 0.0.0.0:* LISTEN -

On server:

tcp 0 0 0.0.0.0:8988 0.0.0.0:* LISTEN 21003/python

tcp 0 0 0.0.0.0:<ssh port> 0.0.0.0:* LISTEN 550/sshd

tcpdump shows that a connection is established with port 9992 on the VPS, but nothing reaches port 8988 on server. The connection is FINned immediately on the VPS.

Why doesn't this work, when the exactly analogous SSH situation does?

Best Answer

You should start the server first and then initiate the remote port forwarding. Not the other way round.

I am not sure why is that, but obviously it resolves the issue.

Related Question