How can I redirect a port number on Linux?

I have a Tomcat application running on my Linux machine on port 8080 (www.myapplication.com:8080/myapps).

I want to redirect the Tomcat port :8080 to the default HTTP port of :80 so that the application can be accesed without a port number (www.myapplication.com/myapps).

How can I do that on Linux?


Solution 1:

You could use iptables to redirect port 80 to 8080.

This is useful if your application is started by an unprivileged user instead of root.

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

HTH, PEra

Solution 2:

There so many ways to achieve this, but first which comes to my mind is to use nginx: How to permanently redirect port 8080 URL to port 80 using nginx

Another one is to use iptables: http://www.cyberciti.biz/faq/linux-port-redirection-with-iptables/

Solution 3:

You should never face the Tomcat or any other Java application server to the external world. The best practice is to install Apache HTTPD and use it a reverse proxy to wrap Tomcat or JBoss.

I recommend to do the following.

  1. Install httpd:

    yum install httpd
  2. Create file

    /etc/httpd/conf.d/myredirect.conf
    with the following content:
    ProxyPass   http://127.0.0.1:8080/myapps/
    ProxyPassReverse       http://127.0.0.1:8080/myapps/
    
  3. If the application resides in the root than the configuration will be the following:

    ProxyPass   http://127.0.0.1:8080/
    ProxyPassReverse       http://127.0.0.1:8080/
    
  4. Restart httpd:

    service httpd restart

The instructions above are provided for RedHat-family linux. They may differ for other ones.