Caddy Server v2 Reverse Proxy Setup Guide

Self-Hosting Nov 19, 2020

Last Updated: 11th May 2023

What is Caddy?

Caddy has a wide range of use cases including:

  • Web Server
  • Reverse Proxy
  • Sidecar Proxy
  • Load Balancer
  • API Gateway
  • Ingress Controller
  • System Manager
  • Process Supervisor
  • Task Scheduler

Today we will be installing and setting up Caddy as a Reverse Proxy. This will be on a Debian 10 system but this will also work with Ubuntu 20.

Install - Official Install Docs: https://caddyserver.com/docs/download
1.

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
  1. sudo apt update
  2. sudo apt install caddy

Check status: sudo systemctl status caddy
You should see something similar to this:

● caddy.service - Caddy
   Loaded: loaded (/lib/systemd/system/caddy.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2020-11-15 20:00:00 UTC; 2min 35s ago
     Docs: https://caddyserver.com/docs/
 Main PID: 6969 (caddy)
    Tasks: 7 (limit: 1167)
   Memory: 15.5M
   CGroup: /system.slice/caddy.service
           └─4028 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
  1. Now head to your server's IP e.g. http://127.0.0.1
    You should see:
    Alt

Reverse Proxy Setup

  1. Create Caddyfile: nano Caddyfile

Use the following template:

example.com {
    reverse_proxy 127.0.0.1:8080
}

Basic Repsond Template:

hello.example.com {
    repsond "Hello World!"
}
  1. Restart Caddy and adopt the new config
    caddy reload

HTTPS
Once the new config is loaded, a Let's Encrypt HTTPS certificate is automatically generated for the domain. Just make sure the A record points to the Caddy server!

Cool Fact: If Let's Encrypt is down, Caddy will automatically fall over to obtain a free HTTPS certificate form ZeroSSL.

Additional Settings

  • Load Balancing
  • Active Health Checks
  • Passive Health Checks
  • Streaming
  • Header Manipulation
  • Transport

That's it! ‌
‌Now grab a coffee ☕️

Our other Caddy Guides:

Caddy Caddyfile Advanced Setup
Our Caddy Setup Guide can be found here [/caddy-setup/]. What will be covered? * HTTP Headers [/caddy-advanced-setup/#headers] * Custom Certificates [/caddy-advanced-setup/#customsslcertificates] * Using an alternative ACME Certificate Autority [/caddy-advanced-setup/#zerosslacmesetup] (ZeroS…
How to enable Caddy HTTP Basic Authentication (basicauth) Guide
What is HTTP Basic Authentication?When visiting a website, protected with Basic Auth the browser will prompt theuser to enter a username and password before any recourses are loaded. The client will then send an Authorization header with each request to thewebsite, to maintain authentication.T…

Tags