Configure Reverse Proxy with Ansible Playbook!!

Yash Panchwatkar
FAUN — Developer Community 🐾
5 min readMar 16, 2021

--

In this story, I am going to set up a Reverse Proxy server with the help of Ansible.

What is a Reverse proxy?

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 provides an additional level of abstraction and control to ensure the smooth flow of network traffic between clients and servers.

What is Round Robin?

Roundrobin load balancing is one of the simplest methods for distributing client requests across a group of servers. Going down the list of servers in the group, the roundrobin load balancer forwards a client request to each server in turn.

For achieving this setup we are going to use the Haproxy server. So without wasting any time let’s jump on practical.

My Ansible inventory file is shown here. For this setup, one computer(lb group) will be configured as a load balancer and another two computers(web group) will get configured as Web servers.

I am creating a file name haproxy.yml.

Here package module will install the Haproxy. After installing the package file named haproxy.cfg will copy to the directory ‘/etc/haproxy/’ where packages get installed.

By default, this configuration/template is shown below.

Here bind keyword on line No 68 denotes which port you have to use for this service and on line No 87 we have to mention the backend servers.

Unfortunately, ansible does not support runtime variable creation so we have to define them at the time of creation. If our requirement is to retrieve the information of ansible hosts file or groups then we have a special variable named as groups. so the use of this variable is already shown in the Haproxy template file.

use of groups variable

Now let us run ansible-playbook.

Here all tasks are done, so let’s crosscheck whether ansible has done the configuration or not

Checking IP of lb here status of haproxy is active. let us check the configuration file

You can see over here that ansible added entry dynamically as we want so finally load balancer setup is done. let us check web group IP/ system

Here HTTPD service is active and content is delivered to its destination file. so let us check our setup on the browser whether it load balancing or not. so for better understanding, I am changing some entries in the index.html file from the web group

So Now our setup is done. let us check is it working or not?

Yeah, It is working fine now after refreshing the page you can see our proxy server connected to different IP

Conclusion:

Today is the world of the Internet and all the content is hosted on the webserver and millions of client use the services and to serve this huge number of client company require many webservers running but the challenge is all the webserver have a different IP address so the client has to remember all the IP which is practically not possible so to overcome this challenge we use Load Balancer and to automate this process we used Ansible.

checkout my GitHub for code.

Thanks for reading… If any questions please feel free to leave a comment below and Do connect with me on these platforms.

  1. Mail: yashpanchwatkar@gmail.com
  2. LinkedIn: https://www.linkedin.com/in/yash-panchwatkar/

Stay tuned for such awesome stories!

👋 Join FAUN today and receive similar stories each week in your inbox! Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--