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

From Notes_Wiki

Home > CentOS > CentOS 6.x > Apache web server configuration > Using ajp module to pass requests to tomcat server listening on localhost transparently

Home > CentOS > CentOS 6.x > Tomcat server configuration > Using ajp module to pass requests to tomcat server listening on localhost transparently

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.sbarjatiya.com' to tomcat server on port 8090 after prepending /login to each request.

<VirtualHost *:443>
    ServerAdmin saurabh@example.com
#   DocumentRoot /home/login/html
    ServerName login.sbarjatiya.com
    ErrorLog logs/login.sbarjatiya.com-error_log
    CustomLog logs/login.sbarjatiya.com-access_log combined
    ProxyPass / ajp://localhost:8009/login/
</VirtualHost>

This way requests coming for http://login.sbarjatiya.com go to http://localhost:8080/login and requests for http://login.sbarjatiya.com/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 saurabh@example.com
    DocumentRoot /home/energy/html
    ServerName energy.sbarjatiya.com
    ErrorLog logs/energy.sbarjatiya.com-error_log
    CustomLog logs/energy.sbarjatiya.com-access_log combined
    ProxyPass /app1/ ajp://localhost:8009/energy/
</VirtualHost>

This way only requests to http://energy.sbarjatiya.com/app1/ will get passed to tomcat server and not requests to http://energy.sbarjatiya.com.


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


Home > CentOS > CentOS 6.x > Apache web server configuration > Using ajp module to pass requests to tomcat server listening on localhost transparently

Home > CentOS > CentOS 6.x > Tomcat server configuration > Using ajp module to pass requests to tomcat server listening on localhost transparently