通过 mod_proxy_ajp 实现的 Apache 反向代理未将请求路由到 Tomcat

通过 mod_proxy_ajp 实现的 Apache 反向代理未将请求路由到 Tomcat

我安装了两个 Apache 和 Tomcat7 实例。一个安装在装有 Mountain Lion 的 Macbook Pro 上,另一个安装在装有 Ubuntu Server 12.10 的 Mac 上的 VirtualBox VM 上。两个实例都具有相同的 VirtualHost 配置和 server.xml 配置。

在我的 Mac 上一切运行正常,但在 Ubuntu VM 上却出现“文件不存在”错误。

当我将 LogLevel 设置为“debug”时,两个日志输出看起来几乎相同。

我是否可以从其他地方查看为什么请求没有代理到 Tomcat?

这是我的配置文件。

服务器.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>

虚拟主机:

<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>

日志输出:

[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

答案1

好的,有两个想法。首先,在您的 Apache 配置中,为什么您有 3 个 URL 的 ProxyPass 设置,而这些 URL 似乎都设置为有效/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

如果可能的话,我建议将其简化为以下内容:

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

我还添加了这一点mod_rewrite以确保添加了尾随斜杠:

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

另外,mod_proxy我这样做:

# Proxy specific settings
ProxyRequests Off
ProxyPreserveHost On

这些都会加起来形成一个像这样的 Apache 虚拟主机配置。我还建议恢复proxy.conf到默认设置。将所有需要的设置放入 VirtualHost 文件中,如下所示,这样您无需执行任何操作,只需确保配置文件在设置之间浮动,并且您不会将配置调整分散到各处的各种文件中:

<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>

当连接 Java 网络服务和 Apache 配置时,这种设置对我来说总是很有效。

相关内容