LINUX, FOSS AND LIBRARY TECHNOLOGY ENTHUSIAST

Saturday, January 29, 2022

Setting up Apache as Reverse Proxy for Tomcat Server

0 comments

This blog post will help you to set up an Apache web server as Reverse Proxy for the Tomcat hosted applications.


Setup Scenario


I have installed DSpace, Tomcat is running on port 8080 and DSpace has two interfaces running with the following URLs.


http://localhost:8080/xmlui

http://localhost:8080/jspui


Now I have installed the Apache server on the same host running on port 80. I will use the Apache server to get users' requests and transfer these requests to corresponding interfaces running on the back-end Tomcat server on port 8080. I need to configure Apache to transfer requests to tomcat like below:


http://dspace.opensio.co.in >> http://localhost:8080/xmlui/

http://dspace.opensio.co.in >> http://localhost:8080/jspui/


Let’s start the configuration


The Debian-based systems use the following command to enable the Proxy module with Apache. 


sudo a2enmod proxy


Configure Apache Virtual Hosts


Now will start working with the virtual host. We are creating three virtual hosts as below. You create only what is required with needed modifications. Edit Apache main configuration file/create a new one and start with the configuration.


VirtualHost for XMLUI :-


To forward all requests sent to dspace.opensio.co.in to backend tomcat server corresponding interface like:


http://dspace.opensio.co.in >> http://localhost:8080/xmlui/


sudo vim /etc/apache2/sites-available/dspace.opensio.co.in.conf


Configure virtual host like this.


<VirtualHost *:80>

        ServerName dspace.opensio.co.in

        <Directory />

                AllowOverride None

        </Directory>

        <Directory /opt/tomcat/webapps/>

                AllowOverride All

                Require all granted

                Options Indexes FollowSymLinks

        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

        ProxyPass / http://localhost:8080/xmlui

        ProxyPassReverse / http://localhost:8080/xmlui http://dspace.opensio.co.in

        <Location "/webapps">

            Order deny,allow

            Allow from all

        </Location>

</VirtualHost>


VirtualHost for JSPUI:-


To forward all requests sent to dspace.opensio.co.in to backend tomcat server corresponding interface like:


http://dspace.opensio.co.in >> http://localhost:8080/jspui/


Configure virtual host like this.


<VirtualHost *:80>

        ServerName dspace.opensio.co.in

        <Directory />

                AllowOverride None

        </Directory>

        <Directory /opt/tomcat/webapps/>

                AllowOverride All

                Require all granted

                Options Indexes FollowSymLinks

        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

        ProxyPass / http://localhost:8080/jspui

        ProxyPassReverse / http://localhost:8080/jspui http://dspace.opensio.co.in

        <Location "/webapps">

            Order deny,allow

            Allow from all

        </Location>

</VirtualHost>


Restart Apache and Test


After making all necessary changes restart the Apache service using the following command and access your sites in a web browser. Make sure you are getting proper pages from tomcat.


sudo a2ensite dspace.opensio.co.in

sudo systemctl reload apache2

sudo systemctl restart apache2


Reference: 

https://www.bogotobogo.com/Java/tutorials/Spring-Boot/How-to-Setup-Apache-as-Reverse-Proxy-for-Tomcat8-Server-using-mod-proxy.php

No comments:

Post a Comment