credits - https://www.haproxy.com/glossary/what-is-a-reverse-proxy |
HAProxy's certificate, and then HAProxy should forward the traffic to a specific backend server. This is a common and perfectly valid use case, but it's important to understand the details.
Here's how you can achieve this:
1. Configure HAProxy:
vi /etc/haproxy/haproxy.cfg frontend my_frontend bind *:443 ssl crt /path/to/your/certificate.pem acl is_for_backend1 hdr(host) -i backend1.example.com # Match the hostname/SAN use_backend backend1 if is_for_backend1 backend backend1 server backend1_server 192.168.1.10:80 check # Your backend server
frontend my_frontend
: Defines the frontend that listens for incoming connections.bind *:443 ssl crt /path/to/your/certificate.pem
: Binds to port 443 (HTTPS) and specifies the path to your certificate. Crucially, this certificate must havebackend1.example.com
as a SAN entry.acl is_for_backend1 hdr(host) -i backend1.example.com
: This Access Control List (ACL) checks theHost
header of the incoming HTTP request. It matches if theHost
header isbackend1.example.com
(case-insensitive).use_backend backend1 if is_for_backend1
: Directs traffic to thebackend1
backend only if theis_for_backend1
ACL matches (i.e., theHost
header isbackend1.example.com
).backend backend1
: Defines the backend server.server backend1_server 192.168.1.10:80 check
: Specifies the IP address and port of your backend server.
2. DNS Configuration:
You need to configure your DNS so that backend1.example.com
resolves to the IP address of your HAProxy server. This is how clients will be able to connect to HAProxy using that hostname.
3. Certificate:
Your SSL certificate must have backend1.example.com
listed as a Subject Alternative Name (SAN). This is essential because when a client connects to backend1.example.com
, HAProxy will present this certificate. The client will then verify that backend1.example.com
is in the certificate's SAN list. If it's not, the client will get a certificate error.
How it Works:
- Client makes a request to
https://backend1.example.com
. - DNS resolves
backend1.example.com
to HAProxy's IP address. - Client connects to HAProxy over HTTPS.
- HAProxy presents its certificate (which has
backend1.example.com
as a SAN). - Client verifies the certificate.
- HAProxy checks the
Host
header of the request. - Because the
Host
header isbackend1.example.com
, theis_for_backend1
ACL matches. - HAProxy forwards the request to the
backend1
backend server (192.168.1.10:80). - The backend server processes the request and sends the response back to HAProxy.
- HAProxy forwards the response back to the client.
Key Points:
- SAN is essential: The SAN in the certificate is the critical piece that allows the client to trust the connection to
backend1.example.com
when it's terminated at HAProxy. - Host header matching: The ACL ensures that HAProxy only forwards traffic to the correct backend when the client uses the specific hostname.
- DNS is crucial: DNS must be configured correctly so that the hostname resolves to HAProxy's IP address.
This setup allows you to use a specific hostname (backend1.example.com
) that's associated with a particular backend server, even though the connection is terminated at HAProxy. This is a very common pattern for load balancing and reverse proxying.
ReplyForward |