Pages

Saturday, July 18, 2015

Apache - mod_proxy_ajp

There are many other modules available in Apache which can connect to the back end servers like Tomcat. mod_proxy_ajp is on such Apache module. mod_proxy_ajp which can be used to forward a client HTTP request to an internal Tomcat application server using the AJP protocol. HTTP protocol uses a plain text format for communications. The plain text format where actual words make up the message can create a Overhead of traffic. AJP takes care of this by converting these messages into binary code thus reducing the amount of space taken by each message.

AJP or Apache JServ Protocol, is an optimized binary version of HTTP that is typically used to allow Tomcat to communicate with an Apache web server. So it is always better to use AJP protocol in communication between Apache and back end servers

Now the mpd_proxy_ajp module is used to reverse proxy to a back-end application server (e.g. Apache Tomcat) using the AJP13 protocol. The usage is similar to an HTTP reverse proxy, but uses the ajp:// prefix:

For this module to work we need another modules to be loaded into Apache which is mod_proxy. It provides support for the Apache JServ Protocol version 1.3 (hereafter AJP13).Thus, in order to get the ability of handling AJP13 protocol, mod_proxy and mod_proxy_ajp have to be present in the server.

In this article we will see how we can configure mod_prox_ajp module in Apache for 2 tomcat server running back end. Now here is the basic Virtual Host configuration done on my Apache server

LoadModule  mod_proxy_ajp  modules/mod_proxy_ajp.so

<VirtualHost myproject.local:8980>

    DocumentRoot /var/www/virtual/www.sam1.com/html/
    ServerName myproject.local
    ErrorLog logs/dummy-www.sam1.com-8280-error_log
    CustomLog logs/dummy-www.sam1.com-8280-access_log common

    <Directory "/var/www/virtual/www.sam1.com/html/">
        Options None
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
        Allow from all
    </Directory>

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

     ProxyPass           /app   ajp://172.16.202.95:18019/myApp/index.jsp
     ProxyPassReverse    /app   ajp://172.16.202.95:18019/myApp/index.jsp

     ProxyPass           /app   ajp://172.16.202.96:18020/myApp/index.jsp
     ProxyPassReverse    /app   ajp://172.16.202.96:18020/myApp/index.jsp

</VirtualHost>

The most important lines in the above configuration are,

ProxyPass           /app   ajp://172.16.202.95:18019/myApp/index.jsp
ProxyPassReverse    /app   ajp://172.16.202.95:18019/myApp/index.jsp

ProxyPass           /app   ajp://172.16.202.96:18020/myApp/index.jsp
ProxyPassReverse    /app   ajp://172.16.202.96:18020/myApp/index.jsp

The proxy pass element sends the request back to the application myApp/index.jsp which is running on the tomcat server running on 172.16.202.95:18019. So when ever we access the Apache Host URL with adding /app we hit the application running in the tomcat server . One more important thing in this case is that we need to configure the AJP protocol in the tomcat-server/conf/server.xml file.

Make sure you change the details in 172.16.202.95:18019. The The ProxyPassReverse is used to change the headers sent by the app  to Apache, before Apache sends it the browser. This element is used to modify headers before sending the response back to the browser.

In the above configuration we have the same application deployed in 2 tomcat servers running on different ips and ports. The application in any one of the tomcat servers will be accessed when we use to access them using http://myproject.local:8980/app.

Hope this helps, More to Come.

No comments :

Post a Comment