将 tomcat6 连接到 apache2

将 tomcat6 连接到 apache2

免责声明:不是服务器管理员

我已经为此苦思冥想了好几个星期(请注意,不是一直苦思冥想,因为那样会让人抓狂)。我一直在尝试将我的 apache2 服务器连接到我的 tomcat 服务器,以至于如果有人在浏览我的 Web 目录时遇到 *.jsp 或任何 servelet,它就会被移交给 tomcat。

我拥有两者Apache2.0 (port 9099),并Tomcat6 (9089)在同一个盒子上运行 Debian lenny。

目前mod_jk已启用mod_jk.conf以下$apacheHOME/mods-enabled/内容:

    # Where to find workers.properties
JkWorkersFile /etc/apache2/workers.properties
    # Where to put jk shared memory
JkShmFile     /var/log/at_jk/mod_jk.shm
    # Where to put jk logs
JkLogFile     /var/log/at_jk/mod_jk.log
    # Set the jk log level [debug/error/info]
JkLogLevel    info
    # Select the timestamp log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
    # Send servlet for context /examples to worker named worker1
JkMount  /*/servlet/* worker1
    # Send JSPs  for context /examples to worker named worker1
JkMount  /*.jsp worker1

workers.properties位于的$apacheHOME/内容:

workers.tomcat_home=/var/lib/tomcat6
workers.java_home=/usr/lib/jdk1.6.0_23/db/
worker.list=worker1
ps=/

worker.worker1.port=9081
worker.worker1.host=localhost
worker.worker1.type=ajp13

web.xml$tomcatHOME/conf已启用以下 servlet

<servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-cla$
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <init-param>
        <param-name>listings</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>


<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
        <param-name>fork</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>xpoweredBy</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>30</session-timeout>
</session-config>

服务器.xml:

--> -->

<!-- Define an AJP 1.3 Connector on port 8009 -->

<Connector port="9081" protocol="AJP/1.3" redirectPort="9088" />

mod_jk.log:

[Mon Feb 21 13:49:35 2011] [2921:3075307264] [info] jk_open_socket::jk_connect.c (566): connect to 127.0.0.1:9081 failed (errno=111)
[Mon Feb 21 13:49:35 2011] [2921:3075307264] [info] ajp_connect_to_endpoint::jk_ajp_common.c (869): Failed opening socket to (127.0.0.1:9081) (errno=111)
[Mon Feb 21 13:49:35 2011] [2921:3075307264] [error] ajp_send_request::jk_ajp_common.c (1359): (worker1) connecting to backend failed. Tomcat is probably not started or is listening on the wrong port (errno=111)
[Mon Feb 21 13:49:35 2011] [2921:3075307264] [info] ajp_service::jk_ajp_common.c (2194): (worker1) sending request to tomcat failed (recoverable), because of error during request sending (attempt=1)
[Mon Feb 21 13:49:35 2011] [2921:3075307264] [info] jk_open_socket::jk_connect.c (566): connect to 127.0.0.1:9081 failed (errno=111)
[Mon Feb 21 13:49:35 2011] [2921:3075307264] [info] ajp_connect_to_endpoint::jk_ajp_common.c (869): Failed opening socket to (127.0.0.1:9081) (errno=111)
[Mon Feb 21 13:49:35 2011] [2921:3075307264] [error] ajp_send_request::jk_ajp_common.c (1359): (worker1) connecting to backend failed. Tomcat is probably not started or is listening on the wrong port (errno=111)
[Mon Feb 21 13:49:35 2011] [2921:3075307264] [info] ajp_service::jk_ajp_common.c (2194): (worker1) sending request to tomcat failed (recoverable), because of error during request sending (attempt=2)
[Mon Feb 21 13:49:35 2011] [2921:3075307264] [error] ajp_service::jk_ajp_common.c (2212): (worker1) Connecting to tomcat failed. Tomcat is probably not started or is listening on the wrong port
[Mon Feb 21 13:49:35 2011] [2921:3075307264] [info] jk_handler::mod_jk.c (2364): Service error=-3 for worker=worker1

端口 9081 似乎存在问题,没有进程正在使用它,因此我认为它的配置不正确。尽管我的 tomcat 端口工作正常。

 ickronia:/var/log/at_jk# telnet localhost 9081
 Trying ::1...
 Trying 127.0.0.1...
 telnet: Unable to connect to remote host: Connection refused
 ickronia:/var/log/at_jk#

据我所知,没有什么奇怪的,因为 apache2、tomcat 和 mod_jk 日志都显示绿色;然而,每当我导航到 jsp 时,它只会显示 javascript。

尽管我仔细查看了日志和文档以寻求帮助,但我还是不确定问题到底是什么。我对 servelet 世界还只是个新手。

答案1

回答二,因为编辑太多了。这是我的工作设置:-

mod-jk.conf:-

JkWorkersFile /etc/libapache2-mod-jk/workers.properties
JkLogFile /var/log/apache2/mod_jk.log
JkLogLevel info
JkMount /portal worker1
JkMount /portal/* worker1

在已启用站点中:-

<VirtualHost _default_:443>
    JkMountCopy On
    blah
    blah
</VirtualHost>

worker.properties(通常通过负载均衡器,但这里简化了):-

worker.list=worker1

worker.worker1.port=9009
worker.worker1.host=host_name
worker.worker1.type=ajp13

tomcat 服务器.xml:-

 <?xml version='1.0' encoding='utf-8'?>
<Server port="9005" shutdown="SHUTDOWN">

  <!-- cut -->

  <Service name="Catalina">
    <Connector port="9080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="9443" />

    <Connector port="9443" protocol="HTTP/1.1" SSLEnabled="true"
               keystoreFile="<path_to_keystore>.ks"
               kestorePass="<password for keystore>"
               maxThreads="200" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />

    <Connector port="9009" protocol="AJP/1.3" redirectPort="9443" />

    <Engine name="Catalina" defaultHost="localhost" jvmRoute="worker1">
      <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">    
      </Host>
    </Engine>
  </Service>
</Server>

因此,Tomcat 正在 9080 上监听 http、在 9443 上监听 https 并在 9009 上监听 ajp。

Apache 正在监听 https 端口 443。发送到该服务器上的 /portal 上下文的任何内容都会被发送到 mod_jk worker1,然后通过 ajp 转发到 tomcat。

希望这可以帮助!

答案2

我猜想 tomcat 中的 server.xml 没有使用 CoyoteConnector 作为端口 9089,而该端口使用 ajp 协议。我怀疑您使用的是 http 协议,如果您遇到http://服务器:9089/它将会加载。

您将希望让 worker.properties 使用 CoyoteConnector 端口。

答案3

您没有说明是否在虚拟主机中执行了 JKMountCopy。

在我的 ubuntu 机器上的 /etc/apache2/sites-enabled/000-default-ssl

<VitualHost _default_:443>
  JkMountCopy On

  blah 
  blah

</VirtualHost>

http://tomcat.apache.org/connectors-doc/reference/apache.html如果你去查找 JkMount 的指令描述,你会看到全局指令是不是由 VirtualHosts 继承。您需要重新指定,或者如果您想要提取所需的所有内容,请使用 JkMountCopy On。

还要确保您要连接的端口在 server.xml 中设置了 AJP 连接器。重定向端口指的是 ssl 端口。

从 Tomcat 附带的示例中...

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

相关内容