Apache HTTP Server as Reverse Proxy with Java Back-End Application running on Tomcat
This is a simple tutorial of how to use the reverse proxy module of Apache together with back-end application.
Web Server
A web server is a system that stores and delivers content or services requested by clients over the internet.
Apache HTTP Server
Apache HTTP Server is a free and open-source cross-platform web server software. It is the most popular web server which works with Multi-Processing Modules (MPMs). These modules helps the server to extend it’s functionality. Apache supports reverse proxy with caching which is our topic in this article.
Reverse-Proxy
Proxy server is a server that acts as an intermediary for requests from clients seeking resources from other servers. A reverse proxy server is a type of proxy server that sits behind the firewall in a private network and directs client requests to the appropriate back-end server.
What we will do?
Here’s a scheme what we will do in this tutorial. When a user sends request to http://localhost/api/user it will be delivered to Apache web server. Here we will have a proxy configuration dealing with /api requests. As a back-end application we will have Java web app running on port 8080 with Tomcat. This app will accepts route “/users” and will return “Hello, user!” and this will be the respond sent to the user.
What we will need before starting?
- Linux Terminal
- IntelliJ
- Tomcat
Installing Apache
Let’s go to the terminal and update the package lists before proceeding to install our server:
$ sudo apt-get update
Now we can install Apache 2:
$ sudo apt-get install apache2 -y
Enabling Apache Modules
As you know, Apache has many modules to extend the core functionality. For our purpose we will be using:
mod_proxy
— the main proxy module for redirecting connectionsmod_proxy_http
— adds support for proxying HTTP connections
Let’s install them:
$ sudo a2enmod proxy
$ sudo a2enmod proxy_http
Now it’s time to restart Apache:
$ sudo service apache2 restart
Creating Back-end
New Maven Project
I assume you have already installed Tomcat, so let’s continue with creating a new Maven project in IntelliJ:
In pom.xml add this dependency:
In src/main create new folder, name it “java” and with right click -> “Make Directory as” -> “Sources Root”.
Creating a servlet
Let’s create a servlet! A servlet is a class which responds to a particular type of network request, in our case — a HTTP request. In java folder create new class, name it “UsersServlet” and write this:
Tomcat Configuration
Add new configuration — Tomcat-Local. Add artifact and save it.
Now we can run the application.
Enabling Reverse Proxy
We will set up the default Apache virtual host to serve as a reverse proxy for single back-end server. Open the default Apache configuration:
$ sudo nano /etc/apache2/sites-available/000-default.conf
Replace the content in VirtualHost
with the following:
Let’s restart Apache:
$ sudo service apache2 restart
Result
Now, if you access http://your_server_ip/api/users
in a web browser, you will see your back-end server response.
You can check my back-end app here: GitHub