How to set up a reverse proxy with HTTPS in less than 2 minutes with Caddy on Ubuntu

Prerequisites

Install Caddy

echo "deb [trusted=yes] https://apt.fury.io/caddy/ /" \
    | sudo tee -a /etc/apt/sources.list.d/caddy-fury.list
sudo apt update
sudo apt install caddy
Install Caddy via the apt package manager

Here's what these commands do:

  1. Add the Caddy source to your local package registry
  2. Pull in the new packages
  3. Install Caddy

Setup Caddy

This is painfully easy. When you installed Caddy it was automatically added as a service, all you need to do is edit the configuration file located at /etc/caddy/Caddyfile . Open it with your favourite text editor and paste the following:

your.domain.com

reverse_proxy 127.0.0.1:8000

Substitute your.domain.com for – well – your domain (don't forget to add a DNS record pointing to your server) and 8000 for the port you want to reverse proxy requests to.

Restart the Caddy service with sudo systemctl restart caddy and Caddy will automatically issue a SSL certificate for your domain via Let's Encrypt and activate HTTPS – no more configuration required.

Extra: If you want to add multiple domains

Just change the Caddyfile to the following and restart the service:

your.domain.com {

	reverse_proxy 127.0.0.1:8000
    
}

other.domain.com {
	
    reverse_proxy 127.0.0.1:5432

}

Extra: More configuration options

There are a lot more directives (that's what the reverse_proxy part is called). If you're curious what else Caddy can do (it's a full featured web server – e.g. a lot) check out their brilliant documentation.

Show Comments