I have two hosts in a network

  • Host A
  • Host B

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

ssh -R -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 to that nginx server. How to do it?


if someone send a GET request with curl he will get response from nginx run on in a container. The flow should be: (for example) ---GET--> --(SSH TUNNEL)--> -----------> (nginx container)      ------------>    ------------>    ------------>

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


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.