What is a Apache Reverse Proxy?
Apache Reverse Proxy: Generally, the users make the server act as an independent server that serves the static or dynamic content when a client or user requests. But Apache and other web servers can act as a reverse proxy or a gateway server. The Reverse Proxy or the gateway server is one server that receives the user requests and provides the response.
The most common case of Apache Reverse Proxy is to use apache server as a reverse proxy for a Node.js or Dotnet server. Here the Apache server receives the request from the users using the external IP at port 80 and forwards the request to the Node or Dotnet server in the port 3000 or 5000 respectively. The angular server default port is 4200. Irrespective of the port, setting up Apache Reverse Proxy is very easy and same.
In other circumstances, we need an Apache reverse proxy to act as the reverse proxy between a few servers and external network. This is done to make sure that the client or the external users are unaware of the web application architecture. It is done for having a secure login server and more.
The following post is divided into two topics. The first topic covers How to configure apache revere proxy for localhost and the second topic covers how to configure apache reverse proxy for multiple servers. The second topic is what you want if you want to configure apache as a server as a reverse proxy for other servers that are hosted on different IP address.
Benefits of Apache Reverse Proxy:
If you wonder why to use a web server as a proxy when the main application can handle all the application request. There are certain benefits of using a web server. The main server handles the application requests from the outer network. One of the important application of this method is load balancing. Load balancing is the process of distributing the incoming traffic across multiple servers in order to lower the load on one particular server.
The purpose of distributing the traffic to multiple servers is to improve the response time of the individual server and improve web application experience for the users.
Another important feature of using a reverse proxy is caching the web application’s static files. We can cache the static resource such as the CSS, JSS, image, JSON and more. These static resource does not change over time and we can cache them such that, the next time there is a request for these resources, we can give these files as response files without having to fetch them as dynamic files.
An important feature is the ability to provide request redirect. This is used when we want to redirect a non-encrypted request to an encrypted request.
How to Configure Apache Reverse Proxy?
In this section, we are going to see apache reverse proxy configuration and how to configure apache for reverse proxy. This section is about Apache Server and we are not going to see apache tomcat. Apache Server is not Apache Tomcat. Apache Tomcat is a Java J2ee serverlet container for running Java Web Apps. It is better if we are clear about the difference.
When the site is under maintenance, we can use a reverse proxy to show the maintenance message as well.
This section is for everyone and so if you have already installed everything, please skip over the installation part.
- We will do this in Ubuntu and Linux servers. Here we have Ubuntu 14.04 to act for the server.
- Now we have to install apache2 and so we enter the following command
enter the following command in the terminal to see the apache main config file
Enter the following command to see if the output of the terminal is the same as given below:
Now enter the following command:
Next enter the following command in the terminal.
After successfully completing the above given steps, we will see a screen as shown below. Here is where we will say Apache to act as a Apache Reverse Proxy. But we do not have a site to act as a reverse proxy yet.
The following steps is to create a Dot Net Core site at port 5000:
If you already have your site running in a certain port, please skip this part. But you are also recommended to read this part as it will be useful for future references.
Here we will create a sample Dot Net Core site.
After entering the above given commands, lets check the version of dotnet installed. Enter the following command to see the version of dotnet installed.
Let us just create a sample site using dotnet and publish it. After publishing, we will just run the server and see at what port it is running from. For that enter the following commands sequentially.
After running the above commands, we can see that the site is up and running in the port 5000
If we have a site that is using a port that is different from port 80. And we want to serve the users making a request from external IP to see the site at port 80 only. Use the following instructions on how to set up apache reverse proxy.
We can start the apache server by the following command. That is,
Now go to the sites-available folder using the following command.
Now we will edit the 000-defauct.con.conf file using nano. To open the site in nano we have to enter the following command.
Delete the contents of the file and enter the following into the file.
Now save the file. To save the file follow the commands
we have to enable the apache module. To enable the apache module, we have to enter the following command.
The above commnad will list all the modules that are available in apache. From the list, we need proxy module and proxy http module. We can enter the following command in the prompt now.
and to enable proxy_http we enter the other command.
After enabling the modules, we are going to reload the site. We will use
If We enter the site’s IP address we can see the site in the browser.
Enabling Apache Reverse Proxy between servers in a Node:
First, we have to install a web server in the www-server node. We can see that any web server will work. We can go with Apache Web server 2.4.X as well. Enter the following command
The above command will display a message that the directory has been removed.
This will open the index.html for editing purpose. Here we can see that, we can enter a sample text as follows.
After entering, we can check if the server is up and running. For that, enter the following command:
After entering the command we will get the message, hello world.
Now enter the following command:
This will reveal the IP address of the server.
After performing the above task, let us enter the same command in the second server.
Now enter the following command in the server:
Enter the following command in the terminal as well:
Now enter the following in the open vim text editor
After this, curl for the localhost:
If this returns Hello World!, we will know that this server is running as well.
Now enter the following command to take note of the IP address of the server.
In the server that is acting as the proxy, we can access the server using the IP address of the server.
Setup the DNS to point to the right IP address:
Here we are going to use /etc/hosts based name resolution.
Enter the following command in the terminal.
Now enter the IP address that should receive all the incoming domain names followed by all the incoming domain names.
Now enter the following commands in the terminal
Now we have to enable Apache’s proxy module. We do this by entering the following command.
We have to disable apache’s default web site configuration.
Next, we have to create a new website configuration.
Now enter the following lines into the config file
After adding the above contents to the file, we can save the file by following below instructions:
Press the escape button
and press Enter
If the proxy server is listening on a different port, we have to configure it with the port value as well.
The following is the port value along with the IP address:
Here is an example for a port value of 8000
And if the proxy server is in a different port and path, do enter the values as follows:
Enable the configuration using the following command.
The configuration files for the second server is the same. There will be only a change of hostname and IP address. Enter the contents of the configuration file as given below.
Now that we have configured everything. Let us restart the Apache server reverse proxy.
In the above post we have seen what is the use of apache reverse proxy. What is Apache reverse proxy? How to do Apache reverse proxy and more. We also saw how to make apache reverse proxy for locally hosted web site. How to do apache reverse proxy for multiple servers and more. It is really possible for beginners to get stuck at some point. If that happens, please ask us in the comments below. We will respond as soon as possible.
For more posts on technical topics please stay tuned to TecKangaroo.