0

I have two hosts in a network 192.168.0.0/24:

  • Host A 192.168.0.15
  • Host B 192.168.0.13

And there is a remote ssh tunnel from B to A:

ssh -R 192.168.0.13:8080:192.168.0.15:8002 192.168.0.13 -N

and that tunnel works.

Host A runs a container nginx that runs nginx on a port 80. I would like to forward a traffic from 192.168.0.15:8002 to that nginx server. How to do it?

So,

if someone send a GET request with curl 192.168.0.13:8080 he will get response from nginx run on 192.168.0.15 in a container. The flow should be:

192.168.0.14 (for example) ---GET--> 192.168.0.13:8080
192.168.0.13 --(SSH TUNNEL)--> 192.168.0.15:8002 
192.168.0.15:8002 -----------> 172.17.0.2:80 (nginx container)
172.17.0.2      ------------> 192.168.0.15
192.168.0.15    ------------> 192.168.0.13
192.168.0.13    ------------> 192.168.0.14

P.S. I have to use iptables- I cannot change an existing tunnel.

1

You can achieve this with the REDIRECT extension.

You can change the destination for all TCP connections from port 8002 to port 80 with the following command on Host A:

iptables -t nat -I PREROUTING -p tcp --dport 8002 -j REDIRECT --to-port 80

If you want the redirect to also work for connections from the localhost, add

iptables -t nat -I OUTPUT -p tcp --dport 8002 -j REDIRECT --to-port 80
| improve this answer | |
  • Thanks! Is the PREROUTING omitted in the case of looback? – Gilgamesz Oct 14 '18 at 15:48
  • PREROUTING is omitted when a locally generated packet is destined for localhost. Diagrams can be useful for seeing which chains and tables are entered and when – Torin Oct 14 '18 at 19:49
  • Do you know a diagram that embrace a localhost case? – Gilgamesz Oct 15 '18 at 4:06

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.