Apache Reverse Proxy via mod_proxy_ajp not routing requests to Tomcat

I have two instances of Apache and Tomcat7 installed. One on my Macbook Pro with Mountain Lion and the other in a VirtualBox VM on that Mac with Ubuntu Server 12.10. Both have the same VirtualHost configuration and server.xml configuration.

Everything works fine on my Mac but on the Ubuntu VM I get “File does not exist” errors.

When I set LogLevel to ‘debug’ for both the log output looks virtually the same.

Is there someplace else I can look to see why the requests aren’t getting proxy’d to Tomcat?

Here are my config files.

server.xml:

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

proxy.conf:

<IfModule mod_proxy.c>                                                                  

# If you want to use apache2 as a forward proxy, uncomment the                          
# 'ProxyRequests On' line and the <Proxy *> block below.                                
# WARNING: Be careful to restrict access inside the <Proxy *> block.                    
# Open proxy servers are dangerous both to your network and to the                      
# Internet at large.                                                                    
#                                                                                       
# If you only want to use apache2 as a reverse proxy/gateway in                         
# front of some web application server, you DON'T need                                  
# 'ProxyRequests On'.                                                                   

ProxyRequests On                                                                        
#<Proxy *>                                                                              
#        AddDefaultCharset off                                                          
#        Order deny,allow                                                               
#        Deny from all                                                                  
#        #Allow from .example.com                                                       
#</Proxy>                                                                               
<Proxy *>                                                                               
    AddDefaultCharset Off                                                               
    Order deny,allow                                                                    
    Allow from all                                                                      
</Proxy>                                                                                

# Enable/disable the handling of HTTP/1.1 "Via:" headers.                               
# ("Full" adds the server version; "Block" removes all outgoing Via: headers)           
# Set to one of: Off | On | Full | Block                                                
#ProxyVia Off                                                                           

</IfModule>

Virtual Host:

<VirtualHost *:80>                                                                      
    DocumentRoot "/home/dbrogdon/development/app/application/portal/src/main/webapp/"
    ServerName ubuntu.dev                                                               
    ErrorLog ${APACHE_LOG_DIR}/ubuntu.dev.error.log                                     
    CustomLog ${APACHE_LOG_DIR}/ubuntu.dev.access.log combined                          
    LogLevel debug                                                                      

    ProxyPass /portal/api-v1 ajp://127.0.0.1:8009/portal/api-v1                         
    ProxyPassReverse /portal/api-v1 ajp://127.0.0.1:8009/portal/api-v1                  

    ProxyPass /portal/jsp ajp://127.0.0.1:8009/portal/jsp                               
    ProxyPassReverse /portal/jsp ajp://127.0.0.1:8009/portal/jsp                        

    ProxyPass /portal/j_spring_security_check ajp://127.0.0.1:8009/portal/j_spring_security_check
    ProxyPassReverse /portal/j_spring_security_check ajp://127.0.0.1:8009/portal/j_spring_security_check

    <Directory "/home/dbrogdon/development/app/application/portal/src/main/webapp/">
        Options -Indexes FollowSymLinks                                                 
        AllowOverride AuthConfig FileInfo                                               
        Order allow,deny                                                                
        Allow from all                                                                  
    </Directory>                                                                        
</VirtualHost>

Log output:

