How to Properly ProxyPass an Entire Domain to Tomcat Including Root Context


5 views

When configuring Apache to proxy requests to Tomcat, many developers encounter unexpected 302 redirects when accessing the root domain. The typical configuration looks like:

ProxyPass / http://localhost:8088/app
ProxyPassReverse / http://localhost:8088/app

While this works for paths like /page, the root URL triggers a Tomcat redirect to /app/, which breaks the expected behavior.

Tomcat automatically redirects root context requests when it detects a context path mismatch. In our case:

  1. Browser requests http://example.com/
  2. Apache proxies to http://localhost:8088/app/
  3. Tomcat responds with 302 to /app/
  4. Browser follows redirect to http://example.com/app/
  5. Apache can't resolve this path

1. Using ProxyHTMLURLMap

Modify the Location header in the redirect response:

ProxyPass / http://localhost:8088/app
ProxyPassReverse / http://localhost:8088/app
ProxyPassReverseCookiePath /app /

<Location />
    ProxyHTMLURLMap /app /
    RequestHeader unset Accept-Encoding
    ProxyHTMLEnable On
    ProxyHTMLDocType <!DOCTYPE html>
    ProxyHTMLCharset utf-8
</Location>

2. Context Root Adjustment

Configure Tomcat to use root context:

# In server.xml
<Context path="" docBase="app" debug="0" reloadable="true"/>

Then simplify Apache config:

ProxyPass / http://localhost:8088/
ProxyPassReverse / http://localhost:8088/

3. RewriteRule Alternative

For more control over URL patterns:

RewriteEngine On
RewriteRule ^/$ http://localhost:8088/app/ [P]
RewriteRule ^/(.*)$ http://localhost:8088/app/$1 [P]
ProxyPassReverse / http://localhost:8088/app/

Verify with curl to inspect headers:

curl -I http://example.com/
curl -I http://example.com/page

Check for proper content at root and absence of redirects to /app/.

Remember to restart Apache after configuration changes:

sudo apache2ctl graceful

When configuring Apache to proxy all requests to a Tomcat backend, a common issue occurs with root URL access:

ProxyPass / http://localhost:8088/app
ProxyPassReverse / http://localhost:8088/app

While this works perfectly for paths like /page, accessing the root domain (example.com or example.com/) triggers an unwanted 302 redirect from Tomcat:

HTTP/1.1 302 Moved Temporarily
Location: http://example.com/app/  # Undesired redirect

Tomcat's default behavior for web applications deployed with context path (/app) is to redirect root requests to the context path. This makes sense for direct Tomcat access but causes issues when proxying.

Here's the proper Apache configuration that handles all cases:

<VirtualHost *:80>
    ServerName example.com
    
    # Main proxy configuration
    ProxyPreserveHost On
    ProxyPass / http://localhost:8088/app/
    ProxyPassReverse / http://localhost:8088/app/
    
    # Special handling for root URL
    ProxyPassMatch ^/$ http://localhost:8088/app/
    ProxyPassReverse / http://localhost:8088/app/
    
    # Optional: Handle static resources separately
    Alias /static /path/to/static/files
</VirtualHost>
  • The trailing slash in http://localhost:8088/app/ is crucial
  • ProxyPreserveHost maintains the original Host header
  • ProxyPassMatch specifically handles the root URL case

Ensure your Tomcat context is properly configured in context.xml:

<Context path="/app" docBase="myapp">
    <Parameter name="org.apache.tomcat.util.http.ServerCookie.STRICT_NAMING" 
              value="false" override="false"/>
</Context>

Verify with curl:

curl -v http://example.com
curl -v http://example.com/page
curl -v http://example.com/api/endpoint

All requests should now proxy correctly to your Tomcat application without unwanted redirects.