Apache as Frontend

Apache as the Frontend of Tomcat

Placing Apache in front of Tomcat can provide URL rewriting, static-content delivery, load balancing, additional security controls, and access to Apache modules while Tomcat continues to run the Java applications.

Overview

Apache can forward client requests to one or more Tomcat application servers through mod_proxy or mod_rewrite, then relay the Tomcat responses back to the user.

URL rewritingExpose separate Tomcat applications through different paths on one Apache frontend.
Static contentLet Apache deliver images, CSS, JavaScript, and other static files while Tomcat handles the application.
Load balancingDistribute requests across several Tomcat environments and gain basic failover capability.
Independent managementManage, restart, and debug backend applications separately while presenting them as one service.

Rewriting Application Links

The documented example uses two applications hosted in separate Tomcat environments:

  • https://env-tomcat.jelastic.com/app1/
  • https://second-tomcat.jelastic.com/app2/
Tomcat applications behind an Apache frontend
One Apache frontend routes requests to multiple Tomcat environments.
1

Prepare the environments

Create one Apache frontend environment and two or more backend Tomcat environments with the Java applications deployed.

2

Edit Apache configuration

Click Config beside Apache and open /etc/httpd/conf/httpd.conf. Add the routing rules inside the VirtualHost block.

<VirtualHost *:80>
  ServerAdmin webmaster@domain.com
  DocumentRoot /var/www/webroot/ROOT
  ServerName website.jelastic.com

  RewriteEngine On

  RewriteRule ^/application1/(.*) http://env-tomcat.jelastic.com/app1/ [P]
  ProxyPassReverse /application1/ http://env-tomcat.jelastic.com/app1/

  RewriteRule ^/application2/(.*) http://second-tomcat.jelastic.com/app2/ [P]
  ProxyPassReverse /application2/ http://second-tomcat.jelastic.com/app2/

  RewriteLog "/var/log/httpd/rewrite.log"
  ErrorLog logs/dummy-host.jelastic.com-error_log
  CustomLog logs/dummy-host.jelastic.com-access_log common
</VirtualHost>
Apache rewrite configuration for Tomcat applications
Configure RewriteRule and ProxyPassReverse for each backend application.
i

Directive purpose

RewriteEngine On enables rewriting. RewriteRule and ProxyPassReverse define the forwarding rules. RewriteLog is optional and records rewrite activity.

3

Save, restart, and test

Save the file, restart Apache, and open the frontend URL with /application1/ or /application2/.

Serving Static Content

Apache can deliver static files while all other requests are proxied to Tomcat.

Apache static content processing scheme
Apache serves static content while Tomcat processes the Java application.
1

Create the static folder

Open Apache Configuration Manager, go to /var/www/webroot/ROOT, create a folder such as static, and upload the static files.

2

Configure proxy exclusions

Open /etc/httpd/conf/httpd.conf and configure the frontend so requests for the static folder stay on Apache while all other requests go to Tomcat.

<VirtualHost *:80>
  ServerAdmin webmaster@domain.com
  DocumentRoot /var/www/webroot/ROOT
  ServerName website.jelastic.com
  ServerAlias *

  ProxyPass        /static !
  ProxyPass        / http://env-tomcat.jelastic.com/app1/
  ProxyPassReverse / http://env-tomcat.jelastic.com/app1/

  ErrorLog logs/dummy-host.jelastic.com-error_log
  CustomLog logs/dummy-host.jelastic.com-access_log common
</VirtualHost>

Proxy exclusion

ProxyPass /static ! prevents requests beginning with /static from being proxied. All other requests are forwarded to Tomcat.

3

Restart and verify

Restart Apache. Open the frontend URL to verify the proxied Tomcat application, then open /static/ to verify the static content.

Load Balancing

The documented load-balancing example uses one Apache frontend and two Tomcat backend environments. Both Tomcat applications must be deployed under the same context.

Apache load balancing between Tomcat servers
Apache distributes requests between multiple Tomcat application servers.
1

Create the backend host list

In /etc/httpd/conf.d, create a file named servers_list and add the backend hosts:

servers {env1_name}.{hoster_domain}|{env2_name}.{hoster_domain}
2

Add the balancing rules

Open /etc/httpd/conf/httpd.conf and configure a random rewrite map.

<VirtualHost *:80>
  ServerAdmin webmaster@domain.com
  DocumentRoot /var/www/webroot/ROOT
  ServerName website.jelastic.com
  ServerAlias *

  RewriteEngine On

  RewriteMap lb rnd:/etc/httpd/conf.d/servers_list
  RewriteRule ^/(.*) http://${lb:servers}/app1/$1 [P,L]

  RewriteLog "/var/log/httpd/rewrite.log"
  ErrorLog logs/dummy-host.jelastic.com-error_log
  CustomLog logs/dummy-host.jelastic.com-access_log common
</VirtualHost>
i

How it works

RewriteMap points to the backend-host list, and RewriteRule forwards incoming requests to a randomly selected Tomcat server.

3

Restart and test distribution

Restart Apache and open the frontend URL. Refresh the page several times to confirm that requests are being sent to different Tomcat servers.

Expected Result

Apache operates as the public frontend while Tomcat runs the Java applications. The frontend can expose several applications through descriptive paths, serve static files directly, or distribute requests between multiple Tomcat backends.

Important Notes

  • Back up httpd.conf before editing it.
  • Restart Apache after saving configuration changes.
  • Use identical application contexts on the Tomcat backends used for load balancing.
  • Protect backend Tomcat environments from unnecessary public access.
  • The source page uses historical Apache rewrite directives; validate them against the Apache version running in your environment.
  • Review Apache rewrite, access, and error logs during troubleshooting.

Common Issues and Solutions

Frontend returns 502 or 503Check the Tomcat environment URL, application context, backend status, and network connectivity.
Rewritten path opens the wrong appReview the RewriteRule order, path patterns, and ProxyPassReverse entries.
Static files are proxied to TomcatPlace the ProxyPass /static ! exclusion before the general proxy rule.
Only one backend receives trafficCheck the servers_list syntax, RewriteMap path, and backend host availability.
Apache fails to restartValidate the VirtualHost, rewrite, and proxy directive syntax for the installed Apache version.