How to Configure Reverse Proxy with Apache in CentOS

To Configure Reverse Proxy with Apache in CentOS

Reverse proxy is a proxy server used to redirects the HTTP connection that request from client to more than one backend webservers. It provides a security and increase performance for web servers. This article shows the configuration process of Reverse Proxy with Apache in CentOS.

Testing Environment

Apache Reverse Proxy:

IP Address &ndash 192.168.5.237
Hostname &ndash node1.example.com

Apache Backend:

IP Address &ndash 192.168.5.238
Hostname &ndash node2.example.com

Pre-requisties

Fully Qualified Domain Name (FQDN) is required for all the servers.
Create the entry in /etc/hosts file for hostname resolution between reverse proxy and Apache backend.


Entry:

192.168.5.237 node1.example.com node1
192.168.5.238 node2.example.com node2

Configuration of Reverse Proxy with Apache

Before configuration process, install the Apache on backend node (node 2).

[root@node2 ~]# yum install httpd -y
Loaded plugins: fastestmirror, langpacks
base                                                                                                                                                         | 3.6 kB  00:00:02     
extras                                                                                                                                                       | 3.4 kB  00:00:00     
updates                                                                                                                                                      | 3.4 kB  00:00:00     
Loading mirror speeds from cached hostfile
 * base: mirror.fibergrid.in
 * extras: mirror.fibergrid.in
 * updates: mirror.fibergrid.in
Resolving Dependencies
-->  Running transaction check
--->  Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be installed
.
.
.
Installed:
  httpd.x86_64 0:2.4.6-40.el7.centos.4                                                                                                                                              

Dependency Installed:
  apr.x86_64 0:1.4.8-3.el7             apr-util.x86_64 0:1.5.2-6.el7             httpd-tools.x86_64 0:2.4.6-40.el7.centos.4             mailcap.noarch 0:2.1.41-2.el7            

Complete!

Start and enable the HTTPD service with the following command.

[root@node2 ~]# systemctl start httpd
[root@node2 ~]# systemctl enable httpd
ln -s ' /usr/lib/systemd/system/httpd.service'  ' /etc/systemd/system/multi-user.target.wants/httpd.service' 

Once the Apache gets enabled, add the firewall rule for outside connections.

[root@node2 ~]# firewall-cmd --permanent --add-service=http
success
[root@node2 ~]# firewall-cmd --reload
Success

Place sample webpage index.html file on document root /var/www/html.

[root@node2 ~]# cd /var/www/html
[root@node2 html]# vim index.html

Entry:

Hi welcome to linuxhelp.com

Now its time to configure reverse proxy on node1 machine. Install the httpd package with the following command.

[root@node1 ~]# yum install httpd -y
Loaded plugins: fastestmirror, langpacks
base                                                                                                                                                         | 3.6 kB  00:00:00     
extras                                                                                                                                                       | 3.4 kB  00:00:00     
updates                                                                                                                                                      | 3.4 kB  00:00:00     
Loading mirror speeds from cached hostfile
 * base: mirror.fibergrid.in
 * extras: mirror.fibergrid.in
 * updates: mirror.fibergrid.in
Resolving Dependencies
-->  Running transaction check
--->  Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be installed
.
.
.
talled:
  httpd.x86_64 0:2.4.6-40.el7.centos.4                                                                                                                                              

Dependency Installed:
  apr.x86_64 0:1.4.8-3.el7             apr-util.x86_64 0:1.5.2-6.el7             httpd-tools.x86_64 0:2.4.6-40.el7.centos.4             mailcap.noarch 0:2.1.41-2.el7            

Complete!

Immediately enable the Mod_proxy module in Apache as follows,

[root@node1 ~]# grep " mod_proxy"  /etc/httpd/conf.modules.d/00-proxy.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_express_module modules/mod_proxy_express.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
LoadModule proxy_fdpass_module modules/mod_proxy_fdpass.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so

Add the following lines to the reverse.conf file as follows,

[root@node1 ~]#  vim /etc/httpd/conf.d/reverse.conf
Apache Reverse Proxy
< IfModule mod_proxy.c> 
          ProxyRequests Off
          < Proxy *> 
                    Require all granted
          < /Proxy> 
           ProxyPass  / http://node2.example.com/
           ProxyPassReverse  / http://node2.example.com/
< /IfModule> 


Utilize the following command to start and enable httpd service.

[root@node1 ~]# systemctl restart httpd
[root@node1 ~]# systemctl enable httpd
ln -s ' /usr/lib/systemd/system/httpd.service'  ' /etc/systemd/system/multi-user.target.wants/httpd.service' 

Again add the firewall rule for outside connections.

[root@node1 ~]# firewall-cmd --permanent &ndash add-service=http
success
[root@node1 ~]# firewall-cmd --reload

Once the above process is completed, open the browser with the IP address for reverse proxy. Now you can view the sample file content as output.

FAQ
Q
What is Apache proxy?
A
Apache httpd can also act as a reverse proxy server, also-known-as a "gateway" server.
Q
Why would you use a reverse proxy?
A
A typical usage of a reverse proxy is to provide Internet users access to a server that is behind a firewall. Reverse proxies can also be used to balance the load among several back-end servers or to provide caching for a slower back-end server.
Q
What is the difference between Proxy and Reverse Proxy?
A
A reverse proxy server is a type of proxy server that typically sits behind the firewall in a private network and directs client requests to the appropriate backend server. A reverse proxy pr
Q
How to setup a Reverse Proxy with Apache Mod_Proxy?
A
For setting reverse proxy in Apache
# a2enmod proxy_http
# Service apache2 restart
Q
Is Load Balancer a reverse proxy?
A
Reverse proxy servers and load balancers are components in a client-server computing architecture. A reverse proxy accepts a request from a client, forwards it to a server that can fulfill it, and returns the server's response to the client.