【问题标题】:Load balancing with Apache and Tomcat combined with URL redirect使用 Apache 和 Tomcat 进行负载平衡并结合 URL 重定向
【发布时间】:2012-06-06 09:38:19
【问题描述】:

我现在有一台 Apache 服务器和两台 Tomcat 服务器。它们使用 mod_jk 模块连接。并且配置了负载均衡。所有请求都会被重定向到负载均衡器,在 httpd.conf:

JKMount /* controller

控制器是负载均衡器,工作的tomcat服务器是worker1,worker2。

问题是,除了自动加载dispatch之外,我还需要一个url匹配重定向。具体来说,http://www.example.com/test1/index.html 的请求应该发给 worker1(Tomcat),而 http://www.example.com/test2/index.html 应该发给 worker2。 但是,在worker1和worker2中,应用程序结构都是webapps/test/结构。

我可以使用 mod_jk url 映射来分派 /test1/ 到 worker1 和 /test2/ 到 worker2,但是 PATH 将是 /test1/ 和 /test2/ 而不是 /test/。同时,如果我使用 apache redirectMatch 或 url rewrite 将 /test1/(/test2/) 更改为 /test/,mod_jk 现在不会将 url 分派给不同的 worker,因为它们具有相同的 PATH。

我该如何处理这种情况?

【问题讨论】:

  • 如果您需要在代理期间更改路径,mod_proxy_ajp 可能更适合您。

标签: apache tomcat servlets url-rewriting mod-jk


【解决方案1】:

也许一个简单的方法是在 tomcat 工作人员上使用urlrewrite 过滤器。根据documantation,您的 urlrewrite.xml 文件应该有以下规则:

    <rule>
       <from>^/test[0-9]*/(.*)$</from>
       <to type="redirect">/$1</to>
    </rule>

因此工作人员会忽略 test1 或 test2 URI 部分。并且 apache 可以按照您使用 mod_jk 计划的方式工作。

【讨论】:

    【解决方案2】:

    您需要使应用程序成为 Tomcat 中的根应用程序。您可以通过向您的应用添加 META-INF/context.xml 来做到这一点:

    <Context path="/"/>
    

    我建议您从 webapps 目录中删除其他应用程序。然后您需要更改您的应用程序 web.xml,以便 servlet(s) 现在映射到具有适当上下文的适当 url:

    <servlet-mapping>
        <servlet-name>TestApp</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>TestApp</servlet-name>
        <url-pattern>/test1</url-pattern>
    </servlet-mapping>
    

    第二个 JVM 中的应用程序需要 url-pattern /test2 代替。对于 Apache/Tomcat 连接,我使用 mod_ajp 而不是 mod_jk。这是您在 Apache 中为 mod_ajp 所需要的:

    <Proxy balancer://cluster>
        BalancerMember ajp://127.0.0.1:8015 route=ajp13_node1
        BalancerMember ajp://127.0.0.1:8016 route=ajp13_node2
    </Proxy>
    <Location "/test">
        ProxyPass balancer://cluster/test stickysession=JSESSIONID
    </Location>
    <Location "/test1">
        ProxyPass ajp://127.0.0.1:8015/test1
    </Location>
    <Location "/test2">
        ProxyPass ajp://127.0.0.1:8016/test2
    </Location>
    

    这是假设 AJP 连接器正在侦听第一个 JVM 的 8015 和第二个 JVM 的 8016。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      • 2012-06-04
      • 1970-01-01
      • 2017-06-19
      • 1970-01-01
      • 1970-01-01
      • 2013-08-23
      相关资源
      最近更新 更多