日期:2014-05-17  浏览次数:20904 次

apache反向代理到jetty

apache和jetty组合使用有三种方式

There are three main ways to connect Apache to Jetty:
   1. Using Apache mod_proxy and a normal Jetty HTTP connector.
   2. Using Apache mod_proxy_ajp and the Jetty AJP connector.
   3. Using Apache mod_jk and the Jetty AJP connector. 

?

jetty推荐的是第一种

We recommend using the HTTP connectors for the following reasons:
    * Jetty performs significantly better with HTTP.
    * The AJP protocol is poorly documented and has many version irregularities.
If you must use AJP, the mod_proxy_ajp module is better than mod_jk. Previously, the load balancing capabilities of mod_jk meant that you had to use (tolerate) it, but with Apache 2.2, mod_proxy_balancer is available and works over HTTP and AJP connectors. 

?

第一种用mod_proxy,实际上是配置反向代理,只是简单的将url转发到jetty,如果要实现负载均衡,apache还需要加载mod_blancer。具体配置比较简单,首先在httpd.conf添加module

LoadModule proxy_module /usr/ali/apache2/modules/mod_proxy.so
LoadModule proxy_balancer_module /usr/ali/apache2/modules/mod_proxy_balancer.so
LoadModule proxy_http_module /usr/ali/apache2/modules/mod_proxy_http.so
LoadModule proxy_ajp_module /usr/ali/apache2/modules/mod_proxy_ajp.so
LoadModule jk_module /usr/ali/apache2/modules/mod_jk.so

?

然后在httpd.conf末尾加上反向代理配置

ProxyRequests Off

<Proxy *>
Order deny,allow
Allow from all
</Proxy>

ProxyPass /test http://localhost:8080/
ProxyPass / http://localhost:8080/

?

apache配置的是80端口,当访问http://localhost或者http://localhost/test时,请求应该转发到jetty,就是http://localhost:8080,配置完成后,启动apache,然后访问http://localhost,访问的是jetty页面。


如果要配置负载均衡,假设本机有两个jetty实例,监听端口分别是8080,8090。则简单的负载均衡配置部分如下

ProxyRequests Off

<Proxy *>
Order deny,allow
Allow from all
</Proxy>

ProxyPass /test balancer://mycluster
ProxyPass / balancer://mycluster

<Proxy balancer://mycluster>
BalancerMember http://localhost:8080
BalancerMember http://localhost:8090
</Proxy> 
?