How to make a redirect of the rank of ports to another server? Example: redirect all connections from 20,000 to 23,000 to 2.3.4.5: PORT (from 20,000 to 23,000)

    2 answers 2

    It is possible only if the other_server will send responses through the first server (that is, in fact, this is the case when the first server is the default gateway for the other_server (this can also be achieved using VPN).

    Suppose server1 has a public address of 188.1.1.5 (this is the address where incoming connections will come to the ports of interest to us.

    Suppose server1 and server2 are interconnected by a network of 192.168.1.0, in which server1 has an address of 192.168.1.1, and server2 has an address of 192.168.1.2, with 192.168.1.1 being the default gw for server2.

    In this case, the iptables setting on server1 will look like this:

    iptables -t nat -A PREROUTING -p tcp --dport 20000:23000 -m state --state NEW -j DNAT --to-destination 192.168.1.2 

    Naturally, IP forwarding must be enabled on server1.

    There is another option - to install on server1 nginx, configured as a TCP proxy . This solution is far from the original formulation of the problem, but it works well without meeting the condition with the default gateway. He has two "but":

    1. All connections to server2 will come from server1
    2. I did not check it on myself :)