如何使用mod_jk挂载上下文引用的Tomcat应用程序?

时间:2011-02-28 06:26:38

标签: tomcat apache2 mod-jk

我在/foo上下文的Tomcat中运行了一个WAR应用程序,这意味着它的URL是http://example.com:8080/foo。现在我正在尝试通过mod_jk将Apache HTTP Server连接到Tomcat。这是我的workers.properties文件:

worker.list=foo
worker.foo.port=8009
worker.foo.host=localhost
worker.foo.type=ajp13
worker.foo.mount=/foo/*

工作正常,但在此网址:http://example.com/foo。我希望它在http://example.com。我错过了什么?

PS。这是我的mod-jk.conf,它包含在httpd.conf中:

LoadModule jk_module modules/mod_jk.so
JkWorkersFile /usr/local/tomcat/conf/workers.properties
<VirtualHost *:80>
  ServerName foo.example.com
  JkMount /* foo
</VirtualHost>

2 个答案:

答案 0 :(得分:9)

您基本上有两个选择:

  1. 修改Tomcat配置以在根目录下安装WAR。如何完成这取决于您部署应用程序的准确程度。除非有一些预防因素,否则这是更清洁的方法。
  2. 通过使用mod_rewrite重写以/ to / foo开头的URL来处理Apache端的问题,此时它将通过JkMount传递给Tomcat
  3. 对于第二个选项,您的Apache配置看起来像这样:

    # Turn on mod_rewrite
    RewriteEngine On
    # This is the rule. Use regexp to match any URL beginning with /, and rewrite it to
    # /foo/remaining_part_of_URL. The [PT] (pass-through) is necessary to make rewritten
    # requests go through JkMount
    RewriteRule ^/(.*) /foo/$1 [PT]
    
    # Forward all URLs starting with foo to Tomcat
    JkMount /foo/* worker
    

    (这实际上没有经过测试,希望它按原样运行!)。您可能还需要在Apache中启用mod_rewrite(查看您的发行版,启用mods的目录可能就是答案)。

    如果你需要了解更多关于mod_rewrite(非常强大的野兽)的信息,请点击此处: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule

答案 1 :(得分:0)

这是一个Mod Rewrite Solution

<强> WORKERS.PROPERTIES

worker.list=worker1
worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.type=ajp13
worker.worker1.mount=/foo/*           #THIS IS THE APP NAME: "FOO"

<强> HTTPD.CONF

<VirtualHost *:80>
   RewriteEngine On
   RewriteRule ^/(.*)/Foo/$1 [PT]
   ServerName example.com             #DOMAIN NAME: "example.com"
   ServerAlias www.example.com
   JkMount /foo/* worker1
 </VirtualHost>