Apache-2.2
通過 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 設置為“調試”時,兩個日誌輸出看起來幾乎相同。
還有其他地方我可以查看為什麼請求沒有被代理到 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 "%r" %s %b" /> </Host> </Engine> </Service> </Server>
代理.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
好吧,兩個想法。首先,在您的 Apache 配置中,為什麼您有 3 個 ProxyPass 設置用於似乎都設置為 live at 的 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 Web 服務和 Apache 配置的世界時,這種設置對我來說總是很有效。