Setup Caddy CDN on Ubuntu in Azure/AWS/GCP

Setup and install a CDN using Caddy on Ubuntu 24.04 in Azure, AWS or Google GCP.  Self-hosted content delivery network CDN using Caddy.  Caddy CDN offers fast, secure, and reliable delivery of static and dynamic web assets. With its caching, SSL, and reverse proxy capabilities, Caddy CDN optimizes web performance and scales content delivery without complex setups.  Deploy using our Caddy image from any of the cloud marketplaces, links below:

Self-Hosted Cloud Caddy CDN

Azure Caddy CDN

Caddy CDN Azure

Deploy Caddy CDN on Ubuntu 24.04 in Azure

AWS Caddy CDN

Coming soon..

GCP Caddy CDN

Coming soon..

Getting Started with Caddy CDN

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 Content Delivery Network (CDN).

Check Caddy Status and Version

To confirm Caddy is running and the version, run the following commands:

				
					sudo systemctl status caddy
				
			
				
					caddy version
				
			

Setup Caddy as Content Delivery Network (CDN)

Here’s a step-by-step guide to setting up Caddy as a basic Content Delivery Network (CDN). This setup will use Caddy’s reverse proxy and caching capabilities to distribute content efficiently across multiple locations.

Prerequisites

  • Basic knowledge of configuring Caddy’s Caddyfile.
  • Optional: Multiple Caddy servers distributed geographically for improved latency.

 

CaddyFile Documentation:

https://caddyserver.com/docs/caddyfile

Step 1. Configure Caching Rules

  • Open the Caddyfile in a text editor:
				
					sudo nano /etc/caddy/Caddyfile
				
			

Create or modify your Caddyfile to define caching rules for static assets. Here’s an example of caching images, CSS, and JavaScript for 30 days:

				
					:80 {
    route {
        # Caching static assets
        file_server
        try_files {path} {path}/ /index.html
        header {
            Cache-Control "public, max-age=2592000" # 30 days in seconds
        }
        # Enable caching for certain file types
        @cacheable {
            path_regexp cachePath \.(jpg|jpeg|png|gif|svg|css|js)$
        }
        handle @cacheable {
            cache {
                ttl 2592000
                header X-Cache-Status
            }
        }
    }
}

				
			

This setup will cache static content for 30 days, improving load times for repeat visitors. The X-Cache-Status header lets you see cache hits and misses.

Once you’ve saved changes and exited the caddy file, reload Caddy to apply changes:

				
					sudo systemctl reload caddy
				
			

Testing Caching in Caddy

Use curl to send multiple requests to a cached endpoint, inspecting the response headers to see if caching is working:

				
					curl -I http://yourdomain.com

				
			
  • Look for the X-Cache-Status header in the response. A response of HIT indicates that the cache is working, while MISS suggests that content is not yet cached.

Step 2. Enable Reverse Proxy to Origin Server

Set up Caddy as a reverse proxy to your main origin server. In your Caddyfile, configure it to point requests to the origin:

				
					:80 {
    reverse_proxy origin.example.com
    header {
        Cache-Control "public, max-age=3600" # 1 hour
    }
}

				
			

This will cache requests at the edge for one hour before checking the origin server again. Adjust the max-age to your content needs.

Step 3. Use SSL/TLS for Secure Connections

Caddy can automatically manage SSL certificates using Let’s Encrypt. Add HTTPS to ensure secure connections between clients and your Caddy servers:

				
					https://cdn.example.com {
    reverse_proxy origin.example.com
    tls {
        issuer acme
    }
}

				
			

Step 4. Restart Caddy to Apply Changes

Once you’ve completed all your configuration, After saving your configuration, restart Caddy to apply the changes:

				
					sudo systemctl reload caddy
				
			

Step 5. Deploy Caddy Servers Geographically (Optional)

  • To achieve CDN-like distribution, deploy multiple Caddy servers using our image in different geographic regions and configure them to cache content.
  • Use a DNS provider with geo-routing to route requests to the nearest Caddy server, reducing latency.

Step 6. Monitoring and Adjusting Cache

  • Use the X-Cache-Status response header to monitor cache performance (HIT, MISS, or STALE).
  • Adjust caching rules and TTLs based on content freshness needs.
  • Check Caddy logs (usually found in /var/log/caddy/)

Optional: Load Balancing Across Multiple Origins

If your origin server is replicated across regions, you can add load balancing:

				
					:80 {
    reverse_proxy origin1.example.com origin2.example.com {
        lb_policy round_robin
    }
}

				
			

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.

Caddy Firewall Ports

Caddy CDN server uses the following ports.

 

  • TCP 80
  • TCP 443

 

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