- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
Using HTTP Rewrites with HAProxy
Wouldn’t it be great if we could manage all our HTTP requests, all in one place? HAProxy has the power to implement HTTP rewrites, changing the request as it moves between the client and the backend servers, transparently. In this hands-on lab, we’re going to get hands-on with HAProxy, using it to configure and test a basic HTTP rewrite. We’re also going to consolidate 2 HTTP `frontends` into 1, using access control lists (ACLs) to direct traffic to the correct `backend`, based on request hostname. Upon completion of this lab, you will be able to configure an HAProxy installation to implement a basic HTTP rewrite as well as implement HAProxy ACLs.
Lab Info
Table of Contents
-
Challenge
Create a Rewrite and Some ACLs
We're going to create a new subdirectory on the site,
/textfilesand move ourtest.txtfile there. We will handle requests for the file in its original location,/test.txt, using an HTTP rewrite in HAProxy.Perform the following:
- Start by creating the new
/usr/share/nginx/html/textfilesdirectory in each container using thepodman execcommand. - Next, we'll move the
test.txtfile to the/textfilessubdirectory. - Check your work by opening a
bashshell in one container usingpodman exec.- Check the contents of the
/usr/share/nginx/htmldirectory. - Check the contents of
/usr/share/nginx/html/textfiles. - Exit the container.
- Check the contents of the
- Perform the following checks with
curl:- http://127.0.0.1:8000/test.txt
- http://127.0.0.1:8100/test.txt
- http://127.0.0.1:8000/textfiles/test.txt
- http://127.0.0.1:8100/textfiles/test.txt
- The first 2 will fail, the second 2 should work.
- Add a
rewriteto each of thefrontendsin the HAProxy configuration file that looks for requests for files that end in.txtand sets the path for those requests to/textfiles/.- Restart the
haproxyservice. - Perform the following checks with
curl:- http://127.0.0.1:8000/test.txt
- http://127.0.0.1:8100/test.txt
- http://127.0.0.1:8000/textfiles/test.txt
- http://127.0.0.1:8100/textfiles/test.txt
- All checks should work now.
- Restart the
- Start by creating the new
-
Challenge
Consolidate the HTTP Frontends Using ACLs
We're going to replace the 2 HTTP frontends with a single frontend, using ACLs, so that requests for
www.site1.comare directed to thesite1backend, and requests forwww.site2.comare directed to thesite2backend.Perform the following:
- Consolidate the 2
frontendsinto a singlehttpfrontend.- Bind the
frontendto port80for all addresses. - Keep the current rewrites.
- Add an
ACLfor each site, so that requests forwww.site1.comare directed to thesite1backend, and requests forwww.site2.comare directed to thesite2backend. - Leave the 2
backendsuntouched. - Restart the
haproxyservice. - Perform the following checks with
curl:- http://www.site1.com/test.txt
- http://www.site2.com/test.txt
- Bind the
- Consolidate the 2
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.