5

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?

2

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

| improve this answer | |
0

The "PREROUTING" chain only applies to traffic received from the network. If you want to redirect locally generated traffic you need to use the "OUTPUT" chain.

You probablly also want some matches on the destination IP address.

| improve this answer | |
0

I would go with

sudo iptables -t nat -A OUTPUT -p tcp --dport 80 -j REDIRECT --to-ports 8100
| improve this answer | |

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.