Mapping Tomcat Apps to Subdomains with Apache

Suppose you have your web app hosted by Tomcat at http://localhost:8080/myapp. That’s an ugly address for end users, so you want it to be accessible as http://somesystem.example.com. Here’s how you can do it with a little help from Apache via mod_proxy and mod_rewrite.

  1. Install Apache:
  2. apt-get install apache2
  3. Enable optional modules:
  4. a2enmod proxy
    a2enmod proxy_http
    a2enmod rewrite
  5. Configure proxy (/etc/apache2/httpd.conf):
  6. ProxyRequests Off
    
    <Proxy *>
      Order deny,allow
      Allow from all
    </Proxy>
  7. Configure virtual host (/etc/apache2/sites-available/somesystem):
  8. <VirtualHost *>
            ServerName somesystem.example.com
            RewriteEngine On
            ProxyPass / http://localhost:8080/myapp
            ProxyPassReverse / http://localhost:8080/myapp
    </VirtualHost>
  9. Enable virtual host:
  10. a2ensite somesystem
  11. Restart Apache:
  12. /etc/init.d/apache2 restart
  13. Enable proxy support in Tomcat (conf/server.xml):
  14. <Connector port="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" proxyPort="80" />
  15. Restart Tomcat

Note: It’s written for Ubuntu, but can be easily converted to other systems.

2 thoughts on “Mapping Tomcat Apps to Subdomains with Apache

Leave a Reply

Your email address will not be published. Required fields are marked *

Spam protection by WP Captcha-Free