- Lab
- A Cloud Guru
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.
Path Info
Table of Contents
-
Challenge
Create a Rewrite and Some ACLs
We're going to create a new subdirectory on the site,
/textfiles
and move ourtest.txt
file 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/textfiles
directory in each container using thepodman exec
command. - Next, we'll move the
test.txt
file to the/textfiles
subdirectory. - Check your work by opening a
bash
shell in one container usingpodman exec
.- Check the contents of the
/usr/share/nginx/html
directory. - 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
rewrite
to each of thefrontends
in the HAProxy configuration file that looks for requests for files that end in.txt
and sets the path for those requests to/textfiles/
.- Restart the
haproxy
service. - 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.com
are directed to thesite1
backend
, and requests forwww.site2.com
are directed to thesite2
backend
.Perform the following:
- Consolidate the 2
frontends
into a singlehttp
frontend
.- Bind the
frontend
to port80
for all addresses. - Keep the current rewrites.
- Add an
ACL
for each site, so that requests forwww.site1.com
are directed to thesite1
backend
, and requests forwww.site2.com
are directed to thesite2
backend
. - Leave the 2
backends
untouched. - Restart the
haproxy
service. - Perform the following checks with
curl
:- http://www.site1.com/test.txt
- http://www.site2.com/test.txt
- Bind the
- Consolidate the 2
What's a lab?
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.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.