Setup Caddy Load Balancer on Ubuntu in Azure/AWS/GCP

Setup and install Caddy Load Balancer on Ubuntu 24.04 in Azure, AWS of Google GCP.  Use our Caddy image from any of the cloud marketplaces below.  Caddy Load Balancer solution provides a powerful, secure, and flexible tool for managing web traffic across multiple backend servers.  This load balance solution is ideal for organizations that need an easy-to-configure and highly effective reverse proxy.

Cloud Caddy Load Balancer IaaS

Caddy Load Balancer Azure

Caddy Load Balancer Azure

Deploy Caddy Load Balancer on Ubuntu 24.04 in Azure

Caddy Load Balancer AWS

Coming Soon..

Caddy Load Balancer GCP

Coming Soon..

Getting Started with Caddy Load Balancer

Once your Caddy server has been deployed, the following links explain how to connect to a Linux VM:

 

 

Once connected and logged in, the following section explains how to start using Caddy as a Load Balancer.

Step 1: Verify Caddy Installation

After deploying Caddy, confirm it’s installed correctly by running:

				
					caddy version
				
			

Confirm Caddy service is running:

				
					sudo systemctl status caddy
				
			

Step 2: Configure the Caddyfile for Load Balancing

Caddy makes it straightforward to set up load balancing with its reverse_proxy directive and flexible policies. This configuration improves performance and fault tolerance by distributing traffic across multiple backend servers.

The Caddyfile is where you define your load balancing setup. Caddy can distribute traffic across multiple backend servers using various load-balancing strategies (like round-robin, least connections, or first available). Here’s a basic configuration:

 

  1. Open the Caddyfile:
				
					sudo nano /etc/caddy/Caddyfile

				
			

2. Define the Load Balancing Setup:

  • Replace <yourdomain.com>, server1, and server2 with your actual domain and backend servers.
				
					<yourdomain.com> {
    reverse_proxy {
        to http://server1:8080 http://server2:8080
        lb_policy round_robin
        lb_try_duration 10s
        lb_try_interval 2s
    }
}

				
			

This configuration:

  • Distributes requests evenly across server1 and server2 using round-robin.
  • lb_try_duration and lb_try_interval specify how long Caddy will try each backend before moving to the next in case of failure.

3. Optional: Use Different Load Balancing Policies Caddy supports several policies:

  • round_robin: Distributes requests evenly across backends.
  • least_conn: Sends requests to the backend with the fewest connections.
  • first: Always tries the first backend and falls back to others only if the first is unavailable.

 

Example for least connections:

				
					lb_policy least_conn

				
			

4. Save and Exit the Caddyfile.

Further documentation on configuration of the Caddy file can be found on: https://caddyserver.com/docs/

Step 3. Enable HTTPS (Optional but Recommended)

If you’re using a public domain, Caddy will automatically obtain SSL certificates from Let’s Encrypt. Ensure your DNS is set up to point to the server running Caddy.

Step 4. Restart Caddy to Apply Changes

After saving your configuration, restart Caddy to apply the changes:

				
					sudo systemctl reload caddy
				
			

Step 5. Testing the Load Balancer

Method 1: Use curl

Run curl requests to your domain and confirm that Caddy routes them to different backend servers:

				
					curl -I http://<yourdomain.com>

				
			

Look at response headers or logs on the backend servers to confirm that traffic is being distributed as expected.

Method 2: Monitor Server Logs

Each backend server should log incoming requests. By comparing the logs across servers, you can see if Caddy is distributing traffic evenly.

Method 3: Load Testing (Optional)

For more comprehensive testing, use a load-testing tool like Apache Benchmark (ab) or wrk:

				
					ab -n 100 -c 10 http://<yourdomain.com>/

				
			

This command sends 100 requests with a concurrency of 10, letting you verify how Caddy handles traffic distribution under load.

Additional Options

Health Checks: Use lb_health_path to specify a health-check path. Caddy will periodically check this path and avoid sending requests to any backend that’s unavailable.

				
					lb_health_path /health

				
			

Timeout Settings: Adjust lb_try_duration and lb_try_interval to control retry behavior.

Caddy Documentation / Support

For further details and documentation refer to the following:

 

https://caddyserver.com/docs/

 

Check Caddy logs (usually found in /var/log/caddy/) to check for any errors.

 

If you’re having any issues with deployment please contact us for support.

Firewall Ports

For a Caddy load balancer setup with reverse proxy, the following ports are typically required:

 

  1. Port 80 (HTTP):
    • Used to receive and handle unencrypted HTTP requests. If HTTPS is enabled, Caddy will redirect HTTP requests on port 80 to HTTPS on port 443.

 

2. Port 443 (HTTPS):

    • Used for secure, encrypted HTTPS connections. If Caddy’s automatic HTTPS is enabled, Caddy will use this port to serve content over SSL/TLS.

 

3. Backend Server Ports:

    • The ports your backend servers are listening on (e.g., 8080, 8081, etc.). Caddy will forward traffic to these ports as per the load balancing setup in the Caddyfile.

The links below explain how to modify / create firewall rules depending on which cloud platform you are using.

 

To setup AWS firewall rules refer to – AWS Security Groups

To setup Azure firewall rules refer to – Azure Network Security Groups

To setup Google GCP firewall rules refer to – Creating GCP Firewalls

Disclaimer: Caddy® is a registered trademark of Light Code Labs, LLC and is licensed under Apache License 2.0 license. The license comes with a “no warranty” clause, meaning the software is provided “as-is” without any guarantees or liability for issues that may arise.

Avatar for Andrew Fitzgerald
Andrew Fitzgerald

Cloud Solution Architect. Helping customers transform their business to the cloud. 20 years experience working in complex infrastructure environments and a Microsoft Certified Solutions Expert on everything Cloud.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x