HAProxy: Configuring Multiple Single Domain SSL Certs for Domain and Subdomain
Image by Toru - hkhazo.biz.id

HAProxy: Configuring Multiple Single Domain SSL Certs for Domain and Subdomain

Posted on

Are you tired of dealing with the complexity of configuring multiple SSL certificates for your domain and subdomains using HAProxy? Look no further! In this article, we’ll take you through a step-by-step guide on how to configure multiple single domain SSL certificates for your domain and subdomains using HAProxy.

What is HAProxy?

HAProxy is a free, open-source load balancer and proxy server that allows you to distribute incoming traffic across multiple servers, ensuring high availability and scalability. It’s widely used in modern web infrastructure to handle HTTP and TCP traffic.

Why Multiple Single Domain SSL Certs?

Multiple single domain SSL certificates are essential when you have multiple domains and subdomains that require separate SSL certificates. This approach provides better security and flexibility compared to using a single wildcard SSL certificate.

For instance, let’s say you have a domain “example.com” and two subdomains “blog.example.com” and “shop.example.com”. You can obtain separate SSL certificates for each domain and subdomain, ensuring that each one is secured independently.

Prerequisites

Before we dive into the configuration process, make sure you have the following prerequisites in place:

  • HAProxy installed and configured on your server
  • Multiple single domain SSL certificates for your domain and subdomains
  • A basic understanding of HAProxy configuration files

Step 1: Create a Separate Backend for Each Domain and Subdomain

In HAProxy, a backend is a group of servers that can receive traffic from the frontend. We’ll create a separate backend for each domain and subdomain to ensure that each one is handled independently.

backend example_com
    mode http
    server server1 127.0.0.1:8000 check
    server server2 127.0.0.1:8001 check

backend blog_example_com
    mode http
    server server1 127.0.0.1:8002 check
    server server2 127.0.0.1:8003 check

backend shop_example_com
    mode http
    server server1 127.0.0.1:8004 check
    server server2 127.0.0.1:8005 check

Step 2: Create a Separate Frontend for Each Domain and Subdomain

A frontend in HAProxy defines how incoming traffic is handled. We’ll create a separate frontend for each domain and subdomain to ensure that each one is handled independently.

frontend example_com_frontend
    bind *:443 ssl crt /etc/haproxy/ssl/example_com.pem
    mode http
    default_backend example_com

frontend blog_example_com_frontend
    bind *:443 ssl crt /etc/haproxy/ssl/blog_example_com.pem
    mode http
    default_backend blog_example_com

frontend shop_example_com_frontend
    bind *:443 ssl crt /etc/haproxy/ssl/shop_example_com.pem
    mode http
    default_backend shop_example_com

Step 3: Configure SSL Certificates for Each Domain and Subdomain

We’ll configure the SSL certificates for each domain and subdomain by specifying the path to the SSL certificate file.

bind *:443 ssl crt /etc/haproxy/ssl/example_com.pem
bind *:443 ssl crt /etc/haproxy/ssl/blog_example_com.pem
bind *:443 ssl crt /etc/haproxy/ssl/shop_example_com.pem

Step 4: Configure HAProxy to Use SNI

SNI (Server Name Indication) is a feature that allows HAProxy to determine which SSL certificate to use based on the domain name in the incoming request.

frontend example_com_frontend
    bind *:443 ssl crt /etc/haproxy/ssl/example_com.pem sni req h reqi hdr(host) -m end
    mode http
    default_backend example_com

frontend blog_example_com_frontend
    bind *:443 ssl crt /etc/haproxy/ssl/blog_example_com.pem sni req h reqi hdr(host) -m end
    mode http
    default_backend blog_example_com

frontend shop_example_com_frontend
    bind *:443 ssl crt /etc/haproxy/ssl/shop_example_com.pem sni req h reqi hdr(host) -m end
    mode http
    default_backend shop_example_com

Step 5: Configure HAProxy to Use TCP Mode

By default, HAProxy operates in HTTP mode. However, since we’re using SSL certificates, we need to configure HAProxy to use TCP mode.

frontend example_com_frontend
    bind *:443 tcp
    mode tcp
    default_backend example_com

frontend blog_example_com_frontend
    bind *:443 tcp
    mode tcp
    default_backend blog_example_com

frontend shop_example_com_frontend
    bind *:443 tcp
    mode tcp
    default_backend shop_example_com

Step 6: Reload HAProxy Configuration

Finally, reload the HAProxy configuration to apply the changes.

sudo service haproxy reload

Troubleshooting Common Issues

If you encounter any issues during the configuration process, here are some common troubleshooting steps:

  • Check the HAProxy logs for errors
  • Verify that the SSL certificates are correctly installed and configured
  • Check the backend and frontend configurations for typos or syntax errors
  • Verify that the servers are correctly configured and running

Conclusion

Configuring multiple single domain SSL certificates for your domain and subdomains using HAProxy can be a challenging task. However, by following the steps outlined in this article, you should be able to achieve a secure and scalable solution for your web infrastructure.

Remember to test your configuration thoroughly to ensure that each domain and subdomain is correctly served with its respective SSL certificate.

Domain/Subdomain SSL Certificate Backend Frontend
example.com example_com.pem example_com example_com_frontend
blog.example.com blog_example_com.pem blog_example_com blog_example_com_frontend
shop.example.com shop_example_com.pem shop_example_com shop_example_com_frontend

By following this guide, you’ll be able to configure multiple single domain SSL certificates for your domain and subdomains using HAProxy, ensuring a secure and scalable solution for your web infrastructure.

Frequently Asked Question

Get the lowdown on configuring multiple single domain SSL certs for domain and subdomain with HAProxy!

How do I configure HAProxy to use multiple single domain SSL certificates for my domain and subdomain?

To configure HAProxy to use multiple single domain SSL certificates, you’ll need to create separate bind lines for each domain or subdomain in your HAProxy configuration file. For example, if you have a domain example.com and a subdomain sub.example.com, you can use the following configuration:

`bind *:443 ssl crt example.com.pem crt sub.example.com.pem`

This tells HAProxy to listen on port 443 and use the specified SSL certificates for each domain or subdomain.

What is the purpose of the crt parameter in the HAProxy configuration?

The crt parameter in the HAProxy configuration specifies the SSL/TLS certificate files to use for a particular bind line. You can specify multiple certificate files, separated by spaces, to enable HAProxy to use multiple certificates for different domains or subdomains.

Can I use a single wildcard certificate for both my domain and subdomain?

Yes, you can use a single wildcard certificate for both your domain and subdomain. A wildcard certificate is a single certificate that can be used for multiple subdomains of a domain. For example, a wildcard certificate for *.example.com can be used for both example.com and sub.example.com.

How do I specify the SSL certificate chain in HAProxy?

To specify the SSL certificate chain in HAProxy, you’ll need to concatenate the SSL certificate, intermediate certificate, and root certificate into a single file, in that order. Then, specify the file path in the crt parameter of the bind line.

Do I need to restart HAProxy after updating the SSL certificates?

Yes, you’ll need to restart HAProxy or reload the configuration after updating the SSL certificates. This ensures that HAProxy loads the new certificates and starts using them for SSL/TLS encryption.

Leave a Reply

Your email address will not be published. Required fields are marked *