Using ajp module to pass requests to tomcat server listening on localhost transparently

From Notes_Wiki
Revision as of 04:16, 7 November 2012 by Saurabh (talk | contribs) (Created page with "=Using ajp module to pass requests to tomcat server listening on localhost transparently= We can use ajp module to pass requests coming to apache web server on port 80 to tom...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Using ajp module to pass requests to tomcat server listening on localhost transparently

We can use ajp module to pass requests coming to apache web server on port 80 to tomcat server listening on localhost port 8080. This way we do not have to give URLs containing port numbers to users and we can still use both servers apache and tomcat on same machine responding to requests on port 80.

To achieve this requests coming for entire virtual host can be proxy passed to some folder of tomcat server. For example, the below configuration passed all requests to virtual host 'login.iiit.ac.in' to tomcat server on port 8090 after prepending /login to each request.

<VirtualHost *:443>
    ServerAdmin help@iiit.ac.in
#   DocumentRoot /home/login/html
    ServerName login.iiit.ac.in
    ErrorLog logs/login.iiit.ac.in-error_log
    CustomLog logs/login.iiit.ac.in-access_log combined
    ProxyPass / ajp://localhost:8009/login/
</VirtualHost>

This way requests coming for http://login.iiit.ac.in go to http://localhost:8080/login and requests for http://login.iiit.ac.in/abc go to http://localhost:8080/login/abc. Note that port number listed in configuration 8009 is not incorrect but is the port on which ajp module listens for forwarding requests and responses between apache and tomcat servers. The actual tomcat server is listening on port 8080 and not on 8009.

In case we do not want to redirect entire virtual host and only portion of requests of virtual host need to be redirected to tomcat server. Then we can specify the corresponding URL in ProxyPass configuration as shown in below example

<VirtualHost *:80>
    ServerAdmin help@iiit.ac.in
    DocumentRoot /home/energy/html
    ServerName energy.iiit.ac.in
    ErrorLog logs/energy.iiit.ac.in-error_log
    CustomLog logs/energy.iiit.ac.in-access_log combined
    ProxyPass /coolroof/ ajp://localhost:8009/energy/
</VirtualHost>

This way only requests to http://energy.iiit.ac.in/coolroof will get passed to tomcat server and not requests to http://energy.iiit.ac.in.


Achieving user dir type effect using tomcat and apache combination

To also achieve user dir kind of effect create a folder in users home folder, say 'tomcat_root'. Then create a symbolic link of this folder in tomcat document root folder, that is do `ln -s $PWD/tomcat_root /var/lib/tomcat5/webapps/<username>'. Now create a proxy pass for users web address or virtual host to ajp://localhost:8009/<username> so that all requests get server from tomcat_root folder, present in users home folder.

Note that document root for tomcat in CentOS 5.4 is /var/lib/tomcat5/webapps and not /usr/share/tomcat5/work/Catalina/localhost