How to use iptables to forward requests to a jBoss server running on a different machine?

You could just run a reverse HTTP proxy (Apache, Squid, varnish, nginx) on machine2. This is actually a fairly common configuration with application servers, where a "front-end" proxy is used to provide caching and failover for a backend application.

You would then have iptables rules on machine1 that would only allow connectivity from machine 2.


I managed to forward requests coming to Machine 2 to Machine 3 by using iptables. Ignore the comments in the script as they may not be correct explanations for the commands.

    #!/bin/bash

    #Execute the following command to enable ip forwarding if it is not already enabled.
    #echo 1 > /proc/sys/net/ipv4/ip_forward

    #nat to forward all requests to specified ports on Machine 2 to specified ports on Machine 1.
    iptables -t nat -A PREROUTING -p tcp -d 10.10.10.21 --dport 80 -j DNAT --to 10.10.10.20:80
    iptables -t nat -A PREROUTING -p tcp -d 10.10.10.21 --dport 1099 -j DNAT --to  10.10.10.20:1099
    iptables -t nat -A PREROUTING -p tcp -d 10.10.10.21 --dport 1098 -j DNAT --to 10.10.10.20:1098

    #Allow response from Machine 1 to Machine 2. 
    iptables -t nat -A POSTROUTING -d 10.10.10.20 -j MASQUERADE

This script causes all http,rmi and naming service requests made to Machine 2 to be forwarded to Machine 1.