apache与tomcat的mod_proxy集成

apache与tomcat的整合有两种方法:

1.mod_jk模块实现

这种方法适用于较早的版本,在Apache2.2*以后的版本就无法与tomcat整合了。

2. mod_proxy代理模块实现

(1) 首先安装mod_proxy模块

sudo apt-get install libapache2-mod-proxy-html

启动mod_proxy模块

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_ajp

重启apache使它生效

sudo /etc/init.d/apache2  restart

(2)   新建apache的配置文件(proxyfile):

 sudo touch /etc/apache2/sites-available/proxyfile
sudo vim /etc/apache2/sites-available/proxyfile
<VirtualHost "*:80">
  ServerAlias 10.167.33.218
  ServerAlias xx.xxx.xx.6
  ServerAlias www.test.com
#  ServerAlias 10.167.33.4
ProxyPass /images !
ProxyPass /upload !
ProxyPass /website/css !
ProxyPass /website/js !
ProxyPass /website/images !
ProxyPass /website ajp://localhost:8009/website
  DocumentRoot "/var/www"
        <Directory / >
                Options FollowSymLinks
                AllowOverride None
        </Directory>
         <Directory "/var/www/">
                Options Indexes FollowSymLinks
                 AllowOverride None
    Order allow,deny
    Allow from all
         </Directory>
         ErrorLog /var/log/apache2/error.log
        LogLevel warn
        CustomLog /var/log/apache2/access.log combined
        ServerSignature On
</VirtualHost>
配置信息说明:



ProxyPass /website ajp://localhost:8009/website 表示所有通过url http://www.test.com/website/*访
问的都由tomcat来处理,ProxyPass /website/images !表示通过urlhttp://www.test.com/website/images/
访问的不通过tomcat处理,由apache来处理。



特别注意配置的顺序,必须先写不由tomcat代理的url,再写需要tomcat代理的url,否则配置不会生效。例如:

ProxyPass /website ajp://localhost:8009/website

ProxyPass /website/images !

配置写成这样,ProxyPass /website/images !配置就不会生效。

到此配置完成之后,执行:

sudo a2ensite  /etc/apache2/sites-available/proxyfile
注意如果出现问题 先进入到/etc/apache2/sites-available下执行sudo a2ensite proxufile 则可以生成虚拟目录1

apache会提示你执行  /etc/init.d/apache2 reload,按照提示执行:

sudo   /etc/init.d/apache2 reload

配置过程就完成了。

如果配置了ajp方式的代理,通过浏览器访问,出现

forbidden You don’t have permission

或者访问不到页面,解决的办法是修改proxy.conf文件

sudo vim /etc/apache2/mods-available/proxy.conf

需要将文件中的Deny form all注释掉,如图

image

再重启apache,问题就能解决。

Mod-proxy优点:

可以只将Apache置于公网,节省公网IP地址资源。可以通过设置来实现Apache专门负责处理静态网页,让Tomcat专门负责处理JSP和servlet等动态请求。

Mod-proxy的缺点:

当其中一台tomcat停止运行的时候,apache仍然会转发请求过去,导致502网关错误。但是只要服务器再启动就不存在这个问题。如果前置Apache代理服务器停止运行,所有集群服务将无法对外提供。

猜你喜欢

转载自baalwolf.iteye.com/blog/1172808
今日推荐