In this tutorial, we will learn how to perform Apache Reverse Proxy configuration, step by step. A reverse proxy is a kind of proxy server that takes HTTP or HTTPS requests & transfers/distributes them to one or more backend servers. A reverse proxy is useful in many ways, like

  • It can hide the origin server, thus making it more secure & immune to attacks,
  • It can act as a load balancer,
  • A reverse proxy can also be used to encrypting/decrypting web server traffic, thus taking some load off from the backend servers.
  • It can also be used for caching static as well as dynamic contents, which also reduces the load off the web servers.

In this tutorial, we are going to discuss how we can use Apache as a reverse proxy server on CentOS/RHEL machines. So let's start with the pre-requisites needed for creating apache as a reverse proxy.

apache reverse proxy configuration step by step

(Recommended read: Guide for creating a LAMP server on CentOS/RHEL)


Pre-requisites

- We will be using both Apache as reverse proxy as well as a backend server, though we can also use some other application or webserver like wildfly or Nginx as backend servers. But for the purpose of this tutorial, we will be using the Apache server only.

So we need to have the Apache server installed on both servers. Install Apache with the following command,

$ sudo yum install httpd

For detailed installation of Apache web server, refer to our article 'Step by Step guide to configure APACHE server.

https://services.vlitag.com/vpaid/?q=9ba3f2539ddb6b2df45a4f25fbfd7843&defaultVolume=&page_url=https://linuxtechlab.com

Modules needed for using Apache as a reverse proxy

After the apache has been installed on the machine, we need to make sure that the following modules are installed & activated on the apache machine, that will be used as a reverse proxy,

1- mod_proxy – it is the main module responsible for redirecting the connections,

2- mod_proxy_http – add the support for proxying HTTP connections,

Check if the following modules are installed & working with the following command,

$ httpd -M

This command will generate the list of modules that are currently working. If these modules are not on the list, then we need to enable them by making the following entry in httpd.conf,

$ sudo vim /etc/httpd/conf/httpd.conf

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_http_module modules/mod_proxy_http.so

Now save the file & exit, then restart the apache service to implement the changes made,

$ sudo systemctl restart httpd


Configuring Backend test server

We have also installed apache on the backend server & will now add a simple html page for testing purposes,

$ sudo vim /var/www/html/index.html

<html>

<head>

<title>Test page for Backend server</title>

</head>

<body>

This is a simple test page hosted on the backend server.

</body>

</html>

Save the file & exit. Now restart the apache service to implement the changes made. Next test the page from a browser on the local or remote system with the following URL,

http://192.168.1.50

where 192.168.1.50 is the IP address of the backend server.


Configuring a simple reverse proxy

After the backend server is ready, the next thing to do is to make our front end i.e. reverse proxy ready. To do so, we need to make the following entry in the apache configuration file i.e. httpd.conf,

$ sudo vim /etc/httpd/conf/httpd.conf

<VirtualHost *:80>

ProxyPreserveHost On

ProxyPass / http://192.168.1.50/

ProxyPassReverse / http://192.168.1.50/

</VirtualHost>

here, we are telling with the 'ProxyPass' parameter that whatever request s received at '/', redirect it to 'http://192.168.1.50/'. Now restart the apache services to implement the changes,

$ sudo systemctl restart httpd

Note:- We can also add port numbers here, for example, we are using this reverse proxy with tomcat as a backend server, we can also this frontend server as a reverse proxy for apache tomcat with the following entries in httpd.conf,

<VirtualHost *:80>

ProxyPreserveHost On

ProxyPass / http://192.168.1.50:8080/test/

ProxyPassReverse / http://192.168.1.50:8080/test/

</VirtualHost>


Testing the reverse proxy

To test the reverse proxy, open the following URL from a web browser,

http://192.168.1.100/

here 192.168.1.100 is the IP address of the reverse proxy server. As soon as the URL loads up, we can then see the page that was hosted on the backend server. This shows that our reverse proxy is correctly configured & working.

This completes our tutorial on Apache Reverse Proxy configuration, step by step. Please do leave any questions/queries you have in the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

Linux TechLab is thankful for your continued support.