Tuesday, February 18, 2014

How to forward request from one port to other using IP tables

There may be case when your application runs on some other port that port 80 like in Apache runs on port 8080 in these cases it requires huge changes in the configuration of Apache to forward request coming at port 80 to 8080 as default port for http request is 80 rather than making huge configuration changes it can be achieved through iptables by simply running a single command.

We will be forwarding request coming to port 80 to port 8080 in the below description.

Run the following command to redirect port 80 traffic to port 8080
  • iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
Run the below command to verify that redirect is working fine
  • iptables -t nat -L
Output of the command will like below.
================================
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
REDIRECT   tcp  --  anywhere     anywhere    tcp dpt:http redir ports 8080
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
================================

As you can see traffic of port 80 is redirected to port 8080, Now you can access you website without domain.com:8080

*Note for remove the port rooting use the command below.
  • iptables -t nat -D PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

No comments:

Post a Comment

Do Write about the Blog and Welcome to the world where open source is every thing :-)