[Fri Mar 08 07:45:41 2013] [debug] mod_proxy_ajp.c(45): proxy: AJP: canonicalising URL //127.0.0.1:8009/portal/jsp/login.jsp
[Fri Mar 08 07:45:41 2013] [debug] proxy_util.c(1506): [client 192.168.2.18] proxy: ajp: found worker ajp://127.0.0.1:8009/portal/jsp for ajp://127.0.0.1:8009/portal/jsp/login.jsp
[Fri Mar 08 07:45:41 2013] [debug] mod_proxy.c(1020): Running scheme ajp handler (attempt 0)
[Fri Mar 08 07:45:41 2013] [debug] mod_proxy_ajp.c(681): proxy: AJP: serving URL ajp://127.0.0.1:8009/portal/jsp/login.jsp
[Fri Mar 08 07:45:41 2013] [debug] proxy_util.c(2011): proxy: AJP: has acquired connection for (127.0.0.1)
[Fri Mar 08 07:45:41 2013] [debug] proxy_util.c(2067): proxy: connecting ajp://127.0.0.1:8009/portal/jsp/login.jsp to 127.0.0.1:8009
[Fri Mar 08 07:45:41 2013] [debug] proxy_util.c(2193): proxy: connected /portal/jsp/login.jsp to 127.0.0.1:8009
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(224): Into ajp_marshal_into_msgb
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(290): ajp_marshal_into_msgb: Header[0] [Host] = [ubuntu.dev]
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(290): ajp_marshal_into_msgb: Header[1] [Connection] = [keep-alive]
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(290): ajp_marshal_into_msgb: Header[2] [Cache-Control] = [no-cache]
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(290): ajp_marshal_into_msgb: Header[3] [Accept] = [text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8]
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(290): ajp_marshal_into_msgb: Header[4] [Pragma] = [no-cache]
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(290): ajp_marshal_into_msgb: Header[5] [User-Agent] = [Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.33 (KHTML, like Gecko) Chrome/27.0.1432.0 Safari/537.33]
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(290): ajp_marshal_into_msgb: Header[6] [Accept-Encoding] = [gzip,deflate,sdch]
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(290): ajp_marshal_into_msgb: Header[7] [Accept-Language] = [en-US,en;q=0.8]
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(290): ajp_marshal_into_msgb: Header[8] [Accept-Charset] = [ISO-8859-1,utf-8;q=0.7,*;q=0.3]
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(290): ajp_marshal_into_msgb: Header[9] [Cookie] = [JSESSIONID=FC550CC672F46C81CE57787EF77A7689]
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(450): ajp_marshal_into_msgb: Done
[Fri Mar 08 07:45:41 2013] [debug] mod_proxy_ajp.c(268): proxy: APR_BUCKET_IS_EOS
[Fri Mar 08 07:45:41 2013] [debug] mod_proxy_ajp.c(273): proxy: data to read (max 8186 at 4)
[Fri Mar 08 07:45:41 2013] [debug] mod_proxy_ajp.c(288): proxy: got 0 bytes of data
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(687): ajp_read_header: ajp_ilink_received 04
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(697): ajp_parse_type: got 04
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(516): ajp_unmarshal_response: status = 200
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(537): ajp_unmarshal_response: Number of headers is = 2
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(599): ajp_unmarshal_response: Header[0] [Content-Type] = [text/html;charset=ISO-8859-1]
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(609): ajp_unmarshal_response: ap_set_content_type done
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(599): ajp_unmarshal_response: Header[1] [Content-Length] = [1420]
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(687): ajp_read_header: ajp_ilink_received 03
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(697): ajp_parse_type: got 03
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(687): ajp_read_header: ajp_ilink_received 05
[Fri Mar 08 07:45:41 2013] [debug] ajp_header.c(697): ajp_parse_type: got 05
[Fri Mar 08 07:45:41 2013] [debug] mod_proxy_ajp.c(590): proxy: got response from 127.0.0.1:8009 (127.0.0.1)
[Fri Mar 08 07:45:41 2013] [debug] proxy_util.c(2029): proxy: AJP: has released connection for (127.0.0.1)
[Fri Mar 08 07:45:41 2013] [error] [client 192.168.2.18] File does not exist: /home/dbrogdon/development/app/application/portal/src/main/webapp/portal, referer: http://ubuntu.dev/portal/jsp/login.jsp

Answer

Okay, two ideas. First, in your Apache config why do you have 3 ProxyPass settings for URLs that all seem to be set to live at /portal/?

ProxyPass /portal/api-v1 ajp://127.0.0.1:8009/portal/api-v1                         
ProxyPassReverse /portal/api-v1 ajp://127.0.0.1:8009/portal/api-v1                  

ProxyPass /portal/jsp ajp://127.0.0.1:8009/portal/jsp                               
ProxyPassReverse /portal/jsp ajp://127.0.0.1:8009/portal/jsp                        

ProxyPass /portal/j_spring_security_check ajp://127.0.0.1:8009/portal/j_spring_security_check
ProxyPassReverse /portal/j_spring_security_check ajp://127.0.0.1:8009/portal/j_spring_security_check

I would suggest simplifying it—if possible—to the following:

ProxyPass /portal ajp://127.0.0.1:8009/portal/
ProxyPassReverse /portal ajp://127.0.0.1:8009/portal/

I also add this bit of mod_rewrite to make sure a trailing slash is added:

# Settings for adding a trailing slash to the URL
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(portal)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1/ [R=301,L]

Also, for mod_proxy I do this:

# Proxy specific settings
ProxyRequests Off
ProxyPreserveHost On

Which would all add up to an Apache virtual host config like this. I would also suggest rolling back proxy.conf to your default. Put all needed settings in the VirtualHost file as below that way you don’t have to do anything but make sure the config file floats between setups and you are not spreading config tweaks to all kinds of files all over the place:

<VirtualHost *:80>                                                                      
    DocumentRoot "/home/dbrogdon/development/app/application/portal/src/main/webapp/"
    ServerName ubuntu.dev                                                               
    ErrorLog ${APACHE_LOG_DIR}/ubuntu.dev.error.log                                     
    CustomLog ${APACHE_LOG_DIR}/ubuntu.dev.access.log combined                          
    LogLevel debug                                                                      

    ProxyPass /portal ajp://127.0.0.1:8009/portal/
    ProxyPassReverse /portal ajp://127.0.0.1:8009/portal/

    # Settings for adding a trailing slash to the URL
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/(portal)$
    RewriteRule ^(.*)$ http://%{HTTP_HOST}$1/ [R=301,L]

    # Settings for Apache Reverse Proxying
    <IfModule mod_proxy.c>

      # Proxy specific settings
      ProxyRequests Off
      ProxyPreserveHost On

      <Proxy *>
        AddDefaultCharset off
        Order deny,allow
        Allow from all
      </Proxy>

      ProxyPass /portal ajp://127.0.0.1:8009/portal/
      ProxyPassReverse /portal ajp://127.0.0.1:8009/portal/

    </IfModule>

    <Directory "/home/dbrogdon/development/app/application/portal/src/main/webapp/">
        Options -Indexes FollowSymLinks                                                 
        AllowOverride AuthConfig FileInfo                                               
        Order allow,deny                                                                
        Allow from all                                                                  
    </Directory>                                                                        

</VirtualHost>

This kind of setup always works great for me when bridging the world of Java web serving and Apache configs.

Attribution
Source : Link , Question Author : Darrell Brogdon , Answer Author : Giacomo1968

Leave a Comment