How to Route Apache Port 80 Requests to Tomcat Port 8080 with URL Rewriting


2 views

When deploying Java web applications, it's common to have Apache HTTP Server (port 80) as the front-facing web server while Tomcat (port 8080) handles the Java processing. The challenge is to make this transition seamless for end users.

The most efficient method is using Apache's mod_proxy module. Add these directives to your virtual host configuration:


    ServerName www.xyz.com
    ProxyRequests Off
    ProxyPreserveHost On

    
        Require all granted
    

    ProxyPass / http://localhost:8080/myApp/
    ProxyPassReverse / http://localhost:8080/myApp/

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

For more complex setups, consider the AJP connector with mod_jk:

LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkMount /myApp worker1
JkMount /myApp/* worker1

To remove the application context path (/myApp) from URLs while preserving Tomcat's original functionality:

ProxyPass / http://localhost:8080/myApp/
ProxyPassReverse / http://localhost:8080/myApp/
ProxyPass /myApp !

After implementing changes, verify with these commands:

sudo apachectl configtest
sudo systemctl restart apache2
curl -I http://localhost

For production environments, add these caching directives:

ProxyPass / http://localhost:8080/myApp/ retry=5
ProxyTimeout 300
ProxyIOBufferSize 8192

If you encounter 502 errors, check:

  • Tomcat is running and listening on 8080
  • SELinux or firewall isn't blocking connections
  • The proxy modules are loaded in Apache

When deploying Java web applications, it's common to have Apache HTTP Server handle public-facing requests on port 80 while Tomcat runs on port 8080. This setup provides better security, load balancing capabilities, and SSL termination points.

You'll need to enable these modules in Apache:

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod rewrite
sudo systemctl restart apache2

Here's a sample configuration for mapping www.xyz.com to a Tomcat webapp:

<VirtualHost *:80>
    ServerName www.xyz.com
    ProxyPreserveHost On
    ProxyPass / http://localhost:8080/myApp/
    ProxyPassReverse / http://localhost:8080/myApp/
    
    # Allow direct access to Tomcat manager
    ProxyPass /manager !
    ProxyPassReverse /manager !
</VirtualHost>

For more complex scenarios where you want to preserve certain paths:

<Location /special-app>
    ProxyPass http://localhost:8080/anotherApp
    ProxyPassReverse http://localhost:8080/anotherApp
    RequestHeader set X-Forwarded-Proto "https"
</Location>

To maintain direct access to Tomcat on port 8080 while proxying through Apache:

<VirtualHost *:8080>
    ServerName www.xyz.com
    # Your original Tomcat configuration remains here
</VirtualHost>

Modern applications often need WebSocket support:

RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) ws://localhost:8080/myApp/$1 [P,L]
  • Always set proper timeouts: ProxyTimeout 300
  • Consider adding: ProxyRequests Off to prevent open proxy
  • For production: SSLProxyEngine On when using HTTPS between Apache and Tomcat

Check these logs when debugging:

tail -f /var/log/apache2/error.log
tail -f /opt/tomcat/logs/catalina.out