Tuesday, December 03, 2013

Apache Tomcat redirecting traffic from port 8080 to 80 using iptables

Apache runs on port 8080 and 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.

To redirect port 80 traffic to port 8080 follow the steps below.

Run the Command
  • netstat -ntl
Output of the command will be like below
================================================
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address     Foreign Address     State
tcp        0      0 127.0.0.1:25                      0.0.0.0:*                 LISTEN
tcp        0      0 0.0.0.0:22                         0.0.0.0:*                   LISTEN
tcp        0      0 ::ffff:127.0.0.1:8005          :::*                            LISTEN
tcp        0      0 :::8009                                 :::*                              LISTEN
tcp       0      0 :::8080                               :::*                            LISTEN
tcp        0      0 :::22                                      :::*                               LISTEN
================================================

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 your 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

2 comments:

Sartaj Hussain said...

WOW didn't knew its that simple. Good job man.... :-)

Unknown said...

Thanx Sartaj :-)

Post a Comment

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