Sunday, January 7, 2018

How to use Apache as Reverse Proxy on CentOS & RHEL

http://linuxtechlab.com/apache-as-reverse-proxy-centos-rhel

Reverse proxy is a kind of proxy server that takes http or https request & transfers/distributes them to one or more backend servers. Reverse proxy is useful in many ways, like
– It can hide the origin serve, thus making it more secure & immune to attacks,
– It can act as a load balancer,
– Reverse proxy can also be used to encrypting/decrypting webserver 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 load off the web servers.
In this tutorial, we are going to discuss how we can use Apache as reverse proxy server on CentOS/RHEL machines. So let’s start with the per-requisites needed for creating apache as reverse proxy,
(Recommended read :Easiest guide for creating a LAMP server on CentOS/RHEL)

Pre-requisites

– We will be using both apache as reverse proxy as well as 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 apache server only.
So we need to have Apache server installed on both the servers. Install apache with the following command,
$ sudo yum install httpd
For detailed installation of Apache webserver, refer to our article ‘Step by Step guide to configure APACHE server.

Modules needed for using Apache as reverse proxy

After the apache has been installed on the machine, we need to make sure that following modules are installed & activated on the apache machine, that will be used as 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 among the list, than 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_h
Now save the file & exit, than restart the apache service to implement the changes made,
$ sudo systemctl restart httpd

Configuring Backend test server

We have also installed apache on backend server & will now add a simple html page for testing purposes,
$ sudo vim /var/www/html/index.html


Test page for Backend server

This is a simple test page hosted on backend server.


Save the file & exit. Now restart the apache service to implement the changes made. Next test the page from a browser on 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 simple reverse proxy

After the backend server is ready, 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 apache configuration file i.e. httpd.conf,
$ sudo vim /etc/httpd/conf/httpd.conf

ProxyPreserveHost On
ProxyPass / http://192.168.1.50/
ProxyPassReverse / http://192.168.1.50/

here, we are telling with ‘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, like for example we are using this reverse proxy with tomcat as backend server, we can also this frontend server as reverse proxy for apache tomcat with the following entries in httpd.conf,

ProxyPreserveHost On
ProxyPass / http://192.168.1.50:8080/test/
ProxyPassReverse / http://192.168.1.50:8080/test/


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 than see the page that was hosted on backend server. This shows that our reverse proxy is correctly configured & working.
In our future tutorial, we will learn how to configure the apache reverse proxy as loadbalancer. Please do leave any questions/queries you have in the comment box below.

No comments:

Post a Comment