in

Apache Reverse Proxy is the easiest way to reverse proxy your web-site

May your site be on a different port number or in a different network entirely, Apache reverse proxy is the easiest way to Learn Reverse Proxy and GO!

apache reverse proxy

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

sudo apt-get update

apache reverse proxy
apache reverse proxy

sudo apt-get install apache2 -y

apache reverse proxy
apache reverse proxy

enter the following command in the terminal to see the apache main config file

cd ..

cd ..

apache reverse proxy
apache reverse proxy

Enter the following command to see if the output of the terminal is the same as given below:

ls -l

apache reverse proxy
apache reverse proxy

Now enter the following command:

cd /etc/apache2/

apache reverse proxy
apache reverse proxy

Next enter the following command in the terminal.

cd /sites-available/

apache reverse proxy
apache reverse proxy

ls -l

sudo nano 000-default.conf

apache reverse proxy
apache reverse proxy

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.

wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb

sudo apt-get install apt-transport-https

sudo apt-get update

sudo apt-get install dotnet-sdk-2.2

After entering the above given commands, lets check the version of dotnet installed.  Enter the following command to see the version of dotnet installed.

dotent –version
apache reverse proxy
apache reverse proxy

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.

cd ..

cd..

cd root

mkdir dotnetweb

cd dotnetweb

dotnet new mvc

dotnet restore

dotnet publish

cd bin

cd Debug

cd netcoreapp2.2

dotnet dotnetweb.dll

apache reverse proxy
apache reverse proxy

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,

sudo etc/intit.d/apache2 start

Now go to the sites-available folder using the following command.

sudo cd /etc/apache2/sites-available/

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.

sudo nano 000-default.con.conf
apache reverse proxy
apache reverse proxy

Delete the contents of the file and enter the following into the file.

<VirtualHost *:80>

ProxyPass  / http://localhost:5000

</VirtualHost>

apache reverse proxy
apache reverse proxy

Now save the file.   To save the file follow the commands

  • press Ctrl+x
  • press y
  • press enter key

we have to enable the apache module.  To enable the apache module, we have to enter the following command.

a2enmod

apache reverse proxy
apache reverse proxy

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.

proxy

apache reverse proxy
apache reverse proxy

and to enable proxy_http we enter the other command.

a2enmod

proxy_http

apache reverse proxy
apache reverse proxy

After enabling the modules, we are going to reload the site.  We will use

sudo /etc/init.d/apache2 reload

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

sudo apt-get update

sudo apt-get install apache2 -y

sudo rm -v /home/s216/html/index.html

The above command will display a message that the directory has been removed.

sudo vim /home/s216/html/index.html

This will open the index.html for editing purpose.  Here we can see that, we can enter a sample text as follows.

Hello World!

After entering, we can check if the server is up and running.  For that, enter the following command:

curl http://localhost

After entering the command we will get the message, hello world.

Now enter the following command:

ip a

This will reveal the IP address of the server.

After performing the above task, let us enter the same command in the second server.

sudo apt-get update

Now enter the following command in the server:

sudo apt-get install apache2 -y

Enter the following command in the terminal as well:

sudo rm -v /home/s216/html/index.html

sudo vim /home/s216/html/index.html

Now enter the following in the open vim text editor

Hello World!

After this, curl for the localhost:

curl http://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.

ip a

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.

sudo vim /etc/hosts/

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

sudo apt-get update

sudo apt-get install apache2

Now we have to enable Apache’s proxy module.  We do this by entering the following command.

sudo a2enmod proxy

sudo a2enmod proxy http

We have to disable apache’s default web site configuration.

sudo a2dissite 000-default.conf

Next, we have to create a new website configuration.

sudo vim /etc/apache2/sites-available/helloworld.con.conf

Now enter the following lines into the config file

<VirtualHost *:80>

ServerName helloworld.com

ProxyPreserveHost on

ProxyPass  / http://192.168.10.107/

ProxyPassReverse / http://192.168.10.107/

</VirtualHost>

After adding the above contents to the file, we can save the file by following below instructions:

Press the escape button

Enter

:wq!

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

<VirtualHost *:80>

ServerName helloworld.com

ProxyPreserveHost on

ProxyPass  / http://192.168.10.107:8000/

ProxyPassReverse / http://192.168.10.107:8000/

</VirtualHost>

And if the proxy server is in a different port and path, do enter the values as follows:

<VirtualHost *:80>

ServerName helloworld.com

ProxyPreserveHost on

ProxyPass  / http://192.168.10.107:8000/path

ProxyPassReverse / http://192.168.10.107:8000/path

</VirtualHost>

Enable the configuration using the following command.

sudo a2ensites helloworld.con.conf

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.

<VirtualHost *:80>

ServerName server.helloworld.com

ProxyPreserveHost on

ProxyPass  / http://192.168.10.108

ProxyPassReverse / http://192.168.10.108

</VirtualHost>

Now that we have configured everything.  Let us restart the Apache server reverse proxy.

sudo systemctl restart apache2

Conclusion:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading…

0

Comments

0 comments