BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

How to Setup Apache as Reverse Proxy for Tomcat Server using mod proxy - 2020

Duke 512




Bookmark and Share





bogotobogo.com site search:




Preparing for reverse proxy

Assuming we already have installed Apache Httpd and Apache Tomcat, and running them on a Debian/Ubuntu compatible machine.

Apache server on the same host running on port 80. We'll use Apache server to get users requests and forward these requests to corresponding applications running on back-end Tomcat server on port 8080.

So, we need to configure Apache to transfer requests to tomcat.

Though there are several ways to do this, in this tutorial, we are going to use mod_proxy which is a general propose proxy module. This can be used to forward requests for a particular web application to a Tomcat instance, without having to configure a web connector such as mod_jk.

The main idea is to setup a VirtualHost exclusive for tomcat on a given path of our site.






webapps directory

Here is the /var/lib/tomcat8/webapps file structure. Under it we have our Spring Boot app (Spring Boot : Deploying War file to Tomcat 8's webapps) folder, gs-spring-boot-0.1.0:


webapps-shallow-tree.png

webapps-directory-structure.png

We want to access home page of the app by http://tomcat.example.com:80. But Tomcat server, by default, listens to 8080, and that's why we want to use proxy setup for Apache.

In our local machine we've already setup the domain via /etc/hosts file:

127.0.0.1 tomcat.example.com




Enable mod_proxy

You need to enable 2 modules: proxy and proxy_http using a2enmod command:

$ sudo a2enmod proxy

$ sudo a2enmod proxy_http




Apache VirtualHost

Here is the Apache VirtualHost configuration, /etc/apache2/sites-available/tomcat.example.com:

<VirtualHost *:80>

        ServerName tomcat.example.com

        <Directory />
                AllowOverride None
        </Directory>
        <Directory /var/lib/tomcat8/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/gs-spring-boot-0.1.0/
        ProxyPassReverse / http://localhost:8080/gs-spring-boot-0.1.0/
        <Location "/webapps">
            Order deny,allow
            Allow from all
        </Location>
</VirtualHost>

The key lines are ProxyPass*:

ProxyPass / http://localhost:8080/gs-spring-boot-0.1.0/
ProxyPassReverse / http://localhost:8080/gs-spring-boot-0.1.0/

At the '/' request, the traffics are forwarded to http://localhost:8080//gs-spring-boot-0.1.0/ via port 8080 which Tomcat is listening to. Note also it goes to gs-spring-boot-0.1.0 which is the our app name (app.war).

Then, we need to make a link for sites-enables:

$ sudo a2ensite tomcat.example.com

After enabling the site, we can check if it's really setup:

$ ls -la ../sites-enabled/
lrwxrwxrwx 1 root root   38 Feb 25 11:18 tomcat.example.conf -> ../sites-available/tomcat.example.conf

To disable it:

$ sudo a2dissite tomcat.example.com

Let's restart the server:

$ sudo service apache2 restart




Test

As we can see from the picture below, we're now able to access via our domain name (tomcat.example.com) with port 80:

tomcat-example-com.png

To access Tomcat home, we can still use localhost:8080:

Tomcat-Home.png



Session ID

It looked like the Proxy setup was complete but later I found there was an issue of keeping the session id. It appears whenever we proxied, a new session is created, so page navigation starts to break.

After a couple of tries, I came up with a solution though not perfect. The url has to have domain/x, and here is my configuration for the proxy on Apache:

<VirtualHost *:80>
        ServerName xeoman.com

        <Directory />
                AllowOverride None
        </Directory>
        <Directory /var/lib/tomcat8/webapps/>
                AllowOverride All
                Require all granted
                Options Indexes FollowSymLinks
        </Directory>

        ErrorLog /var/www/xeoman.com/logs/error.log
        CustomLog /var/www/xeoman.com/logs/access.log combined


        RedirectMatch ^/$ /x
        ProxyPass /x http://localhost:8080/x
        ProxyPassReverse /x http://localhost:8080/x

        <Location "/webapps">
            Order deny,allow
            Allow from all
        </Location>
</VirtualHost>

The redirect from '/' to '/x' is a side-effect of my solution. Otherwise, it goes to tomcat manager page but not to my root page.


Here is my site deployed on my VPS:

xeoman-com.png

xeoman-com-fileupload.png

Please let me know if you know how to remove the x in the url.


The source is available from spring-mvc-showcase.









Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Spring Boot



Spring Boot : Hello world with Mavan 3

Spring Boot : Hello world with Gradle 2

Spring Boot (Gradle 2) : Hello world with Authentication

Spring Boot : Deploying War file to Tomcat 8's webapps

How to Setup Apache as Reverse Proxy for Tomcat Server using mod proxy

Maven : mvn command cheat sheet

Spring-Boot REST API with CORS App Maven war file deploy to Tomcat

Spring-Boot / Spring Security with AngularJS - Part I (Introduction)

Spring-Boot / Spring Security with AngularJS - Part II (Dynamic resource load from Angular)

Spring-Boot / Spring Security with AngularJS : Part III (Form-based Authentication)




Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Java Tutorials



Java Tutorial Home

Basics - Compiling and Launching

Inner Classes

Constructor

Enums

Static & Finally

Default and Protected

Polymorphism

Exception Handling

Exception Handling II

String Class

Threads

Threads II - State Transition

Threads III - Synchronization

Object Class

File I/O

Serialization

ArrayList

Autoboxing

Java Graphics Interface I - Basics

Java Graphics Interface II - Labels, Text Fields, Layouts

Java Graphics Interface III - paintComponent

TCP Sockets Server/Client

Scala - Functional Java Programming

Apache CXF install

Tomcat 7 Ubuntu 14 Install on Amazon EC2 instance

What is Apache Maven?

Maven life cycle

Eclipse Maven 3 plugin on Ubuntu 14.04

Apache Maven 3 - Setting up and creating a project

Apache Maven 3 - Compile, build, and install a Maven project

Apache Maven 3 - Dependencies

Apache Maven 3 - Web Application

Apache Maven 3 - Plugins (compiler)

Apache Maven 3 - Plugins (Jetty)

Eclipse CDT / JNI (Java Native Interface) / MinGW



Spring Framework

Hello World App with Spring 4 & Maven 3 - Part I





JUnit & Maven Tutorial



JUnit 4 Introduction (Hello World)

JUnit 4 Test with Eclipse Luna (Hello World)

JUnit 4 Test with Maven (Hello World)











Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master