Iptables port redirection on Ubuntu

iptables

I have an apache server running on 8100. When open http://localhost:8100 in browser we will see the site running correctly.

Now I would like to direct all request on 80 to 8100 so that the site can be accessed without the port number. I am not familiar with iptables so I searched for solutions online. This is one of the methods that I have tried:

user@ubuntu:~$ sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
user@ubuntu:~$ sudo iptables -A INPUT -p tcp --dport 8100 -j ACCEPT
user@ubuntu:~$ sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8100

It's not working. The site works on 8100 but it's not on 80. If print out the rules using "iptables -t nat -L -n -v", this is what I see:

user@ubuntu:~$ sudo iptables -t nat -L -n -v
Chain PREROUTING (policy ACCEPT 14 packets, 2142 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REDIRECT   tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           
tcp dpt:80 redir ports 8100 

Chain INPUT (policy ACCEPT 14 packets, 2142 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 177 packets, 13171 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 177 packets, 13171 bytes)
 pkts bytes target     prot opt in     out     source               destination 

The OS is a Ubuntu on a VMware. I thought this should be a simple task but I have been working on it for hours without success. 🙁 What am I missing?

Best Answer

Try: sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to 127.0.0.1:8100