Skip to main content

APACHE REVERSE PROXY WITH MOD_PROXY MODULE ON CENTOS 7

A reverse proxy is a type of proxy server that takes HTTP/HTTPS requests and distributes it to one or more backend servers. Reverse proxies are useful because many modern web applications process incoming HTTP requests using backend application servers which aren't meant to be directly accessed by user.

First install httpd
#yum install httpd

Open /etc/httpd/conf/httpd.conf and check mod_proxy and related modules is enabed.Defaultly they will be enabled.Some of mod_proxy modules are:
>mod_proxy, the main proxy module Apache module for redirecting connections; it allows Apache to act as a gateway to the underlying application servers.
>mod_proxy_http, which adds support for proxying HTTP connections.
>mod_proxy_balancer and mod_lbmethod_byrequests, which add load balancing features for multiple backend servers.
Use httpd -M command to check it
#httpd -M




We can see they are defaulty enabled.

Reverse proxying a single backend server
#cd /etc/httpd/conf.d/
#vi default-site.conf
<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://52.172.53.183:8080/
    ProxyPassReverse / http://52.172.52.183:8080/
</VirtualHost>


>ProxyPreserveHost:It makes Apache pass the original Host header to the backend server
>ProxyPass:it specifies that everything under the root URL (/) should be mapped to the backend server at the given address.
>ProxyPassReverse should have the same configuration as ProxyPass.This makes sure that if the backend server returns a location redirect header, the client's browser will be redirected to the proxy address and not the backend server address, which would not work as intended.
The configuration file would look like this:

#systemctl restart httpd

Now from browser http://your_proxy_server_ip. We can see it is directing to the server and port we mentioned in configuration((Dont forget to change default port in backend server) as per our requirement)


 Load balancing multiple backend servers
#vi default-site.conf

<VirtualHost *:80>
<Proxy balancer://mycluster>
    BalancerMember http://52.172.53.183:8080
    BalancerMember http://52.172.33.59:8081
</Proxy>

    ProxyPreserveHost On
    ProxyPass / balancer://mycluster/
    ProxyPassReverse / balancer://mycluster/
</VirtualHost>

>Here //mycluster can be any name as we want
>In BalancerMember we want give address of backend servers which we want to load balence

The configuration file would look like this:

#service httpd restart
Now from browser http://your_proxy_server_ip. We can see it is directing to both backend servers randomly.(Dont forget to change default port in backend servers as per our requirement)